java 在 Swing 中在运行时更改语言环境
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/615973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing locale at runtime in Swing
提问by Tony Eichelberger
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 应用程序中的语言环境,并让屏幕上的所有文本元素使用来自新语言环境的 ResourceBundle 的本地化文本进行自我更新。
Can this be done without customizing swing components or creating UIDelegates for all components that handle rendering localized text?
这是否可以在不自定义 Swing 组件或为处理呈现本地化文本的所有组件创建 UIDelegates 的情况下完成?
If no, then what is a good solution I can consider implementing?
如果不是,那么我可以考虑实施的好解决方案是什么?
采纳答案by yanchenko
You have a method that is used to change app locale (and probably persist the new value) and another one to get localized strings.
Create an interface:
interface LocaleChangeListener { void onLocaleChange(); }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.
您有一个用于更改应用程序区域设置(并可能保留新值)的方法和另一个用于获取本地化字符串的方法。
创建接口:
interface LocaleChangeListener { void onLocaleChange(); }通过需要能够在运行时更改区域设置并在 overrides 中设置新值的 UI 组件来实现它
onLocaleChange()。现在,有一个侦听器列表,这些侦听器将通过第一种方法在语言环境更改时收到通知。
回答by Marc
use ResourceBundle.getBundle(BUNDLE_NAME).getString(key);to access the Strings.
用于ResourceBundle.getBundle(BUNDLE_NAME).getString(key);访问字符串。
when updating the Default Locale e.g. via Locale.setDefault(Locale.GERMAN);clear the Resourcebundle cache: ResourceBundle.clearCache();
当更新默认语言环境时,例如通过Locale.setDefault(Locale.GERMAN);清除 Resourcebundle 缓存:ResourceBundle.clearCache();
the next call of ResourceBundle.getBundle(BUNDLE_NAME).getString(key);should the return the localized String of the chosen Locale.
下一次调用ResourceBundle.getBundle(BUNDLE_NAME).getString(key);应该返回所选语言环境的本地化字符串。
回答by James Van Huis
You may want to save the language preference out, and then require a restart of the app for changes to take effect.
您可能想要保存语言首选项,然后需要重新启动应用程序才能使更改生效。
Then, you should be able to use Locale.setDefault(Locale.<desired language>);on startup, prior to rendering the GUI. That should properly switch your locale, which will result in the desired .properties file(s) being loaded.
然后,Locale.setDefault(Locale.<desired language>);在渲染 GUI 之前,您应该能够在启动时使用。这应该正确切换您的语言环境,这将导致加载所需的 .properties 文件。
回答by Siegfried
What about, on changing locale, do a firePropertyChangeEvent("locale", "..."), then add propertyChangeListener() and register them, whereever labels and the like are to be updated?
怎么样,在改变语言环境时,做一个 firePropertyChangeEvent("locale", "..."),然后添加 propertyChangeListener() 并注册它们,无论在哪里更新标签等?
回答by Tom Hawtin - tackline
There's two obvious approaches I see:
我看到了两种明显的方法:
Instead of getting a Stringfrom the ResourceBundle, get some kind of event-source Stringholder. Documentwould be the very heavy solution, but anything that can handle replacing an immutable value will do. Instead of just setting the text on a label, say, have a method that also sets up a listener. Note, this quite a "heavy" solution.
不是String从中获取 a ,而是ResourceBundle获取某种事件源String持有者。Document将是非常繁重的解决方案,但任何可以处理替换不可变值的方法都可以。与其只是在标签上设置文本,不如说,有一种方法也可以设置侦听器。请注意,这是一个相当“重”的解决方案。
Alternatively, have a central repository of listeners that are fired on a locale change, that each then go back and re-execute the relevant part of the set up code (don't duplicate). For common cases where you have, say, a JLabelusing a resource string literally, then you can combine these all into one listener with a WeakHashMap<JLabel,String>. Sometimes it works out better to avoid lots of little listeners.
或者,拥有一个在区域设置更改时触发的监听器中央存储库,然后每个监听器返回并重新执行设置代码的相关部分(不要重复)。对于常见的情况,例如,JLabel从字面上使用资源字符串,然后您可以将所有这些组合到一个带有WeakHashMap<JLabel,String>. 有时,避免很多小听众会更有效。

