67 |
67 |
/**
|
68 |
68 |
* The constructor.
|
69 |
69 |
*/
|
70 |
|
public LanguageSwitchHandler() {
|
71 |
|
}
|
72 |
|
|
|
70 |
public LanguageSwitchHandler() {}
|
|
71 |
|
73 |
72 |
/**
|
74 |
73 |
* the command has been executed, so extract extract the needed information
|
75 |
74 |
* from the application context.
|
... | ... | |
80 |
79 |
*/
|
81 |
80 |
@Override
|
82 |
81 |
public Object execute(ExecutionEvent event) throws ExecutionException {
|
83 |
|
|
|
82 |
|
84 |
83 |
Shell shell = HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell();
|
85 |
84 |
|
86 |
|
String locale = Locale.getDefault().getLanguage();//RCPPreferences.getInstance().getString(RCPPreferences.UI_LOCALE);
|
|
85 |
String locale = Locale.getDefault().getLanguage();// RCPPreferences.getInstance().getString(RCPPreferences.UI_LOCALE);
|
87 |
86 |
|
88 |
|
ArrayList<String> values = new ArrayList<String>();
|
|
87 |
ArrayList<String> values = new ArrayList<>();
|
89 |
88 |
values.add("fr"); //$NON-NLS-1$
|
90 |
89 |
values.add("en"); //$NON-NLS-1$
|
91 |
|
//values.add("ru"); //$NON-NLS-1$
|
|
90 |
values.add("ru"); //$NON-NLS-1$
|
92 |
91 |
|
93 |
92 |
if (!values.contains(locale)) { // set selected locale
|
94 |
93 |
locale = "en";
|
95 |
94 |
}
|
96 |
95 |
|
97 |
|
ComboDialog d = new ComboDialog(shell, TXMUIMessages.chooseTheLanguage, values, locale); //$NON-NLS-1$
|
|
96 |
ComboDialog d = new ComboDialog(shell, TXMUIMessages.chooseTheLanguage, values, locale);
|
98 |
97 |
if (d.open() == Window.OK) {
|
99 |
98 |
locale = d.getSelectedValue();
|
100 |
|
//RCPPreferences.getInstance().put(RCPPreferences.CONFIG_LOCALE, locale);
|
101 |
|
} else {
|
|
99 |
// RCPPreferences.getInstance().put(RCPPreferences.CONFIG_LOCALE, locale);
|
|
100 |
}
|
|
101 |
else {
|
102 |
102 |
return null;
|
103 |
103 |
}
|
104 |
104 |
|
105 |
105 |
// Does not work:
|
106 |
|
// System.getProperties().setProperty(IApplicationContext.EXIT_DATA_PROPERTY, "-nl " + locale);
|
107 |
|
// changeLocaleTXMIni(locale);
|
|
106 |
// System.getProperties().setProperty(IApplicationContext.EXIT_DATA_PROPERTY, "-nl " + locale);
|
|
107 |
// changeLocaleTXMIni(locale);
|
108 |
108 |
|
109 |
109 |
if (changeLocaleConfigIni(locale)) {
|
110 |
110 |
PlatformUI.getWorkbench().restart();
|
111 |
|
} else {
|
112 |
|
Log.warning("** Error: TXM language configuration could not be set. You can set TXM language by editing HOME/TXM-<version>/.txm/TXM.ini) configuration file by adding '-nl\n<language code>' before the '-vmargs' line.");
|
113 |
111 |
}
|
|
112 |
else {
|
|
113 |
Log.warning(
|
|
114 |
"** Error: TXM language configuration could not be set. You can set TXM language by editing HOME/TXM-<version>/.txm/TXM.ini) configuration file by adding '-nl\n<language code>' before the '-vmargs' line.");
|
|
115 |
}
|
114 |
116 |
|
115 |
117 |
return null;
|
116 |
118 |
}
|
117 |
|
|
|
119 |
|
118 |
120 |
/**
|
119 |
121 |
* Change locale of TXM.ini.
|
120 |
122 |
*
|
121 |
123 |
* @param locale the locale
|
122 |
124 |
*/
|
123 |
125 |
public static void changeLocaleTXMIni(String locale) {
|
124 |
|
|
125 |
|
// Location configArea = Platform.getInstallLocation();
|
126 |
|
// //MessageBox.warning(Platform.getInstanceLocation().getURL().toString());
|
127 |
|
// // String product = Platform.getProduct().getName();
|
128 |
|
// if (configArea == null) {
|
129 |
|
// return;
|
130 |
|
// }
|
131 |
|
|
|
126 |
|
|
127 |
// Location configArea = Platform.getInstallLocation();
|
|
128 |
// //MessageBox.warning(Platform.getInstanceLocation().getURL().toString());
|
|
129 |
// // String product = Platform.getProduct().getName();
|
|
130 |
// if (configArea == null) {
|
|
131 |
// return;
|
|
132 |
// }
|
|
133 |
|
132 |
134 |
URL location = null;
|
133 |
135 |
try {
|
134 |
136 |
String path = System.getProperty("osgi.instance.area"); //$NON-NLS-1$
|
135 |
137 |
location = new URL(path + "TXM.ini"); //$NON-NLS-1$
|
136 |
|
} catch (MalformedURLException e) {
|
|
138 |
}
|
|
139 |
catch (MalformedURLException e) {
|
137 |
140 |
System.err.println(NLS.bind(TXMUIMessages.switchLanguageColonMalformedUrlColonP0, location));
|
138 |
141 |
return;
|
139 |
142 |
}
|
... | ... | |
148 |
151 |
file.renameTo(new File(fileName));
|
149 |
152 |
|
150 |
153 |
// update TXM.ini
|
151 |
|
System.out.println(TXMUIMessages.tXMiniColon+location);
|
152 |
|
Log.severe(TXMUIMessages.tXMiniColon+location);
|
|
154 |
System.out.println(TXMUIMessages.tXMiniColon + location);
|
|
155 |
Log.severe(TXMUIMessages.tXMiniColon + location);
|
153 |
156 |
try {
|
154 |
|
|
|
157 |
|
155 |
158 |
BufferedReader in = new BufferedReader(new FileReader(fileName));
|
156 |
159 |
BufferedWriter out = new BufferedWriter(new FileWriter(location.getFile()));
|
157 |
160 |
try {
|
... | ... | |
159 |
162 |
boolean isNlWiritten = false;
|
160 |
163 |
String line = in.readLine();
|
161 |
164 |
while (line != null) {
|
162 |
|
|
|
165 |
|
163 |
166 |
// write nl before vmargs
|
164 |
167 |
if (line.equals("-vmargs") && !isNlWiritten) //$NON-NLS-1$
|
165 |
168 |
{
|
... | ... | |
169 |
172 |
out.newLine();
|
170 |
173 |
isNl = true;
|
171 |
174 |
}
|
172 |
|
|
|
175 |
|
173 |
176 |
// write line if -nl was not found in previous line
|
174 |
177 |
// else locale
|
175 |
178 |
if (!isNl) {
|
176 |
179 |
out.write(line);
|
177 |
|
} else {
|
|
180 |
}
|
|
181 |
else {
|
178 |
182 |
out.write(locale);
|
179 |
183 |
isNl = false;
|
180 |
184 |
isNlWiritten = true;
|
181 |
185 |
}
|
182 |
186 |
out.newLine();
|
183 |
|
|
|
187 |
|
184 |
188 |
// check nl line
|
185 |
189 |
if (line.equals("-nl")) { //$NON-NLS-1$
|
186 |
190 |
isNl = true;
|
187 |
191 |
}
|
188 |
|
|
|
192 |
|
189 |
193 |
line = in.readLine();
|
190 |
194 |
}
|
191 |
195 |
// create nl if it was not presents
|
... | ... | |
196 |
200 |
out.newLine();
|
197 |
201 |
}
|
198 |
202 |
out.flush();
|
199 |
|
} finally {
|
|
203 |
}
|
|
204 |
finally {
|
200 |
205 |
if (in != null) {
|
201 |
206 |
try {
|
202 |
207 |
in.close();
|
203 |
|
} catch (IOException e) {
|
|
208 |
}
|
|
209 |
catch (IOException e) {
|
204 |
210 |
org.txm.utils.logger.Log.printStackTrace(e);
|
205 |
211 |
}
|
206 |
212 |
}
|
207 |
213 |
if (out != null) {
|
208 |
214 |
try {
|
209 |
215 |
out.close();
|
210 |
|
} catch (IOException e) {
|
|
216 |
}
|
|
217 |
catch (IOException e) {
|
211 |
218 |
org.txm.utils.logger.Log.printStackTrace(e);
|
212 |
219 |
}
|
213 |
220 |
}
|
214 |
221 |
}
|
215 |
|
} catch (FileNotFoundException e) {
|
|
222 |
}
|
|
223 |
catch (FileNotFoundException e) {
|
216 |
224 |
org.txm.utils.logger.Log.printStackTrace(e);
|
217 |
|
} catch (IOException e) {
|
|
225 |
}
|
|
226 |
catch (IOException e) {
|
218 |
227 |
org.txm.utils.logger.Log.printStackTrace(e);
|
219 |
228 |
}
|
220 |
229 |
}
|
... | ... | |
231 |
240 |
if (configArea == null) {
|
232 |
241 |
return false;
|
233 |
242 |
}
|
234 |
|
|
235 |
|
String fileName = Platform.getConfigurationLocation().getURL().getFile() +
|
|
243 |
|
|
244 |
String fileName = Platform.getConfigurationLocation().getURL().getFile() +
|
236 |
245 |
System.getProperty("file.separator") + "config.ini"; //$NON-NLS-1$ //$NON-NLS-2$
|
237 |
|
|
|
246 |
|
238 |
247 |
File file = new File(fileName);
|
239 |
248 |
if (!file.exists()) {
|
240 |
249 |
MessageBox.error(NLS.bind(TXMUIMessages.theFileP0DoesNotExist, file.getAbsolutePath()));
|
... | ... | |
244 |
253 |
file.renameTo(new File(fileNameBackup));
|
245 |
254 |
|
246 |
255 |
// update config.ini
|
247 |
|
Log.info("config.ini: "+fileName); //$NON-NLS-1$
|
|
256 |
Log.info("config.ini: " + fileName); //$NON-NLS-1$
|
248 |
257 |
try {
|
249 |
|
|
|
258 |
|
250 |
259 |
BufferedReader in = new BufferedReader(new FileReader(fileNameBackup));
|
251 |
260 |
BufferedWriter out = new BufferedWriter(new FileWriter(fileName));
|
252 |
261 |
try {
|
253 |
262 |
boolean isNlWritten = false;
|
254 |
263 |
String line = in.readLine();
|
255 |
264 |
while (line != null) {
|
256 |
|
|
|
265 |
|
257 |
266 |
if (!isNlWritten) {
|
258 |
267 |
if (line.contains("osgi.nl=")) { //$NON-NLS-1$
|
259 |
268 |
out.write("osgi.nl=" + locale); //$NON-NLS-1$
|
260 |
269 |
isNlWritten = true;
|
261 |
|
} else
|
|
270 |
}
|
|
271 |
else
|
262 |
272 |
out.write(line);
|
263 |
|
} else
|
|
273 |
}
|
|
274 |
else
|
264 |
275 |
out.write(line);
|
265 |
276 |
|
266 |
277 |
out.newLine();
|
... | ... | |
272 |
283 |
out.newLine();
|
273 |
284 |
}
|
274 |
285 |
out.flush();
|
275 |
|
} finally {
|
|
286 |
}
|
|
287 |
finally {
|
276 |
288 |
if (in != null) {
|
277 |
289 |
try {
|
278 |
290 |
in.close();
|
279 |
|
} catch (IOException e) {
|
|
291 |
}
|
|
292 |
catch (IOException e) {
|
280 |
293 |
org.txm.utils.logger.Log.printStackTrace(e);
|
281 |
294 |
}
|
282 |
295 |
}
|
283 |
296 |
if (out != null) {
|
284 |
297 |
try {
|
285 |
298 |
out.close();
|
286 |
|
} catch (IOException e) {
|
|
299 |
}
|
|
300 |
catch (IOException e) {
|
287 |
301 |
org.txm.utils.logger.Log.printStackTrace(e);
|
288 |
302 |
}
|
289 |
303 |
}
|
290 |
304 |
}
|
291 |
|
} catch (FileNotFoundException e) {
|
|
305 |
}
|
|
306 |
catch (FileNotFoundException e) {
|
292 |
307 |
org.txm.utils.logger.Log.printStackTrace(e);
|
293 |
|
} catch (IOException e) {
|
|
308 |
}
|
|
309 |
catch (IOException e) {
|
294 |
310 |
org.txm.utils.logger.Log.printStackTrace(e);
|
295 |
311 |
}
|
296 |
312 |
|
297 |
313 |
String txt;
|
298 |
314 |
try {
|
299 |
315 |
txt = IOUtils.getText(file, "UTF-8");
|
300 |
|
return txt.contains("osgi.nl="+locale);
|
301 |
|
} catch (IOException e) {
|
|
316 |
return txt.contains("osgi.nl=" + locale);
|
|
317 |
}
|
|
318 |
catch (IOException e) {
|
302 |
319 |
e.printStackTrace();
|
303 |
320 |
return false;
|
304 |
321 |
}
|
305 |
322 |
}
|
306 |
|
}
|
|
323 |
}
|