Reputation: 44571
I'm not sure if this is a silly question or not but I have an app that I need to make available to other languages. I have read a lot about this but I am confused on how to go about it. Do I actually need to translate the entire app into whichever languages I would like or will the system do that? I read the Android doc about using localization (Android docs) and many forum posts. I have read about changing the Configuration in the app but even changing the locale on my device doesn't work. Can anyone please point me in the right direction for this? I want the user to be able to change languages for the app itself as it may be passed around between people with different preferences. Thanks in advance!
Upvotes: 0
Views: 930
Reputation: 482
To add support for more locales, create additional directories inside res/. Each directory's name should adhere to the following format:
-b+[+] For example, values-b+es/ contains string resources for locales with the language code es. Similarly, mipmap-b+es+ES/ contains icons for locales with the es language code and the ES country code. Android loads the appropriate resources according to the locale settings of the device at runtime.
For example, the following are some different resource files for different languages: English strings (default locale), /values/strings.xml:
<resources>
<string name="hello">Hello</string>
</resources>
French strings (fr locale), /values-fr/strings.xml:
<resources>
<string name="hello">Bonjour</string>
</resources>
Use the Resources in your App:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
Upvotes: 0
Reputation: 30994
Basically you need to put strings that you want translated into a file
res/values/strings.xml
for the default en locale and then e.g. refer to such an entry in code via Resources.getString(R.string.theId)
or in other xml files via @string/theId
,
where theId
represents the id of the entry as e.g. in
<string name="theId">Username</string>
in the xml file.
Now to translate it you create additional
res/values-<lc>/strings.xml
files where stands for the respective 2 letter locale code.
Upvotes: 3