我正在尝试以编程方式更改我的RCP插件的语言。到目前为止我没有运气。以下方法对我不起作用。重启后语言不会改变。我使用特定文件夹中的侦听翻译文件动态填充Menu
。选择LanguageSelectedListener.widgetSelected
时的方法MenuItem
。尝试使用导出的产品并从eclipse运行。
private void setMenuLanguages(Menu menu){
File dir = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getFile() + language_directory);
String[] dirList = dir.list();
String currentLocale = System.getProperty("user.language");
Locale current = new Locale(currentLocale, "", "");
Locale[] locales = new Locale[dirList.length];
LanguageSelectedListener listener = new LanguageSelectedListener();
for(int i=0; i<dirList.length; i++){
String file = dirList[i].split(".properties")[0];
String locShort;
if(file.equals("messages"))locShort = "en"; //default english
else locShort = file.split("_")[1];
locales[i] = new Locale(locShort);
MenuItem menuItem = new MenuItem(menu, SWT.RADIO, i);
menuItem.setText(locales[i].getDisplayName());
menuItem.setData("locale", locales[i]);
menuItem.addSelectionListener(listener);
if(locales[i].getLanguage().equals(current.getLanguage()))
menuItem.setSelection(true);
}
return;
}
private class LanguageSelectedListener extends SelectionAdapter{
@Override
public void widgetSelected(SelectionEvent e) {
MenuItem item = (MenuItem) e.widget;
if(!item.getSelection() || !MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), "Info", "Programm wird neu gestartet. Möchten Sie fortfahren?")){
item.setSelection(false);
return;
}
Locale locale = (Locale) item.getData("locale");
StringBuffer arguments = new StringBuffer();
arguments.append("${eclipse.vm}\n"); //$NON-NLS-1$
arguments.append("-nl\n").append(locale.getLanguage()).append("\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.setProperty("eclipse.exitcode", Integer.toString(IApplication.EXIT_RELAUNCH)); //$NON-NLS-1$
System.getProperties().setProperty(IApplicationContext.EXIT_DATA_PROPERTY, arguments.toString());
PlatformUI.getWorkbench().restart();
}
}
所以我通过操纵产品的ini文件(而不是config.ini文件)实现了hacky解决方案。对我来说效果很好。
String installLoc = Platform.getInstallLocation().getURL().getPath();
String oldIniLocation = installLoc + "decoder.ini";
String content = null;
File oldIni = null;
try {
oldIni = new File(oldIniLocation);
content = FileUtils.readFileToString(oldIni, "UTF-8");
} catch (IOException e1) {
throw new RuntimeException("reading from file failed!", e1);
}
Locale locale = (Locale) item.getData("locale");
if(content.contains("-nl")){
content = content.replaceFirst("-nl\r\n..", "-nl\r\n" + locale.getLanguage());
}
else{
StringBuilder newContent = new StringBuilder("-nl\r\n" + locale.getLanguage() +"\r\n");
content = newContent.append(content).toString();
}
File newIni = new File(installLoc + "rename.ini");
try {
FileUtils.writeStringToFile(newIni, content, "UTF-8");
} catch (IOException e1) {
throw new RuntimeException("writing to file failed!", e1);
}
oldIni.delete();
//renaming newIni by moving it
oldIni = new File(installLoc + "decoder.ini");
try {
FileUtils.moveFile(newIni, oldIni);
} catch (IOException e1) {
throw new RuntimeException("renaming/moving file failed!", e1);
}
System.setProperty("eclipse.exitcode", Integer.toString(IApplication.EXIT_RESTART)); //$NON-NLS-1$
PlatformUI.getWorkbench().restart();