Sometimes we do not just publish the game in the domestic market, sometimes need to publish in the international market, at this time we need at least 2 sets of menus for different players to use, after all, Chinese is not everyone can read.
LIBGDX provides I18nbundle for us to use,I18nbundle provides a set of mechanisms that allow us to load resources from the properties file, where the resources in the properties file are "name" and " Values ", and the resources preferably have a parent file, which prevents the resource from being lost when the value in the parent file can be used:
Through the above image can be seen string.properties is the parent file, the other 2 corresponding to the Chinese file and English files
1. 由于默认的BitmapFont不支持中文,因此必须自己重新创建一个.fnt文件,文件中必须包含Properties中的所有汉字,具体看前面博客。 2. 在用Eclipse创建properties文件时默认的编码不是**UTF-8**,这时显示的不是中文,切换编码到**UTF-8**就OK了
I18nbundle Common Method Description:
1. Static I18nbundle Createbundle (FileHandle basefilehandle, Java.util.Locale Locale) Create I18nbundle objects based on properties file and country code
2. java.lang.String format (java.lang.String key, java.lang.Object ... args) formatting gets the values
3. Java.lang.String get (java.lang.String key) to get values based on key
Here's a look at the code, which is also described in the code:
Public class localizationtest extends applicationadapter {SpriteBatch SB; Vector3 Touch; Orthographiccamera Cam;PrivateBitmapfont font;PrivateLanguagemanager LM;PrivateFileHandle Enfilehandle, Cnfilehandle;PrivateString GameTitle, Gamebegin, gamehelp;@Override Public void Create() {SB =NewSpriteBatch (); Touch =NewVector3 (); Cam =NewOrthographiccamera (640,480); Cam.position.set ( the, -,0); Cam.update ();/** * The default font does not support Chinese, you need to use Hiero to create font files * *Font =NewBitmapfont (Gdx.files.internal ("Font/in.fnt")); LM =NewLanguagemanager ();//Initialize language manager, load languageEnfilehandle = Gdx.files.internal ("STRINGS_EN_GB"); Cnfilehandle = Gdx.files.internal ("STRINGS_ZH_CN"); Lm.loadlanguage ("中文版", Enfilehandle, locale.uk);//Load text fileLm.loadlanguage ("Chinese", Cnfilehandle, Locale.china);if(Lm.getcurrentlanguage (). Equals ("ZH_CN") {Lm.setcurrentlanguage ("Chinese"); }Else{Lm.setcurrentlanguage ("中文版"); } initializetext (); }Private void Initializetext() {I18nbundle b = lm.getcurrentbundle (); GameTitle = B.get ("GameTitle");//According to key to get the corresponding valuesGamebegin = B.format ("Gamebegin",Bayi);///based on key to obtain the corresponding values, then format the languageGamehelp = B.get ("Gamehelp"); }Private void Translate() {if(Lm.getcurrentlanguage (). Equals ("Chinese")) Lm.setcurrentlanguage ("中文版");ElseLm.setcurrentlanguage ("Chinese"); Initializetext (); }@Override Public void Render() {Gdx.gl.glClearColor (0.39F0.58F0.92F1.0f); Gdx.gl.glClear (Gl20.gl_color_buffer_bit); Sb.setprojectionmatrix (cam.combined); Sb.begin (); Font.draw (SB, GameTitle, -,260); Font.draw (SB, Gamebegin, -, the); Font.draw (SB, Gamehelp, -, -); Font.draw (SB,"Touch up Translate", the, -); Sb.end ();if(Gdx.input.justTouched ()) {Touch.set (Gdx.input.getX (), Gdx.input.getY (),0); Cam.unproject (Touch);if( the< touch.x && touch.x<640&&//Click the first quadrant to toggle the language -< TOUCH.Y && touch.y<480) {translate (); } } }@Override Public void Dispose() {sb.dispose (); Font.dispose (); } Public class languagemanager { PrivateObjectmap<string, I18nbundle> languages;PrivateString Currentlanguage; Public Languagemanager() {languages =NewObjectmap<string, i18nbundle> ();/* The default language format obtained here is not uniform with the later saved language format, and will be used for a conversion * or the language and I18nbundle objects are not stored in the Objectmap, with other data structures, this is not considered in this case * */Currentlanguage = Locale.getdefault (). toString (); } Public void loadlanguages(String name, I18nbundle bundle) {if(Name! =NULL&&!name.isempty () && Bundle! =NULL) Languages.put (Name.tolowercase (), bundle); }/** * Store the language and its corresponding I18nbundle object for easy access later * @param Name language * @param filehandle p Roperties File Object * @param Locale Country code */ Public void Loadlanguage(String name, filehandle filehandle, locale locale) {if(Name! =NULL&&!name.isempty () && filehandle! =NULL&& Locale! =NULL) Languages.put (Name.tolowercase (), I18nbundle.createbundle (FileHandle, Locale)); } Public void Loadlanguage(String name, FileHandle filehandle) {if(Name! =NULL&&!name.isempty () && filehandle! =NULL) Languages.put (Name.tolowercase (), I18nbundle.createbundle (FileHandle)); } Public void Removelanguage(String name, I18nbundle bundle) {if(Name! =NULL&&!name.isempty () && Bundle! =NULL) Languages.remove (Name.tolowercase ()); } Public void Setcurrentlanguage(String name) {if(Languages.containskey (Name.tolowercase ())) Currentlanguage = name; } PublicStringGetcurrentlanguage() {System.out.println ("Currentlanguage:"+ currentlanguage);returnCurrentlanguage; } PublicI18nbundleGetcurrentbundle() {returnLanguages.get (Currentlanguage); } }}
Libgdx the internationalization of Chinese and English menu switching