在 Swing 中运行时更改语言环境

2022-01-18 00:00:00 internationalization java swing

我希望能够在运行时更改我的 Swing 应用程序中的语言环境,并让屏幕上的所有文本元素使用来自新语言环境的 ResourceBundle 的本地化文本进行自我更新.

I would like to be able to change the locale in my Swing application at runtime and have all the text elements on the screen update themselves with localized text from a ResourceBundle of the new locale.

这是否可以在不自定义 Swing 组件或为所有处理呈现本地化文本的组件创建 UIDelegates 的情况下完成?

Can this be done without customizing swing components or creating UIDelegates for all components that handle rendering localized text?

如果不是,那么我可以考虑实施什么好的解决方案?

If no, then what is a good solution I can consider implementing?

推荐答案

  1. 您有一个用于更改应用程序区域设置(并可能保留新值)的方法和另一个用于获取本地化字符串的方法.

  1. You have a method that is used to change app locale (and probably persist the new value) and another one to get localized strings.

创建接口:

interface LocaleChangeListener {
    void onLocaleChange();
}

通过需要能够在运行时更改语言环境并在覆盖 onLocaleChange() 中设置新值的 UI 组件实现它.

Implement it by UI components that need to be able to change locale at runtime and set the new values in overrides onLocaleChange().

现在,有一个侦听器列表,将通过第一种方法通知区域设置更改.

Now, have a list of listeners that will be notified on locale change by the first method.

相关文章