How does the android load the resources(string)? [duplicate] - android

For e.g there is app which provides multi-language support, in my activity/UI, I call getResources().getString(R.string.hello) which exist in strings.xml,such that
values\strings.xml
values-ru\strings.xml
Now when calling getResources().getString(R.string.hello) and need to access string based on system locale, so will one get strings from values\strings.xml OR values-ru\strings.xml?
OR
does one need to change my app locale based on system locale (keep app locale same as system locale) and then retrieve the value from getString(), something suggested in below links
get-string-from-default-locale-using-string-in-specific-locale
how-to-get-string-from-different-locales-in-android
I have searched various other links, but not able to find the solution

MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For info on Localizing with Resources
http://developer.android.com/guide/topics/resources/localization.html
More info #
http://developer.android.com/training/basics/supporting-devices/languages.html
Also check the below link
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/content/ContextWrapper.java/
86 #Override
87 public Resources getResources()
88 {
89 return mBase.getResources();
90 }
Return a Resources instance for your application's package.
332
333 public final String getString(int resId) {
334 return getResources().getString(resId);
335 }
Return a localized string from the application's package's default string table.
Parameters:
resId Resource id for the string

It is done automatically. By standard the language that is on is in your values\strings.xml but if the user device has his language set to ru the string automatically is the one on the values-ru\strings.xml and so on for all the languages that you put on your resources.
You can read more about this subject in here.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.

Related

Android localization for any region some country

I create folder values-ru (for any region). But with using Ukrainian language or Belarus i have an error.
android.content.res.Resources$NotFoundException: String resource ID #0x7f0d0037
Tell me, why i got an error. Does i must create values-ru-uk and etc. for all region?
You should define all your strings in the default (source) language, regardless of what translations you're adding.
These goes in: values/strings.xml.
If this is missing (or the string is missing in that file) then I expect that's why you get an error.
You can then add as many language files as you need: e.g:
values-uk/strings.xml. (Ukrainian)
values-be/strings.xml. (Belarusian)
or specific regions thereof: e.g:
values-ru-rUA/strings.xml. (Russian in Ukraine)
Disclaimer: I'm not an Android developer, but I've written localisation software that generates strings files.

multi language support in android studio

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

How getResources().getString() works android

For e.g there is app which provides multi-language support, in my activity/UI, I call getResources().getString(R.string.hello) which exist in strings.xml,such that
values\strings.xml
values-ru\strings.xml
Now when calling getResources().getString(R.string.hello) and need to access string based on system locale, so will one get strings from values\strings.xml OR values-ru\strings.xml?
OR
does one need to change my app locale based on system locale (keep app locale same as system locale) and then retrieve the value from getString(), something suggested in below links
get-string-from-default-locale-using-string-in-specific-locale
how-to-get-string-from-different-locales-in-android
I have searched various other links, but not able to find the solution
MyProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Add the string values for each locale into the appropriate file.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.
For info on Localizing with Resources
http://developer.android.com/guide/topics/resources/localization.html
More info #
http://developer.android.com/training/basics/supporting-devices/languages.html
Also check the below link
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.3_r2.1/android/content/ContextWrapper.java/
86 #Override
87 public Resources getResources()
88 {
89 return mBase.getResources();
90 }
Return a Resources instance for your application's package.
332
333 public final String getString(int resId) {
334 return getResources().getString(resId);
335 }
Return a localized string from the application's package's default string table.
Parameters:
resId Resource id for the string
It is done automatically. By standard the language that is on is in your values\strings.xml but if the user device has his language set to ru the string automatically is the one on the values-ru\strings.xml and so on for all the languages that you put on your resources.
You can read more about this subject in here.
At runtime, the Android system uses the appropriate set of string resources based on the locale currently set for the user's device.

android strings.xml in various languages, scenario

I have a strings.xml question
My app is in english.
values/strings.xml is in English
there are many other languages in the app too
my latest string key additions are in values-en/strings.xml , the english locale folder , but not in values the default language folder
how will this affect a non-english user that loads a view which tries to access the strings only defined in values-en? will the OS find the string in that one file and display it in english?
this is tricky to me because it is not in the default values xml file
thanks for the insight
Imagine the strings system as a series of if conditions. First we check the language of the user, once we have that we check to see if the folder for that specific language contains the string we are looking for. If it does, we return that string, otherwise we check the "values" folder by default.
if (language.equals("en") {
stringsFolders = "values-en";
} else if (language.equals("es") {
stringsFolders = "values-es";
}
if (stringsFolder.contains(key) {
return stringsFolder.get(key);
} else if ("values".contains(key) {
return "values".get(key);
} else {
throw CantFindException();
}
If a string belongs in values-en and a spanish user looks for it, they will not have the opportunity to check the values-en folder because they don't qualify for it. Alternatively, the "values" folder is always checked by default so placing it there will work for all languages

Is there a way to retrieve ALL localised versions of an Android application's label?

If an application label is localised, I have a requirement to retrieve all localisation variants. Effectively I need to know if the android:label points to a string reference and, if so, all the locale values present in the string resources. Any ideas?
I don't have a full answer but here's what I came up with.
Localization - Here is the official documentation for android localization. You can ensure that your android:label points to a string reference if you have it point to a key in the /res/values/strings.xml. Specific localization strings can be overridden by using qualifiers on the directory, for instance for French versions you can put them in /res/values-fr/strings.xml. Any values not found in the French version will default to the previous unqualified file.
I'm not sure about getting all the locale strings for a specific resource but there is getAssets().getLocales() which will return a string array of the locales the asset manager has resources for (I'd assume for any resource, not just a particular resource)
Lastly, if you don't need to do this at run time you could try to write a script/program to examine all the localization directories under /res and compile a list of all resources with their localized values.

Categories

Resources