13

I am aware of Creating a new values directory for the language with the suffix of the language code. For german: values-de or french: values-fr then copy our string.xml into that and translate each entry. And this works based on the Phone Localization settings

I wanted to know if we can bypass the phone setting and and make the user select his required language inside the app?

My requirement is, i want to give a language selection option inside my app, and make the user select the language he wants for the app.. how to dynamically switch between the string.xml (for different languages) ???

thanks in advance

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
amithgc
  • 6,167
  • 6
  • 29
  • 38
  • You can use the following library, which provides the language list, the preference for your settings screen, and overrides the language in your application: https://github.com/delight-im/Android-Languages – caw Mar 24 '14 at 01:57

4 Answers4

10

Create method which sets your basic Locale.Lets say

public static void setDefaultLocale(Context context,String locale) {
        Locale locJa = new Locale(locale);
        Locale.setDefault(locJa);

        Configuration config = new Configuration();
        config.locale = locJa;

        context.getResources().updateConfiguration(config, context.getResources()
                .getDisplayMetrics());

        locJa = null;
        config = null;
    }

Now check when user selected Locale.(Here basically I have used menu for language selection).

Configuration config = new Configuration();
String newLocale = config.locale.getLanguage().substring(0, 2)
    .toLowerCase();
if ("ja".equalsIgnoreCase(newLocale)) {
// Call above method with context & newLocale
} 
// Sequentially you check for Locale & change that.
Shashank_Itmaster
  • 2,465
  • 5
  • 30
  • 56
  • Any reason why you set locJa and config to null at the end of setDefaultLocale? They're about to go out of scope anyway... – Bob Whiteman Feb 10 '11 at 23:59
  • No there no reason behind setting them null. The code of method posted here is directly taken from my application so I am making them as there are not used further in my application. – Shashank_Itmaster Feb 11 '11 at 03:18
5

Check out this post... It is the same thing basically.

Changing Locale within the app itself

Locale appLoc = new Locale("en");
Locale.setDefault(appLoc);
Configuration appConfig = new Configuration();
appConfig.locale = appLoc;
getBaseContext().getResources().updateConfiguration(appConfig,
    getBaseContext().getResources().getDisplayMetrics());
Community
  • 1
  • 1
Anthony Graglia
  • 5,355
  • 5
  • 46
  • 75
1

If you want to get images according their respective languages,You should create layout folder below like this.First i take the example for custom localization.

Locale appLoc = new Locale("xx");
Locale.setDefault(appLoc);
Configuration appConfig = new Configuration();
appConfig.locale = appLoc;
getBaseContext().getResources().updateConfiguration(appConfig,
    getBaseContext().getResources().getDisplayMetrics()); 

Your layout folder should be layout-xx and your drawable folder also should be drawable-xx.But one thing that when you change the language,you have to refresh the layout.I used in my app, take a button and set the background image.But sometimes images are not changed so i have done like this .

btn.setBackgroundDrawable(null);
btn.setBackgroundResource(R.drwable.yourimage);
Ramesh Akula
  • 5,720
  • 4
  • 43
  • 67
0

It is very easy just follow this link

 languageToLoad = "hi"; // your language
        locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());
        this.setContentView(R.layout.activity_main);

- See more at: http://www.theappguruz.com/blog/multi-language-support-to-android-app#sthash.eGmzq57K.dpuf

Yogesh Rathi
  • 6,331
  • 4
  • 51
  • 81