Reputation: 17915
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:
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?
Upvotes: 19
Views: 19198
Reputation: 2032
I combined the two approaches necessary to work on all Android versions. See my answer here.
Upvotes: 0
Reputation: 51
This one has no side effects: API17 and higher
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(locale);
return context.createConfigurationContext(config).getResources();
Upvotes: 5
Reputation: 370
I had the same issue with ListPreference that saved "state name" and "body figure" preferences. The ListPreference gets its values and entries from an array saved in the localized "values" folders. The list values were the same (english) but the entries were localized (english and hebrew).
I finally used the following code:
public void OnSharedPreferenceChanged (SharedPreferences sharedPreferences, String key)
{
SharedPreferences.Editor editor = sharedPreferences.edit();
Preference pref = settings.findPreference(key);
pref.setSummary(sharedPreferences.getString(key, ""));
if (key.equalsIgnoreCase(PREF_STATE)
|| key.equalsIgnoreCase(PREF_BODY_FIGURE))
{
editor.putString(key, ((ListPreference) pref).getValue());
editor.commit();
}
}
That way the preference always showed the localized string to the user (as the summary) but saved the English string in the preference memory which was ultimately sent to the server.
Upvotes: 0
Reputation: 1491
If you use JB 2.2.x or above (basically API >= 17) you can use createConfigurationContext do:
public String translate(Locale locale, int resId) {
Configuration config = new Configuration(context.getResources().getConfiguration());
config.setLocale(locale);
return context.createConfigurationContext(config).getText(resId);
}
Upvotes: 7
Reputation: 3964
I believe, this is the safe way to go (without touching current configuration):
Configuration conf = new Configuration();
conf.setToDefaults(); // That will set conf.locale to null (current locale)
// We don't want current locale, so modify it
conf.locale = new Locale(""); // Or conf.locale = Locale.ROOT for API 9+
// or conf.setLocale(Locale.ROOT) for API 14+
// Since we need only strings, we can safely set metrics to null
Resources rsrcDflt = new Resources(getAssets(), null, conf);
String apples = srcDflt.getString(R.string.apples); // Got it at last!
Upvotes: 0
Reputation: 6897
This "force" localization won't work if your devices doesn't have locale you're forcing.
Cannot prove this "just like this", but just now i'm struggling with such issue, where i'm forcing resource to be in locale/language witch isn't installed on device (on android 2.3.3), and still getting strings from values-en (english) resources.
On other devices, with locale you're forcing (but not necessary set as current locale), you'll get correct resources.
Upvotes: 2
Reputation: 1064
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"...
Upvotes: 26
Reputation: 170508
You have to identify the element by something else, like an id, or even the English Name.
It is not possible to get the original string for a given localized string. One of the reason is that localization of strings is not a transitive function, but there are many other reasons why this is not a good direction.
Upvotes: 1