Java首选项和国际化(i18n) [英] Java Preferences and Internationalization (i18n)

查看:290
本文介绍了Java首选项和国际化(i18n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java教程建议对属性文件使用首选项API. 建议使用属性文件和ResourceBundle处理应用程序中的内部化要求.

The Java tutorials recommend using the Preferences API over Properties files. Properties files and ResourceBundles are the recommended way to handle Internalization requirements in applications.

我正在考虑将两者都用于桌面应用程序,该应用程序将以特定于语言环境的方式显示首选项.

I am considering using both for a desktop application that will display preferences in a locale specific way.

有人能指出这种方法的问题吗?

Can anyone point out problems with this approach?

也许我应该只使用属性文件期间?

Maybe I should just use Properties files period?

推荐答案

我正在考虑将两者都用于桌面应用程序,该应用程序将以特定于语言环境的方式显示首选项.

I am considering using both for a desktop application that will display preferences in a locale specific way.

好的,所以您想要的是翻译为以下形式的配置文件:

OK, so what you want is translated configuration file in form of:

some_translated_key=some_value

好吧,除非您想在某个时候支持 MUI ,这应该没什么大不了的.但是,如果这样做,则同一台计算机上的不同用户可能会使用不同的语言,或者用户可能会切换语言,则将密钥与属性进行匹配将会遇到麻烦.阅读密钥时,您必须扫描所有翻译,并且一定会为同一密钥输入多个条目.怎么解决呢?好吧,这是一个好问题.

Well, unless you want to support MUI at some point it should not be a big deal. However, if you do, so that different users on the same computer could use different languages, or user might be able to switch language, you would have troubles in matching key to a property. You would have to scan all translations while reading the key, and you would surely end up with multiple entries for the same key. How to resolve that? Well, that's a good question.

根据我的经验,配置文件应该与语言无关(中性文化),并且绝不可以手工编辑(也就是说,翻译键并不重要).

From my experience, configuration files should be language-independent (neutral culture) and should never be edited by hand (that is translating keys doesn't really matter).

我认为字符编码可能有问题,但是以下代码段可以正常工作(文件采用UTF-8编码):

I thought there could be a problem with character encoding, but following code snippet works without an issue (files are UTF-8 encoded):

public class Main {
    private static final String FILE_NAME = "i18ned.properties";
    private File propertiesFile;
    private Properties properties;

    public Main() {
        properties = new Properties();
        propertiesFile = new File(FILE_NAME);
        if (propertiesFile.exists()) {
            try {
                properties.load(new BufferedReader(new FileReader(
                        propertiesFile)));
            } catch (FileNotFoundException e) {
                // not likely, but should be logged either way
            } catch (IOException e) {
                // logger should be used instead
                e.printStackTrace();
            }
        }
    }

    public void saveProperties() {
        try {
            properties
                    .store(new BufferedWriter(new FileWriter(propertiesFile)), "");
        } catch (IOException e) {
            // oops, use logger instead
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Main main = new Main();
        main.storeSome();
        main.readSome();
    }

    private void readSome() {
        String highAsciiKey = "żółć";
        String value = properties.getProperty(highAsciiKey);
        System.out.println(value);
    }

    private void storeSome() {
        String highAsciiKey = "żółć";
        String highAsciiValue = "łąkę";
        properties.setProperty(highAsciiKey, highAsciiValue);
        saveProperties();
    }

}

这篇关于Java首选项和国际化(i18n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆