Looking for to add different languages into your application or switching between languages dynamically?
Here is a tutorial to show how to answer to these questions trough an helper what I wrote in Java, and just need the locale code of the language what you want to use in your application.
First of all, you will be to add a value directory (in res/) related to every language what you want to add into your application.
In order to create a resource directory associated to a specified language, create a new Android resource directory, select “Language” and click on “>>” button.
You should arrive on this view.
Select a language and it will create automatically a directory under the format “values-fr” if you select the french one.
Here is the result after creating a directory for French and Finnish.
Add a string.xml file in every of the directory what you create with the translation into the language associated.
If you wish, you can also add a dimens.xml or a styles.xml in order to custom the style of your application.
Now that you have all the directories with all the files translated, here is the code to change the local of your device and load automatically and programmatically the translated files.
You can use this method in that way:
Assuming that we are calling this method inside of an activity (to be able to use this) and get access to the resources of your project.
Giving “fr” in parameter will change the local of the device and load automatically the files into your values-fr directory.
Some locales are already defined in the Locale object, like English, French, Chinese, Italian and the locale can be initialised just in calling: Locale.an for example.
I invite you to read a little bit more about Locale on android dev documentation.
If the language what you want to use is not in the list, no panic you can create a new Locale object. It takes in parameter the country code (2 letters).
If you have any files into the assets directory, html or json files for example. Do not forget to load them as well.
Changing the locale does not load them automatically.
Find this example on Github here.