我在运行时更改Locale时遇到了一些小问题。
目标
根据配置文件,我必须更改应用程序语言的Locale。
此Locale不一定与主机/操作系统Locale或JVM默认Locale相同。
此外,在调用应用程序时,我不能修改user.language
。因此,我必须在运行时执行此操作。
问题
简要概述我的代码,我读取配置文件并获取不同的选项(包括Locale)。之后,我根据这些配置的选项初始化应用程序环境。
然后,我构建我的框架并启动应用程序生命周期。
public static void main(String[] args) {
File fichier;
Ini ini; //Ini4J object
Modele modele = new Modele(); //My Model class: it stores configuration and other stuff
try {
fichier = new File(Modele.CONFIGURATION);
ini = new Ini(fichier);
modele.setLocaleLang(ini.get(Modele.LOCALE, Modele.LANG, String.class));
// read more options
} catch(InvalidFileFormatException e) {
// exception processing
} catch (IOException e) {
// exception processing
} finally {
ini = null;
fichier = null;
}
// More code
JComponent.setDefaultLocale(modele.getLocaleLang());
// More initialization code
MyFrame fenetre = new MyFrame(modele);
fenetre.visualiser();
}
在生命周期中,您可以打开文件。显然,我使用JFileChooser解决此问题:
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setFileFilter(modele.FILTRE_OUVRIR);
jfc.showOpenDialog(null);
所有的文件选择器都是按照配置的语言环境设置的,但类型选择器没有改变。以下图片显示了问题(操作系统语言环境:es_ES,配置的语言环境:fr_FR):
![enter image description here](https://istack.dev59.com/A6i14.webp)
请问有人能解释一下这个问题吗?我的代码有什么问题吗?使用文件过滤器可能会出现问题吗?
感谢您提供任何建议。