将语言环境正确设置为JFileChooser [英] Set locale properly to a JFileChooser
问题描述
在运行时更改语言环境时,我遇到了一个小问题.
I'm having a little problem when I change Locale at runtime.
目标
我必须根据配置文件更改应用程序语言的语言环境.
I have to change the Locale of my application language according to a configuration file.
此语言环境不一定与主机/OS语言环境相同,也不必与JVM默认语言环境相同.
This locale does not necessarily be the same of the host/OS locale nor the JVM default locale.
此外,调用应用程序时无法修改user.language
.然后,我必须在运行时执行该操作.
Moreover, I can't modify user.language
when I call the application. Then, I must do that at runtime.
问题
总结我的代码,我阅读了配置文件并获得了不同的选项(包括语言环境).之后,我根据这些配置的选项初始化应用程序环境.
Summarizing my code, I read the configuration file and get the different options (including locale). After that, I initialize the application environment according to these configured options.
之后,我构建框架并开始应用程序生命周期.
After, I build my frame and starts the application life-cycle.
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来解决该问题:
Well, during the life-cycle, you can open files. Obviously, I use a JFileChooser for that issue:
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jfc.setFileFilter(modele.FILTRE_OUVRIR);
jfc.showOpenDialog(null);
使用配置的语言环境设置的所有文件选择器,但类型选择器不会更改.下图显示了问题(操作系统语言环境:es_ES,配置的语言环境:fr_FR):
All the file chooser it's setted with the configured locale, but the type selector doesn't change. The following image shows the problem (OS Locale: es_ES, configured locale: fr_FR):
如您所见,在"Fichiers de type"组合框中:该选项以西班牙语而不是法语显示.
As you can see, in the combobox "Fichiers de type": the option is showed in Spanish instead of French.
请他人向我解释问题?我的代码有问题吗?由于我正在使用文件过滤器,因此可能会出现问题吗?
Colud someone explain me the problem? Is something wrong in my code? Could be a problem due to I'm using a file filter?
谢谢你的任何建议.
推荐答案
就像BasicFileChooserUI使用的acceptAllFilter中的错误一样,它不会查找文本的本地化版本:
Looks like a bug in the acceptAllFilter used by BasicFileChooserUI, it doesn't lookup the localized version of the text as it should:
// BasicFileChooserUI
/**
* Returns the default accept all file filter
*/
public FileFilter getAcceptAllFileFilter(JFileChooser fc) {
return acceptAllFileFilter;
}
// buggy acceptAllFilter: doens't respect locale
protected class AcceptAllFileFilter extends FileFilter {
public AcceptAllFileFilter() {
}
public boolean accept(File f) {
return true;
}
public String getDescription() {
return UIManager.getString("FileChooser.acceptAllFileFilterText");
}
}
如果您的模型返回null,则使用此默认值. (我看到的)唯一的出路是让模型返回一个执行正确操作的过滤器,例如f.i.:
This default is used if your model returns null. The only way out (that I see) is to let the model return a filter that does-the-right-thing, like f.i.:
protected class AcceptAllFileFilter extends FileFilter {
private Locale locale;
public AcceptAllFileFilter(Locale locale) {
this.locale = locale;
}
@Override
public boolean accept(File f) {
return true;
}
@Override
public String getDescription() {
return UIManager.getString("FileChooser.acceptAllFileFilterText", locale);
}
}
这篇关于将语言环境正确设置为JFileChooser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!