Android learning-adaptive internationalization Language
[Preface] adaptive knowledge has nothing to do with programming. The key lies in the modification of configuration files. Adaptive content includes: language, screen, and platform. Let's talk about how to adapt to internationalization today. Internationalization (international) Abbreviation: i18n, because there are 18 characters between I and n, localization (localization), L10n for short. Generally, a language is expressed in the format of "_ Region". For example, zh_CN indicates Simplified Chinese. Operation Method: to support international languages, you only need to create a new folder named "values-Country Number" in the res/directory. For example, "values-zh-rCN" indicates Simplified Chinese, values-zh-rTW indicates Traditional Chinese and values-jp indicates Japanese. Note: The configuration options include the language code and region code. The configuration options for Chinese and Chinese are zh-rCN; the options for English and American are en-rUS. En and en indicate Chinese and English, CN and US indicate China and the United States, and r is required. For example, you can create an Android project Android_i18n. The default code in res/values/strings. xml is as follows: Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <string name = "app_name"> "Android_i18n" </string> 5 <string name = "hello_world"> Hello world! </String> 6 <string name = "action_settings"> Settings </string> 7 8 </resources>: copy the code without any modification and run the program directly on your phone, the default effect is as follows: Declaration: the default language of my mobile phone is set to Chinese: in fact, no matter what language the mobile phone is set to, the running effect is the same, because we have not set up internationalization. Let's start setting it now. Add a simplified Chinese Language internationalization: Create a folder under the res/directory to indicate the Simplified Chinese: values-zh-rCN. The directory structure is shown in. Note: if you want to add an International English Language, the new folder is values-en. Modify the res/values-zh-rCN/strings. xml Code, as follows: Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <string name = "app_name"> "android Internationalization" </string> 5 <string name = "hello_world"> hello, world </string> 6 <string name = "action_settings"> Settings </string> 7 8 </resources>: because the mobile phone is set to a Chinese language, the loaded string is the resource in the res/values-zh-rCN/directory. Success. Question 1: I have not added resources corresponding to traditional Chinese. If I set the language of my mobile phone to traditional Chinese, what will happen when I run the program? Answer: at this time, if the resource of Traditional Chinese is not defined, resources under the res/values/directory are loaded by default. Question 2: Now we define a string, which is defined only under res/values-zh-rCN/, rather than res/values/. What will happen to the running program? Answer: now we add a Button in activity_main.xml: 1 <Button 2 android: layout_width = "match_parent" 3 android: layout_height = "wrap_content" 4 android: text = "@ string/login_button"/> 04th line of code: the name displayed by the button references the string login_button. In this case, let res/values/strings. the xml Code remains unchanged: copy the Code <? Xml version = "1.0" encoding = "UTF-8"?> <Resources> <string name = "app_name"> "Android_i18n" </string> <string name = "hello_world"> Hello world! </String> <string name = "action_settings"> Settings </string> </resources> copy the code in res/values-zh-rCN/strings. add the declaration of a line of login_button in xml: (07th lines of code) Copy code 1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <resources> 3 4 <string name = "app_name"> "android Internationalization" </string> 5 <string name = "hello_world"> hello, world </string> 6 <string name = "action_settings"> Settings </string> 7 <string name = "login_button"> login button </string> 8 9 </resources> copy the Code: the activity_main.xml view is as follows: run the program. If the language of the mobile phone is set to English, the result is as follows: (abnormal display) if the language of the mobile phone is set to Chinese, the result is as follows: (normal display) In fact, internationalization is not difficult, but it will take a lot of effort to support languages in more countries.