Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get the application's resources languages

Tags:

Is it possible, at runtime, to know which resources languages are embedded in my app?

i.e the presence of this folders:

values-en values-de values-fr ... 
like image 715
VinceFR Avatar asked Jul 23 '12 10:07

VinceFR


People also ask

How do I get current app language?

This example demonstrate about how to Get the current language in Android device. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I change the language on Android runtime?

Use SharedPreferences to keep track of the language the user chose, and then set the activities to use that language in the onCreate (), and maybe onResume() method. This way it will persist across app restarts etc. do all developer uses this way in there apps, I feel its not very clear !


1 Answers

It's complicated because even if you have a folder named values-de it doesn't mean you have any resources there. If you have string.xml in values-de it doesn't mean you have string value there.

values:

<resources>     <string name="app_name">LocTest</string>     <string name="hello_world">Hello world!</string>     <string name="menu_settings">Settings</string> </resources> 

values-de:

<resources>     <string name="hello_world">Hallo Welt!</string> </resources> 

You can test if a resource for a specific locale is different than the default:

DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); Resources r = getResources(); Configuration c = r.getConfiguration(); String[] loc = r.getAssets().getLocales(); for (int i = 0; i < loc.length; i++) {     Log.d("LOCALE", i + ": " + loc[i]);      c.locale = new Locale(loc[i]);     Resources res = new Resources(getAssets(), metrics, c);     String s1 = res.getString(R.string.hello_world);     c.locale = new Locale("");     Resources res2 = new Resources(getAssets(), metrics, c);     String s2 = res2.getString(R.string.hello_world);      if(!s1.equals(s2)){         Log.d("DIFFERENT LOCALE", i + ": "+ s1+" "+s2 +" "+ loc[i]);     } } 

It has one fault - you can check one value whether it has translation.

The dirty code above will print something like:

LOCALE(5667): 51: en_NZ LOCALE(5667): 52: uk_UA LOCALE(5667): 53: nl_BE LOCALE(5667): 54: de_DE DIFFERENT LOCALE(5667): 54: Hallo Welt! Hello world! de_DE LOCALE(5667): 55: ka_GE LOCALE(5667): 56: sv_SE LOCALE(5667): 57: bg_BG LOCALE(5667): 58: de_CH DIFFERENT LOCALE(5667): 58: Hallo Welt! Hello world! de_CH LOCALE(5667): 59: fr_CH LOCALE(5667): 60: fi_FI

like image 81
pawelzieba Avatar answered Oct 28 '22 17:10

pawelzieba