3

First of all sorry for my weak english.

I was searching for how to change language of my android application. I found in lots of link saying that creating values-fr (for french), values-en (for english) etc and copying strings.xml file into those folders. And replace string names inside strings.xml according to language. Finally adding some code blocks which say that which "values folder" will be used (see code block below), I can achieve language support.

String languageToLoad  = "en"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());

But I was wondering that is it possible to create "values-any" file in runtime and after creating, is it possible to put strings.xml file inside that folder which is filled before and assume that the strings.xml file coming from some database or webservice.

Briefly is it possible to achieve language support dynamically? Any idea will be greatly appreciated. Thanks in advance.

Hilal
  • 902
  • 2
  • 22
  • 47

1 Answers1

4

I was wondering that is it possible to create "values-any" file in runtime

No. Resources are read-only at runtime.

is it possible to achieve language support dynamically?

Not through the Android resource system.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks, so this is also not possible that change strings.xml file at runtime, is it ? – Hilal Apr 14 '16 at 13:15
  • @Hilal that is correct, you cannot change the values at runtime. – Marko Apr 14 '16 at 13:17
  • 1
    @Hilal: Correct. Resources are read-only at runtime. You are welcome to store your own translations in your own files/databases/whatever, and load those strings yourself. However, at that point, you are outside the Android resource system. BTW, you do not need the code block in your question to achieve "language support" for resources. Android automatically uses the user's chosen device locale. That code block is if you wish to *override* the user's chosen device locale, and that code may require some adjustment on Android N. – CommonsWare Apr 14 '16 at 13:17
  • Thanks @Marko, so I will ask another question for only dynamic language support in android. – Hilal Apr 14 '16 at 13:21
  • @CommonsWare thanks, you said that language support is not possible through the Android resource system. May you suggest another way? – Hilal Apr 14 '16 at 13:24
  • @Hilal: I did, in my previous comment. "You are welcome to store your own translations in your own files/databases/whatever, and load those strings yourself." – CommonsWare Apr 14 '16 at 13:26
  • @CommonsWare sorry thanks, I missed that. So assume that I stored my translations but isn't it so difficult to load those strings by myself for each component one by one? – Hilal Apr 14 '16 at 13:32
  • @Hilal: It will require you to write some utility code that replaces things like `getString()` in your app. Also, please bear in mind that **other apps** can only use your string resources. So things like your launcher icon caption will only come from string resources (and only for the device locale, not something that you override in your process). This is why few developers go down this path. – CommonsWare Apr 14 '16 at 13:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/109164/discussion-between-hilal-and-commonsware). – Hilal Apr 14 '16 at 13:47