使用eclipse开发国际化程序

2021/11/21 11:09:44

本文主要是介绍使用eclipse开发国际化程序,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.1 什么是i18n

以下内容引自wikipedia Internationalization_and_localization

The terms are frequently abbreviated to the numeronyms i18n (where 18 stands for the number of letters between the first i and the last n in the word internationalization, a usage coined at Digital Equipment Corporation in the 1970s or 1980s) and L10n for localization, due to the length of the words. Some writers have the latter acronym capitalized to help distinguish the two.
简单来说,i18n是Internationalization的缩写,18代表单词中间的18个字母。

1.2 什么是Locale

一个Locale对象代表了一个特定的地域、政府、或文化地区,它们使用的文字/语言是有别于其他Locale的。
Locale对象有三个主要组件:language、country、variant。
variant代表特定制造商或特定浏览器代码,例如WIN代表Windows,MAC代表Macintosh,POSIX代表POSIX,可以用下划线将两个variant隔开,重要的放前面。
完整的language、country、variant可以在language-subtag-registry中找到,
找language: search for "Type: language"
找country: search for "Type: region"
找variant: search for "Type: variant"

Locale有三个构造器:

Locale(String language)
// Construct a locale from a language code.
Locale(String language, String country)
// Construct a locale from language and country.
Locale(String language, String country, String variant)
// Construct a locale from language, country and variant.

例如要构造一个加拿大使用的英语的Locale对象,可以这样写:
Locale lcoale = new Locale("en", "CA");
此外,Locale类提供了静态final域,他们返回特定国家或者语言的地区,因此可以通过调用它的静态方法来构造Local对象:
Locale lcoale = Locale.CANADA_FRENCH;
完整的静态final域可以在Java的API中找到。

另外,静态getDefault方法返回用户计算机所在地区:
Locale lcoale = Locale.getDefault();

1.3 properties文件

我们在开发程序时,一般都会将文本元素放在单独的文件中,
不同语言不同文件,
然后使用ResourceBundle来读取属性文件,
文件的命名方式一般为:basename_languageCode_countryCode,
basename可以自己写,后面的格式时固定的。
比如basename为MyResource,则可以有以下文件:



这篇关于使用eclipse开发国际化程序的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程