【问题标题】:How to change programmatically the application language in an eclipse application?如何以编程方式更改 Eclipse 应用程序中的应用程序语言?
【发布时间】:2023-03-24 05:05:01
【问题描述】:

我刚刚完成了this 国际化教程,我对此非常满意。但我想知道,如果我构建一个工具栏并为用户提供一些更改语言的选项,它将如何在我的应用程序中工作?我确信应用程序需要重新启动,我期待 eclipse 为其提供动态行为。所以没有期望。

【问题讨论】:

    标签: eclipse eclipse-plugin internationalization eclipse-rcp


    【解决方案1】:

    你应该看看这篇博文:

    http://blog.vogella.com/2013/05/03/eclipse-internationalization-part-14-current-situation-by-dirk-fauth/

    在 e4 环境中有一个org.eclipse.e4.core.services.nls.ILocaleChangeService,但是你需要为它准备你的应用程序。

    最好下载源码看看: https://github.com/fipro78/e4translationexample

    如果您不使用 e4 或不介意重新启动,您可以使用此代码(未测试):

        // set the eclipse relaunch property
        StringBuffer arguments = new StringBuffer();
        arguments.append("${eclipse.vm}\n"); //$NON-NLS-1$
        arguments.append("-nl\n").append("de").append("\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    
        arguments.append("-vmargs\n"); //$NON-NLS-1$
        arguments.append(System.getProperty("eclipse.vmargs")); //$NON-NLS-1$
    
        System.setProperty("eclipse.exitcode", Integer.toString(IApplication.EXIT_RELAUNCH)); //$NON-NLS-1$
        System.getProperties().setProperty(IApplicationContext.EXIT_DATA_PROPERTY, arguments.toString());
        PlatformUI.getWorkbench().restart();
    

    请记住,这在您的开发环境中不起作用,因为不支持重新启动。

    【讨论】:

    • 您是从 Eclipse 还是产品运行?
    • 两种方式都试过了。没有成功。唯一对我有用的是操作产品中的 config.ini。但这会有点 hacky,这就是为什么我正在寻找更好的解决方案
    • 你在运行上面的代码吗?你修改了语言环境吗? arguments.append("-nl\n").append("de")...
    【解决方案2】:

    我们这样做:

    IPreferenceStore preferenceStore = new ScopedPreferenceStore(new InstanceScope(),getBundle().getSymbolicName());
    preferenceStore.setValue("language", locale.toString()); //$NON-NLS-1$
    getWorkbench().restart();
    

    虽然您可以从绑定到处理程序的参数化命令中获取语言环境,例如:

    public class ChangeLanguageHandler extends AbstractHandler implements IElementUpdater {
    
      private static final String PARAM = "lang"; //$NON-NLS-1$
    
      @Override
      public Object execute(final ExecutionEvent event) throws ExecutionException {
        // Consider to supress deselection event:
        if (event.getTrigger() instanceof Event) {
          final Widget widget = ((Event) event.getTrigger()).widget;
          if (widget instanceof MenuItem) {
            if (!((MenuItem) widget).getSelection()) {
              return null;
            }
          }
        }
        final Shell shell = HandlerUtil.getActiveShell(event);
        final String lang = event.getParameter(PARAM);
        if (openDialogAndAskIfUserIsSureAboutThis_BecauseItRequiresARestart) {
          invokeCodeAboveWithTheNewLocale(new Locale(lang));
        }
        return null;
      }
    
      // Update the Checked State...
      @SuppressWarnings("rawtypes")
      @Override
      public void updateElement(final UIElement element, final Map parameters) {
        final Object param = parameters.get(PARAM);
        element.setChecked(Locale.getDefault().getLanguage().equals(param));
      }
    

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 2020-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 2012-05-29
      相关资源
      最近更新 更多