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");
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");
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.
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.