I read this google doc.
It says we must use this format:
<resource type>-b+<language code>[+<country code>]
for example: value-b+es/string.xml
But somewhere it use value-es/string.xml
Is it true?
also how system can understand which language must choose?
for example I call string by this:
String hello = getResources().getString(R.string.hello_world);
Do I have to use a condition? (if yes how?) ...I couldn't undesrtand the doc well.
Yes. Android OS can choose the best language for user from your app by searching res folder.
For example,you can define the Spanish string values in the res/values-es/strings.xml.
So, if user set up their primary language as a Spanish in the phone, Android OS will read strings from your res/values-es/strings.xml folder first instead of res/values/strings.xml.
If some strings missing in the res/values-es/strings.xml then it will be referenced from res/values/strings.xml
Android loads text and media resources from the project’s ‘res’ directory. based on the current device configuration and locale.
For example, if the code loads a string called ‘R.string.title’, Android will choose the correct value for that string at runtime by loading the appropriate strings.xml file from a matching ‘res/values’ directory.
AndroidAppProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
Now u can load specific locale strings from res folder using:
getResources().getString(R.string.hello_world);
For ex:
Configuration conf = getResources().getConfiguration();
conf.locale = new Locale("fr"); //french language locale
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_world);
this will load R.string.hello_world from values-fr/ directory.
See the Doc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With