3

在重构我的一个项目时,我将所有语言 ISO 代码(如"en""de"字符串)替换为Locale类及其常量Locale.ENGLISHLocale.GERMAN以使其更节省重构并最大限度地减少错误源。然后我使用 locale.getLanguage() 将 ISO 代码作为字符串获取。

我用这种方法遇到的问题是国家和变量字段形式的语言环境类的开销。我正在考虑编写自己的语言类来避免这种开销。

使用自定义类是一种好习惯,还是已经有一个Language我错过的专用类?

4

2 回答 2

10

You missed one of the basic priciples of programming: Don't reinvent the wheel.

Yes, the Locale class can do more than you need, but the overhead is usually extremely negligible. Also, using it enables other coders to instantly understand that part of your code.

于 2012-09-04T08:55:39.183 回答
2

开销并没有那么高。这得到了每个语言环境。

long start = System.currentTimeMillis();
for (Locale l : Locale.getAvailableLocales())
    l.toString();
long time = System.currentTimeMillis() - start;
System.out.println(time + " ms.");

印刷

15 ms.
于 2012-09-04T08:57:43.113 回答