Objective
Adaptive knowledge is not related to programming, the key is the modification of the configuration file. The adaptive content includes: language, screen, platform. Today, let's talk about how to adapt to international speech.
Internationalization (internationalization) abbreviation:i18n, because there are 18 characters between I and N, localization (localization), referred to as l10n.
Language is generally used in the form of a language, such as: ZH_CN to express Simplified Chinese.
Operation Method:
In order to support the internationalized language, we just have to re-create the folder in the res/ directory as:values-Country number , as values-zh-rcn indicates Simplified Chinese , values-zh-rtw means traditional, values-jp is Japanese, etc.
Note: The configuration options include the language code and the region code . The configuration options for Chinese and Chinese are zh-rcn; The configuration options for English and us are en-rus. En and en are Chinese and English; CN and US represent China and the United States; The preceding r is necessary .
Example: Support for Chinese Simplified internationalization
The operation is as follows:
Now create a new Android project android_i18n, the default in Res/values/strings.xml code is as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4 <stringname= "App_name">"Android_i18n"</string>5 <stringname= "Hello_world">Hello world!</string>6 <stringname= "Action_settings">Settings</string>7 8 </Resources>
Do not make any changes, run the program directly on the phone, the default effect is as follows:
Disclaimer: The default language of my phone is set to Chinese:
In fact, no matter what language the phone is set to run, the effect is the same, because we have not yet internationalized settings. Now let's start setting it up.
Now add a simplified Chinese internationalization: Create a new folder in the res/directory for Simplified Chinese:values-zh-rcn, the directory structure is as follows:
Note: If you want to add an English internationalization, the new folder is Values-en .
Modify the code for the Res/values-zh-rcn/strings.xml as follows:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4 <stringname= "App_name">"Android Internationalization"</string>5 <stringname= "Hello_world">Hello, world</string>6 <stringname= "Action_settings">Set up</string>7 8 </Resources>
Running the program at this point, the effect is as follows:
Because the phone is set to the Chinese language, the string that is loaded at this time is a resource under the res/values-zh-rcn/directory.
Done.
Question one:
Now I do not add the corresponding resources of traditional Chinese, if I set the language of the phone to traditional Chinese, run the program will be what phenomenon?
Answer: At this point, if there is no traditional Chinese resources defined, then the default load is the resources under the res/values/directory .
Question two:
Now define a string of characters , only defined under res/values-zh-rcn/, not the res/values/definition , what is the running program?
Answer:
Now we add a button to the Activity_main.xml:
1 < 2 android:layout_width= "Match_parent"3 android: Layout_height= "Wrap_content"4 android:text= "@string/login_ Button "/>
The No. 04 line of code: The button displays a name that references the string Login_button
At this point, let the Res/values/strings.xml code remain the same:
<?XML version= "1.0" encoding= "Utf-8"?><Resources> <stringname= "App_name">"Android_i18n"</string> <stringname= "Hello_world">Hello world!</string> <stringname= "Action_settings">Settings</string></Resources>
Add a line Login_button declaration in Res/values-zh-rcn/strings.xml: (No. 07 line of code)
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Resources>3 4 <stringname= "App_name">"Android Internationalization"</string>5 <stringname= "Hello_world">Hello, world</string>6 <stringname= "Action_settings">Set up</string> 7<string name= "Login_button"> login button </ string > 8 9 </Resources>
Effect:
The Activity_main.xml view is as follows:
Run the program, if the phone's language is set to English, the effect is as follows: (Abnormal display)
If the language of the phone is set to Chinese, the effect is as follows: (can display normally)
In fact, internationalization is not difficult, but if you want to support the language of more countries, the workload will be very large.
Finally, attach something for your usual reference:
language abbreviation code for Country _ Region:(Query alternate)
Attach common links to international resources:
National language abbreviation: http://www.loc.gov/standards/iso639-2/php/code_list.php
Country and region abbreviation: http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html
Country _ Regional Language quick check table: http://www.cnblogs.com/Mien/archive/2008/08/22/1273950.html
Android Learning----Adaptive International language