【问题标题】:Loading of Java message properties for internalization加载 Java 消息属性以进行内部化
【发布时间】:2015-09-04 14:28:15
【问题描述】:

我有以下 Java i18n 消息类:

public class Messages { 

    private static final String BUNDLE_NAME = "languages.message";
    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

    private Messages() {
    }

    public static String getI18n(String key) {
        try {
            return RESOURCE_BUNDLE.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }

    public static String getI18n(String key, Object... params  ) {
        try {
            return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }
}

我创建了以下消息属性文件:

message.properties
message_de.properties
message_de_DE.properties

在我的程序中,我根据系统的默认语言环境进行翻译。如果是de_DE,则加载德语消息属性message_de_DE.properties。

如果默认语言环境是de_CH,则没有消息属性文件。那么message_de.properties 是作为后备加载的还是我需要自己实现?

【问题讨论】:

    标签: java localization internationalization translation properties-file


    【解决方案1】:

    根据this 的博文,你是对的。

    因此,当您系统的默认语言环境是 de_DE 并且您请求语言环境 en_US 的资源时,属性文件的查找顺序是:

    1. MyApp_en_US.properties
    2. MyApp_en.properties
    3. MyApp_de_DE.properties
    4. MyApp_de.properties
    5. MyApp.properties

    【讨论】:

      猜你喜欢
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2013-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      相关资源
      最近更新 更多