Localization with resources in Android
Adding translation to your Android app
You have to create a different
strings.xml file for every new language.
- Right-click on the res folder
- Choose New → Values resource file
- Select a locale from the available qualifiers
- Click on the Next button (>>)
- Select a language
- Name the file strings.xml
Setting the language programmatically:
The function above will change the text fields which are referenced from strings.xml. For example, assume that you have the following two text views:
Then, after changing the locale, the language strings having the ids
hello will be changed accordingly.
Change locale of android application programatically
In above examples you understand how to localize resources of application. Following example explain how to change the application locale within application, not from device. In order to change Application locale only, you can use below locale util.
Initialize locale that user preferred, from Application class.
You also need to create a base activity and extend this activity to all other activity so that you can change locale of application only one place as follows :
Note : Always initialize locale in constructor.
Now you can use LocalizationActivity as follow.
Note: When you change locale of application programmatically, need to restart your activity to take the effect of locale change In order to work properly for this solution you and use locale from shared preferences on app startup you
android:name=".LocaleApp"in you Manifest.xml.
Sometimes Lint checker prompt to create the release build. To solve such issue follow below options.
If you want to disable translation for some strings only then add following attribute to default string.xml
Ignore all missing translation from resource file add following attribute It's the ignore attribute of the tools namespace in your strings file, as follows:
Another way to disable non-translatable string
If you have a lot of resources that should not be translated, you can place them in a file named donottranslate.xml and lint will consider all of them non-translatable resources.
You can also add locale in resource file
You can also disable missing translation check for lint from app/build.gradle
Configuration types and qualifier names for each folder under the "res" directory
Each resource directory under the
res folder (listed in the example above) can have different variations of the contained resources in similarly named directory suffixed with different
qualifier-values for each
Example of variations of `` directory with different qualifier values suffixed which are often seen in our android projects:
Exhaustive list of all different configuration types and their qualifier values for android resources:
|MCC and MNC||Examples:|
|Language and region||Examples:|
|Screen pixel density (dpi)||ldpi|
|Primary text input method||nokeys|
|Navigation key availability||navexposed|
|Primary non-touch navigation method||nonav|
|Platform Version (API level)||Examples:|
Type of resource directories under the "res" folder
When localizing different types of resources are required, each of which has its own home in the android project structure.
Following are the different directories that we can place under the
\res directory. The resource types placed in each of these directories are explained in the table below:
|animator/||XML files that define property animations.|
|anim/||XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)|
|color/||XML files that define a state list of colors. See Color State List Resource|
|drawable/||"Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes: : |
|mipmap/||Drawable files for different launcher icon densities. For more information on managing launcher icons with mipmap/ folders, see Managing Projects Overview.|
|layout/||XML files that define a user interface layout. See Layout Resource.|
|menu/||XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource.|
|raw/||Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.|
|However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead ofres/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.|
|values/||XML files that contain simple values, such as strings, integers, and colors, as well as styles and themes|
|xml/||Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.|