International English is internationalization, this is too long, so it is also called i18n (english Word internationalization the first character I and n,18 for the middle of the number of characters).
in addition to i18n and l10n (localization), g11n (globalization), and m17n (multilingualization), the difference is:
- I18N supports multiple languages, but the same time can only be in English and a selected language, such as English + chinese, English + german, English + Korean and so on;
- L10N (localization), supported in 2 languages, English and another language (E.G. chinese);
- g11n (globalization), A simple understanding can be considered g11n = i18n + l10n.
- m17n (multilingualization) can support multiple languages at the same time, for example, you can see chinese, english, german, and Korean on one Page.
Internationalization has become one of the essential elements of Java in order to make it possible for people in different countries to use software or websites adapted to their environment and language.
The internationalization mechanism in the software development process, makes the software and the specific language or the region decoupling. When the software we do is ported to other countries, it is not necessary to change the code of the software itself to adapt to local area use, so internationalization is a must.
So how do you internationalize in java?
After learning, I also learned some of the necessary introductory Knowledge.
first, we can get the default language and region of the native operating system by testing the Code.
Package Java_.util_;import Java.util.locale;import Java.util.concurrent.synchronousqueue;public class Locale_ { public static void main (string[] Args) {locale locA = new locale ("zh"); Locale LOCB = new locale ("zh", "CN"); Locale LOCC = new locale ("zh", "CN", "WIN"); Locale LOCD = locale.china; System.out.println (loca.getlanguage ()); System.out.println (locb.getlanguage ()); System.out.println (locb.getcountry ()); System.out.println (locc.getlanguage ()); System.out.println (locc.getcountry ()); System.out.println (locc.getvariant ()); Locale LoCE = (locale) Loca.clone (); System.out.println (loca.equals (loce)); System.out.println (loca.equals (locb)); locale[] locales = Locale.getavailablelocales (); System.out.println (locales.length);//for (Locale loc:locales) {//System.out.println (loc.tostring ());// System.out.println ("----");/} Locale LOCF = Locale.getdefault (); System.out.println (locf.tostring ()); System.out.println (locf.getlanguage ()); System.out.println (locf.getcountry ()); }}
We need to transfer hard-coded text to an external resource file, creating two internationalized resource Files:
Name = base name +locale
1. Set the resource File.
Messgesbundle_en_us.properties:
k1=hello,{0}k2=morning,{0}
Messgesbundle_zh_cn.properties:
k1=\u4f60\u597d\uff0c{0}k2=\u65e9\u5b89\uff0c{0}
2, use Resourcebundle.getbundle (baseName, locale) to find the messgesbundle_en_us.properties file.
packagejava_.util_;Importjava.util.Locale;Importjava.util.ResourceBundle; public classResourcebundle_ { public Static voidmain (string[] Args) {Locale localeen=NewLocale ("en", "US"); ResourceBundle Resen= Resourcebundle.getbundle ("messagesbundle", localeen); System.out.println ("k1 =" + resen.getstring ("k1")); System.out.println ("k2 =" + resen.getstring ("k2")); Locale LOCALECN=Locale.getdefault (); ResourceBundle RESCN= Resourcebundle.getbundle ("messagesbundle", localecn); System.out.println ("k1 =" + rescn.getstring ("k1")); System.out.println ("k2 =" + rescn.getstring ("k2")); }}
3, the use of Placeholders.
Set current locale
Using Class Messageformat
packagejava_.text_;Importjava.text.MessageFormat;Importjava.util.Locale;Importjava.util.ResourceBundle; public classMessageformat_ { public Static voidmain (String [] Args) {Locale Loc=Locale.getdefault (); ResourceBundle Resb= Resourcebundle.getbundle ("messagesbundle", loc); Messageformat Msgformat=NewMessageformat (resb.getstring ("k1")); System.out.println (msgformat.format (Newobject[]{"lime"})); }}
4. Set the default internationalized resource File.
The two locales we set up above already have resource files, but what if we randomly set a locale, and if there is no resource file for it? Then you need to set a default Internationalized resource File. Just add a resource file named messgesbundle.properties, and the corresponding settings in it will be ok.
Note the Point:
- The default locale is determined by the operating System.
- Locale is composed of language and country Code.
- Internationalized resource files are composed of basename+locale, such as: messgesbundle_en_us.properties
- The default internationalized resource file is by Basename. Properties named, such as:messgesbundle.properties
- If the resource file is placed in the package, then the basename will be added to the package name, or not found.
Java Support for internationalization