Is it possible to load localization resources at runtime?

I have an application with Internet access and do not want to store many string.xml files for different languages.

What I want:

  • The application contains only one string.xml with English strings.
  • When the user launches it, I see the user language of the phone and set my own server for the necessary language file. (resource keys will be the same)
  • When a new resource file appears, the entire interface should work with the new file.

The question is how to modify an existing one or add a new string.xml file at runtime?

+6
source share
3 answers

Obviously you cannot change, load or delete strings.xml at run time

If you want to store locations, you will need to use SQLite storage to store translations.

similarly:
How to change strings.xml file at runtime
changing runtime strings.xml

+2
source

Now I see only the solution:

  • Create a "localization proxy" that will return the necessary resource to me (from string.xml or the loaded resource)
  • Replace all getString() and getText() with your own getStringFromLocalization method
  • Override TextView , Button and some other custom views and change the init and setText methods there.
+2
source

Overriding the standard resource / language using resource files that match the time and then your scarification performance compared to tuning. Do this only if you need it.

0
source

Source: https://habr.com/ru/post/908915/


All Articles