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.
            Asked
            
        
        
            Active
            
        
            Viewed 8,398 times
        
    4 Answers
36
            
            
        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;
}
 
    
    
        Eugen Martynov
        
- 19,888
- 10
- 61
- 114
- 
                    Great answer, thank you. Wish I could back-port this and save myself the effort of messing with the `AssetManager` – brandall Mar 27 '16 at 20:39
- 
                    2Thanks! This is also the solution recommended by Google: https://code.google.com/p/android/issues/detail?id=67672 – phreakhead May 10 '16 at 20:40
- 
                    Thank you Eugen.. This answer should be marked as correct one. – Varad Mondkar Apr 16 '18 at 14:01
9
            
            
        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.
 
    
    
        vikifor
        
- 455
- 1
- 5
- 17
- 
                    6
- 
                    2
- 
                    Constructor of Resources changes the locale of your app, so be careful restoring the original resource. – vipsy Nov 23 '15 at 18:28
- 
                    See the comments under the answer of http://stackoverflow.com/questions/5244889/load-language-specific-string-from-resource on how to fix this – Rule Mar 09 '16 at 15:31
2
            
            
        Yes, you can. You have to create a new Resources object specifying the intending Configuration.
 
    
    
        K-ballo
        
- 80,396
- 20
- 159
- 169
0
            
            
        In Java 7 (so not android) Locale can be set differently for format resources and different for display:
Locale.setDefault(DISPLAY, Locale.PL);
Locale.setDefault(FORMAT, Locale.US);
Similar thread: Changing Locale within the app itself .
 
    
    
        Community
        
- 1
- 1
 
    
    
        Waldemar Wosiński
        
- 1,490
- 17
- 28