Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I access to resources from different locale android?

Tags:

android

locale

I have two locale in my application. Can I access to resources, for example string array from different locale without to change current locale ? I mean with coding I don't like to change it in Settings.

like image 819
vikifor Avatar asked Jun 03 '12 22:06

vikifor


People also ask

How do I change localization in Android?

You'll find this screen either in the System Settings app: Languages, or System Settings: System: Languages and input. The Language preference screen should contain one entry called “English (Europe)”. Click Add language and add a fallback language.

What is locale Android?

With Locale®, you create situations specifying conditions under which your phone's settings should change. For example, your "At Work" situation notices when your Location condition is "77 Massachusetts Ave.," and changes your Volume setting to vibrate.

How do I localize a string in Android?

Localizing Strings In order to localize the strings used in your application , make a new folder under res with name of values-local where local would be the replaced with the region. Once that folder is made, copy the strings. xmlfrom default folder to the folder you have created. And change its contents.

What is the use of resource ID in android?

drawable for all drawable resources) and for each resource of that type, there is a static integer (for example, R. drawable. icon ). This integer is the resource ID that you can use to retrieve your resource.


2 Answers

The better solution would be (if you're on API 17):

@NonNull
protected String getEnglishString() {
    Configuration configuration = getEnglishConfiguration();

    return getContext().createConfigurationContext(configuration).getResources().getString(message);
}

@NonNull
private Configuration getEnglishConfiguration() {
    Configuration configuration = new Configuration(getContext().getResources().getConfiguration());
    configuration.setLocale(new Locale("en"));
    return configuration;
}
like image 181
Eugen Martynov Avatar answered Sep 21 '22 12:09

Eugen Martynov


Here is the code that work for me if cMK is String array from current locale and cEN is string array from diffrent locale

 cMK = getResources().getStringArray(R.array.cities);

         Configuration confTmp =new Configuration( getResources().getConfiguration());

         confTmp.locale = new Locale("en");

         DisplayMetrics metrics = new DisplayMetrics();

         getWindowManager().getDefaultDisplay().getMetrics(metrics);

         Resources resources = new Resources(getAssets(), metrics, confTmp);

         /* get localized string */
         cENG = getResources().getStringArray(R.array.cities);

The current locale isn't changed and that was the point.

like image 30
vikifor Avatar answered Sep 20 '22 12:09

vikifor