0

I was surprised to find that there is pretty much no mention of this anywhere.

Here's the problem: I have a tutorials app and majority of the "1 stars" I get are due to complains that my app is not in their local language. Since I can't possibly learn a dozen languages, I've decided the least I can do is use google translate and make more resources.

Since the translation won't be perfect, I want to give the users the option to switch back to the default (english) version.

How I expect to go about doing this:

Say I have the following folders:

values, value -fr, values-ja, values-sp

I want to simply put a spinned in my settings activity and let the user choose the language. So, I'll probably get a SharedPref variable, store it permanently and make it easily changeable via settings.

Say I store a int value as sharedpref. If it's (example) 0, I want use english strings.... If it's 1, I want to use french and so on.

So, how can I pragmatically change the values folder. (I know that by default, andorid WILL select -fr, -ja, -en depending on the locale set on first device startup. But I want the users to have an option to go back to default since (again) translation might be imperfect.

user2999332
  • 1
  • 1
  • 1

2 Answers2

3

Try this..

    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());
  • Do you mind explaining it a bit? If I understood correctly, just changing the string parameter in the above code should do it, right? Also, "config" changes "locale" for the current app only, right? .. no special permissions? – user2999332 Nov 16 '13 at 14:23
  • Yes, I used this code in my app settings screen. when user select language from screen, it will change the default language. But you have to call setContentView(R.layout.yourID) again to take effect. I made like this. Every time when I start an activity, it checks the sharedpreference for selected language, then I change String languageToLoad = "en"; and then call setContentView() It worked for me. Hope it will help you...... – Md. Tahmid Mozaffar Nov 16 '13 at 14:34
  • its not working for build tool Naugot..any alternative? – Rahul Sep 02 '17 at 06:43
2

I've done that before but as a check boxes not spinner, hope it helps you here what I did:

first make an alert message show up when user touch a button:

Button btn2 = (Button) findViewById(R.id.button2); //here is your button
    btn2.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
              final CharSequence[] items = {"العربية", "English", "français", "русский", "中国", "اردو", "Indonesia"};//here you write your supported languages 
              AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);//here the alert dailog that will show up when button touched
              builder.setTitle(getResources().getString(R.string.language)); 
              builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
                  public void onClick(DialogInterface dialog, int item) {
                      i=item;
                      if(items[i] == "العربية"){
                          setLocale("ar");
                      }if(items[i] == "English"){
                          setLocale("en");
                      }if(items[i] == "français"){
                          setLocale("fr");
                      }if(items[i] == "русский"){
                          setLocale("ru");
                      }if(items[i] == "中国"){
                          setLocale("zh");
                      }if(items[i] == "اردو"){
                          setLocale("ur");
                      }if(items[i] == "Indonesia"){
                          setLocale("in");
                      }
                  } 
              }); 
              AlertDialog alert = builder.create(); 
              alert.show(); 
              alert.setCanceledOnTouchOutside(true);
          }});  
}

now the language picker subclass:

public void setLocale(String lang) {

    Locale myLocale = new Locale(lang);
    Resources res = getResources();
    DisplayMetrics dm = res.getDisplayMetrics();
    Configuration conf = res.getConfiguration();
    conf.locale = myLocale;
    res.updateConfiguration(conf, dm);
    Intent refresh = new Intent(this, Main.class);
    refresh.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(refresh);
}

now about adding language folders that holds the actual language (ar, en, ru ... etc) for that I have for you this tutorial https://www.youtube.com/watch?v=WugUIEJ_lvk lastly for translation use Google translator :) and if you have CASH I can hook you up with translation companies or you could use Google services in the developer page.

if you needed anything ask me...

3lomahmed
  • 849
  • 6
  • 10