Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>NOTE</strong> If your minimum API is 17+, go straight to the bottom of this answer. Otherwise, read on...</p> <p>If you have various res folders for different locales, you can do something like this:</p> <pre><code>Configuration conf = getResources().getConfiguration(); conf.locale = new Locale("pl"); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); Resources resources = new Resources(getAssets(), metrics, conf); String str = resources.getString(id); </code></pre> <p>Alternatively, you can just restart your activity using the method pointed to by @jyotiprakash.</p> <p><strong>NOTE</strong> Calling the <code>Resources</code> constructor like this changes something internally in Android. You will have to invoke the constructor with your original locale to get things back the way they were.</p> <p><strong>EDIT</strong> A slightly different (and somewhat cleaner) recipe for retrieving resources from a specific locale is:</p> <pre><code>Resources res = getResources(); Configuration conf = res.getConfiguration(); Locale savedLocale = conf.locale; conf.locale = desiredLocale; // whatever you want here res.updateConfiguration(conf, null); // second arg null means don't change // retrieve resources from desired locale String str = res.getString(id); // restore original locale conf.locale = savedLocale; res.updateConfiguration(conf, null); </code></pre> <p>As of API level 17, you should use <code>conf.setLocale()</code> instead of directly setting <code>conf.locale</code>. This will correctly update the configuration's layout direction if you happen to be switching between right-to-left and left-to-right locales. (Layout direction was introduced in 17.)</p> <p>There's no point in creating a new <code>Configuration</code> object (as @Nulano suggests in a comment) because calling <code>updateConfiguration</code> is going to change the original configuration obtained by calling <code>res.getConfiguration()</code>.</p> <p>I would hesitate to bundle this up into a <code>getString(int id, String locale)</code> method if you're going to be loading several string resources for a locale. Changing locales (using either recipe) calls for the framework to do a lot of work rebinding all the resources. It's much better to update locales once, retrieve everything you need, and then set the locale back.</p> <p><strong>EDIT</strong> (Thanks to @Mygod):</p> <p>If your minimum API level is 17+, there's a much better approach, as shown in <a href="https://stackoverflow.com/a/33629163/535871">this answer</a> on another thread. For instance, you can create multiple <code>Resource</code> objects, one for each locale you need, with:</p> <pre><code>@NonNull Resources getLocalizedResources(Context context, Locale desiredLocale) { Configuration conf = context.getResources().getConfiguration(); conf = new Configuration(conf); conf.setLocale(desiredLocale); Context localizedContext = context.createConfigurationContext(conf); return localizedContext.getResources(); } </code></pre> <p>Then just retrieve the resources you like from the localized <code>Resource</code> object returned by this method. There's no need to reset anything once you've retrieved the resources.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload