Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get string from default locale using string in specific locale

Ok, I know title sound crazy :)

Here is what I want. My app is localized for device user but information I send back to server need to be all English. My default app locale English.

For example, I have array:

  • Apples
  • Oranges
  • Peaches

I have localized array:

  • Яблоки
  • Апельсины
  • Персики

When russian user sees list and selects couple items - I need to get corresponding english versions.

I guess my answer boils down to how to do getString() and pass locale? Or how do I get Array in specific locale?

like image 706
katit Avatar asked Jun 29 '11 19:06

katit


1 Answers

The code below will retrieve localized string for polish language even if the default locale on the device is different:

Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("pl");
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Resources resources = new Resources(getAssets(), metrics, conf);
/* get localized string */
String str = resources.getString(R.string.hello);

I hope this also apply to other resource types like array, so it should suffice to replace "pl" with "en"...

like image 82
wjeshak Avatar answered Oct 03 '22 22:10

wjeshak