我有我的資源文件夾,這2個屬性文件: messages_hr_HR.properties和messages.properties的Java負載錯誤的資源
我不使用的Java類的Locale類我用我自定義的Locale類:
public class Locale {
private String bundleName;
private String localeName;
private String iconPath;
public Locale(String bundleName, String localeName, String iconPath) {
super();
this.bundleName = bundleName;
this.localeName = localeName;
this.iconPath = iconPath;
}
public String getBundleName() {
return bundleName;
}
public void setBundleName(String bundleName) {
this.bundleName = bundleName;
}
public String getLocaleName() {
return localeName;
}
public void setLocaleName(String localeName) {
this.localeName = localeName;
}
public String getIconPath() {
return iconPath;
}
public void setIconPath(String iconPath) {
this.iconPath = iconPath;
}
}
我有消息類的靜態方法返回鍵的值:
public class Messages {
private Messages() {
// do not instantiate
}
private static List<Locale> availableLocales;
private static Locale defaultLocale = new Locale("messages.messages", "EN", "/images/icons/locale/en.png");
static {
availableLocales = new ArrayList<Locale>();
availableLocales.add(defaultLocale);
availableLocales.add(new Locale("messages.messages_hr_HR", "HR", "/images/icons/locale/hr.png"));
}
private static Locale LOCALE = defaultLocale;
private static ResourceBundle RESOURCE_BUNDLE = loadBundle();
private static ResourceBundle loadBundle() {
return ResourceBundle.getBundle(LOCALE.getBundleName());
}
public static String getString(String key) {
try {
ResourceBundle bundle = RESOURCE_BUNDLE == null ? loadBundle() : RESOURCE_BUNDLE;
return bundle.getString(key);
} catch (MissingResourceException e) {
return "!" + key + "!";
}
}
public static void setLocale(Locale locale) {
LOCALE = locale;
RESOURCE_BUNDLE = loadBundle();
}
public static Locale getLocale() {
return LOCALE;
}
public static List<Locale> getAvailableLocales() {
return availableLocales;
}
}
然後在我的GUI我有可用的語言單選按鈕菜單中,當用戶點擊一些我只是叫Messages.setLocale(clickedLanguageLocale);
所以你可以看到我負責加載特定的文件,而不是Java的。
問題在於,在某些計算機上它的行爲很奇怪。有些文字是英文(messages.properties),一些文字是克羅地亞文(messages_hr_HR)。首先想到的是一些操作系統的默認語言環境,但是因爲我使用了mine類,所以沒有任何意義。
此行爲的任何想法?
嗯,也許是因爲那些靜態加載?但我不認爲這是問題,因爲GUI必須加載才能選擇我想要的語言。所以如果加載GUI,資源也會被加載。然後在GUI類我對單選按鈕組偵聽:'localeMenuItem.addActionListener(新的ActionListener(){ \t \t \t \t \t \t \t \t @覆蓋 \t \t \t \t公共無效的actionPerformed(ActionEvent的五){ \t \t \t \t \t Messages.setLocale(區域); \t \t \t \t \t setStringValues(); \t \t \t \t} \t \t \t});'我設置新的語言環境。 – vale4674
嗯,看起來這可能是一個問題,因爲你使用的是默認的getBundle()。請閱讀以下文檔:http://docs.oracle.com/javase/6/docs/api/java/util/ResourceBundle.html#getBundle%28java.lang。字符串,%20java.util.Locale,%20java.lang.ClassLoader%29。 getBundle()將在用戶系統中使用默認的java.util.Locale,而不一定是您在Locale類中默認提到的那個。 – shams
也許這可能是問題所在。我怎樣才能加載一個包名?但另一件奇怪的事情是,HR語言環境在Locale類中不存在,不知何故'getBundle()'方法決定將其作爲默認值。 – vale4674