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