It is a good practice to extract strings from the UI in the application code and store them in additional files. Android in each project through a resource directory makes this matter very simple.
If you created a project using the Android SDK tool, the tool creates a directory in the top-level directory of your project res/
. In this directory there are many subdirectories for storing multiple types of resources. There are also default files, for example res/values/strings.xml
, to hold strings.
Creating regional directories and string files
In order to support more languages, create additional directories under the res/directory values
, with the file names using values plus hyphenation symbols plus international standardized language codes. For example, the values-es/
directory contains a simple resource file for the area with the language code "ES". The Android system loads the appropriate resource files based on the locale settings set when the device is running. For more information, see providing alternative Resources.
Once you have decided to support this language, create the appropriate resource directory and string file for it. For example:
myproject/ res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xml
Adds a string value to the file for each zone.
At run time, theAndroid system loads the appropriate resource files based on the locale settings set when the device is running.
For example, the following are different resource files for different languages.
中文版 (default region), /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, /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, /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>
tip: You can use zone limits (or any configuration restrictions) for any type of resource, such as if you want to provide different bitmap drawable for different regions. For more information, see Localization.
Using string resources
You can <string>
name
reference the resource in the code or other XML file using the string name specified in the attribute of the element.
In your source code, you can use R.string.<string_name>
the syntax to reference a string resource.
For example:
//Get A string resource from your app ' sResources
StringHello= getResources()
.getString(R.string.Hello_world);//or supply a string resource to a method, that requires a stringTextViewTextView= New TextView( This);TextView.SetText(R.string.Hello_world);
In other XML files, you can use @string/<string_name>
syntax to reference string resources.
For example:
<textview Android:layout_width="Wrap_content" Android:layout_height="Wrap_content" Android:text="@string/hello_world" />