-1

How can I get an access to a string resources in language, different from user's current locale language.

String string = context.getString(R.string.string_id, "en");
Semyon Tikhonenko
  • 3,872
  • 6
  • 36
  • 61
  • 1
    http://stackoverflow.com/a/6526588/885028 – aga Mar 05 '15 at 14:02
  • 1
    possible duplicate of [Get string from default locale using string in specific locale](http://stackoverflow.com/questions/6526201/get-string-from-default-locale-using-string-in-specific-locale) – John R Mar 05 '15 at 14:46

2 Answers2

1

You can set your apps configuration. Use this:

Locale locale = new Locale("en");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getResources().updateConfiguration(config, null);

and then you can use:

String string = context.getResources().getString(R.string.string_id);

Good luck.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
0

you normally modify the language before loading the layout of the activity. Besides, you must create a "value-en" clone folder of the "value" one in res. Into this folder you mst put the same strings and resources, with the same name, but translated into another language. When you create these clones folders into "res" folder you can recall the resource in a classic way after having determined the language of the activity. It looks like this way:

try{  
    Configuration c = new Configuration(getResources().getConfiguration());
    String lingua = readFileAsString("/sdcard/Lingua.txt");
    if(lingua.equals("")){ 
        c.locale = Locale.ITALIAN;
    }else if(lingua.equals("ITALIANO")){
        c.locale = Locale.ITALIAN;
    }else if(lingua.equals("ENGLISH")){ 
        c.locale = Locale.US;
    }
    getResources().updateConfiguration(c, getResources().getDisplayMetrics());

}catch(Exception e){ }

setContentView(R.layout.main);
opzioni[0] = getResources().getString(R.string.home);
opzioni[1] = getResources().getString(R.string.indietro);
opzioni[2] = getResources().getString(R.string.disattiva_audio);
opzioni[3] = getResources().getString(R.string.chiudi);
opzioni[4] = getResources().getString(R.string.ripeti);

In this example I recover the laguage from a text file into sdcard and, before charging the layout, I set the language. As you can see the getResources() doesn't specify the language... but it goes to the folder clone with the suffix which explain the right langage.

Alessandro
  • 105
  • 1
  • 9