0

I have such a question: I have an Login/Registration Activity which is by default is using Russian version of strings.xml. So when user is entering application - he sees text in Russian language. But on that activity there is a button to choose another language. When he clicks that button - I open another activity in which he can choose which language to use (English/Spanish/German/etc). When he chooses a language (let's say German). How can I from this point in time, show to the user text which is now should be used from German version of strings.xml? And also - how can I do such a thing: if user's locale is from Russian, Ukraine, Georgia --- then use Russian version of strings.xml, and if user's locale from any other country - user English version of strings.xml? Thanks.

yozhik
  • 4,644
  • 14
  • 65
  • 98
  • 1
    Possible duplicate of [How can I change language of whole application by only single click?](https://stackoverflow.com/questions/43292013/how-can-i-change-language-of-whole-application-by-only-single-click) – Markus Kauppinen Aug 14 '18 at 10:54
  • 1
    Possible duplicate of [Change app language programmatically in Android](https://stackoverflow.com/questions/2900023/change-app-language-programmatically-in-android) – Saurabh Bhandari Aug 14 '18 at 11:00

2 Answers2

2

I would strongly recommend using lokalise

We have an app with 6 flavours and more than 16 languages. It would be a nightmare, if only this library didn't exist.

They have a tutorial on how to use custom locale: but in short:

    // Create a new Locale object
    Locale locale = new Locale("ru");
    Locale.setDefault(locale);
    // Create a new configuration object
    Configuration config = new Configuration();
    // Set the locale of the new configuration
    config.locale = locale;
    // Update the configuration of the Accplication context
    getResources().updateConfiguration(
        config, 
        getResources().getDisplayMetrics()
    );
jujka
  • 1,190
  • 13
  • 18
  • Thanks, can you please give a link to your application in playmarket, to try to test it on different locales? – yozhik Aug 14 '18 at 11:29
  • 1
    @yozhik Unfortunately, I can not: we don't distribute it through GooglePlay, as it's a bookmaking company, which are prohibited by Google policies. You may take a look here: https://www.fonbet.ru/get/?android (will start downloading automatically) – jujka Aug 14 '18 at 11:32
  • Georgy, I have one more question: is this workaround with setting of user locale will work outside Application class (I need to use it in Activity). I mean, if I will switch to another locale in current activity and will call: getResources().updateConfiguration(). Will it apply changes immediately? Or I need to recreate only this Activity or I need to kill application and start it again, while getting locale previously saved to Shared preferances? – yozhik Aug 14 '18 at 12:15
  • 1
    @yozhik Yes and no. It will change immediately, however you'll have to reload current screen or navigate back / to another screen for those changes to be applied. There can be few workarounds about this limitation, e.g. showing a fully transparent fragment and closing it immediately, for app to be displayed correctly – jujka Aug 14 '18 at 12:29
1

What you want to do is:

  1. Have a list of language/locale wise content translations in the format of key-value pairs. You can achieve this within the same file or have separate files and name them according to the language-locale combo. I prefer the latter version, easier to maintain.

  2. Next at time of app init, you want to read the phone's current locale with

    Locale current = getResources().getConfiguration().locale;
    

    Map this value to the content files (varies from framework to framework) created above and you have instant localization.

  • So, I need to create strings-RU.xml, strings-UA.xml, strings-GE.xml(which will have Russian text) and strings.xml which will have english text, and strings-SP.xml which will have Spain text? Right? But I didn't understand the part how to get strings resources then based on current Locale. Can you please provide code and where do I need to write it regarding my LoginRegistration activity, which starts right after Splash activity? – yozhik Aug 14 '18 at 11:27
  • You may check the locale in the splash activity itself. – Vikrant Siwach Aug 18 '18 at 05:56