2

I have string.xml into files values-ar , values-fr and I want to get a string value from one of them manually. for example:

R.string-ar....
R.string-fr....

I know that the system search the string according to the system language or the app language. but I want to get a string from a specified file regardless of the system or the app language!

david
  • 157
  • 1
  • 1
  • 12

2 Answers2

0

here you go

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

Locale newlocale = new Locale("fr");
setLocale(newlocale );
String value = getResources().getString(R.string.username);//fr string
setLocale(current );


public void setLocale(Locale locale){
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
      getBaseContext().getResources().getDisplayMetrics());
}
Yazan
  • 6,074
  • 1
  • 19
  • 33
  • edit was made, Locale.setDefault(locale); first line of the function was newlocale (wrong) – Yazan Sep 01 '14 at 06:59
  • Yazan can you take a look at my question? http://stackoverflow.com/questions/25566116/android-spanned-text-while-deleting-smiles-shows-text – Nadir Novruzov Sep 01 '14 at 07:03
0

This is changing locale programmatically, but you need to call this in every activity onCreate

Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);
Nadir Novruzov
  • 467
  • 1
  • 6
  • 16