It is a good practice to store strings in the UI in an external file, which is extracted by code. Android can easily implement this feature through the resource catalog in the project.
If you are creating a project using Android SDK Tools (see Creating Android Projects (Creating an Android project)), a directory will be created at the root of the project res/
, and the directory contains subdirectories of all resource types. It contains the project's default file res/values/strings.xml
, for example, to hold a string value.
Creating locale directories and string files
To support multiple languages, res/
create an additional directory in the name of the values
hyphen and ISO country code, such as a values-es/
directory of simple resource files for locales where the language code is "es". Android loads the appropriate resources at run time, depending on the locale of the device. See providing alternative Resources.
If you decide to support a language, you need to create a resource subdirectory and a string resource file, for example:
MyProject/ res/ values/ strings.xml values-es/ strings.xml values-fr/ strings.xml
Adds a string value from a different region language to the appropriate file.
The Android system runtime uses the appropriate string resource based on the current locale of the user device.
For example, here are a few different languages that correspond to different string resource files.
English (default region language), /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 language, /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>
Note: You can use a zone modifier (or any configuration modifier) in any resource type, such as a localized version for bitmap, for more information see localization.
Using Character resources
We can refer to our own string resources in source code and other XML files through <string>
the attributes of the elements name
.
A string resource can be referenced in the source code through R.string.<string_name>
syntax, and many methods can accept the string in this way.
For example:
// Get a string resource from your app‘s ResourcesString hello = getResources().getString(R.string.hello_world);// Or supply a string resource to a method that requires a stringnew TextView(this);textView.setText(R.string.hello_world);
In other XML files, you can use @string/<string_name>
syntax to reference string resources whenever an XML attribute is to accept a string value.
For example:
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
1.3.1 adapted to different languages