Android official Getting Started document [9] support for different languages
Supporting Different Languages
Support for different languages
This class teaches
1.Create Locale directories and String Files
2.Use the String Resources
You should also read
? Localization Checklist
? Localization with Resources
This course teaches you
1. Create a string of locale directories and files
2. Using String resources
You should also read
? Localization Checklist
? localization with resources
It's always a good practice to extract UI strings from your app code and keep them in an external file. Android makes this easy with a resources directory on each Android project.
It's always a good practice to extract the UI string from your application code and save it in an external file. Android makes this easy and a resource directory in each Android project.
If you created your project using the "Android SDK Tools" (read Creating an Android project), the tools create a Res/direct Ory in the top level of the project. Within this res/directory is subdirectories for various resource types. There is also a few default files such as Res/values/strings.xml, which holds your string values.
If you use the Android SDK tool to create a project (read create Android project), the tool creates a res/directory for the top level of the project. Subdirectories in this res/directory are various resource types. There are also some default files, such as Res/values/strings.xml, which contain your string values.
Create Locale directories and String Files
Create a string of locale directories and files
--------------------------------------------------------------------------------
To add languages, create additional values directories inside Res/that include a hyphen and the ISO Lang Uage code at the end of the directory name. For example, values-es/is the directory containing simple resourcess for the Locales with the language code "ES". Android loads the appropriate resources according to the locale settings of the device at run time. For more information, see providing alternative Resources.
In order to support more languages, create an internal res/, which includes a hyphen, and the ISO language code added value directory at the end of the directory name. For example, the value-es/is the directory that contains the language code "ES" for the locale of the simple resourcess. The robot loads the appropriate resources according to the locale settings of the device at runtime. For more information, see providing alternative resources.
Once you ' ve decided on the languages you'll support, create the resource subdirectories and string resource files. For example:
Once you decide which language you will support, create the resource subdirectory and the string resource file. For example:
myproject/
res/
values/
Strings.xml
values-es/
Strings.xml
values-fr/
Strings.xml
Add the string values for each locale into the appropriate file.
Adds a string value to each locale to the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user ' s device.
At run time, the Android system uses the appropriate settings based on the locale string resource that is currently set for the user's device.
For example, the following is some different string resource files for different languages.
For example, the following are some of the different string resource files for different languages.
中文版 (default locale),/values/strings.xml:
English (default locale),/values/strings.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "title" >my application</string>
<string name= "Hello_world" >hello world!</string>
</resources>
Spanish Spanish,/values-es/strings.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "title" >mi aplicación</string>
<string name= "Hello_world" >hola mundo!</string>
</resources>
French French,/values-fr/strings.xml:
<?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<string name= "title" >mon application</string>
<string name= "Hello_world" >bonjour le monde!</string>
</resources>
Note:you can use the locale qualifier (or any of the configuration Qualifer) on any resource type, such as if you want to provid E Localized versions of your bitmap drawable. For more information, see Localization.
Note: You can use the locale qualifier in any resource type (or any configuration qualifier) if you want to draw a localized version for your bitmap, such as. For more information, see Localization.
Use the String Resources
Using the string resource
--------------------------------------------------------------------------------
You can reference your string resources in your source code and other XML files using the resource name defined by the < ;string> element ' s name attribute.
You can reference the source code of your string resource and use a different XML file with the resource name defined by the < string > element's Name property.
In your source code, you can refer-a string resource with the syntax r.string.<string_name>. There is a variety of methods that the accept a string resource this.
In your source code, you can refer to using syntax r.string string resources. <string_name>. There are a variety of ways to accept string resources.
For example:
For example:
Get a string resource from your app ' s Resources
String Hello = getresources (). getString (R.string.hello_world);
Or supply a string resource to a method, that requires a string
TextView TextView = new TextView (this);
Textview.settext (R.string.hello_world);
In other XML files, you can refer to a string resource with the syntax @string/<string_name> whenever the XML attrib Ute accepts a string value.
In other XML files, you can refer to the syntax of a string resource @string/<string_name> whenever an XML attribute accepts a string value.
For example:
For example:
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "@string/hello_world"/>
Next:supporting Different Screens
Next: Support for different screens
This article is translated from: https://developer.android.com/training/basics/supporting-devices/languages.html
Android official Getting Started document [9] support for different languages