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
Related
I have created a folder values-fr for French localization and placed String.xml with french strings in it. When I change the language in my testing device to french it's not changing. PS: Xamarin Android.
Just to post the answer from the comments.
Try this to get the Locale language (just to check), put it in to OnCreate() method:
Android.Content.Res.Configuration conf = res.Configuration;
var languageCodeValue = conf.Locale;
if the language it's ok, then check the folder name, you can see more information here https://learn.microsoft.com/es-es/xamarin/android/app-fundamentals/localization
Then check the name of the file, Xaml can't recognize the UpperCase, so you need to define all in minus, check if you have named the file strings.xml
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 has built in functionality to switch between resources based on a users device language (http://developer.android.com/training/basics/supporting-devices/languages.html), but is it possible to switch the resources manually?
For example if I have:
yProject/
res/
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Can I change which string file is used based on the users preference rather than their device language? So someone using a french language device can choose to use the English text if they want?
I know I can do it with string variables in my code rather than using the xml, but I feel the xml would be neater.
Yes you can. This is a copy/paste from a project doing so, based on user preference, to be put in onCreate():
Resources res = getResources();
Configuration conf = res.getConfiguration();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String def = Locale.getDefault().getDisplayLanguage();
String lang = prefs.getString("LANGUAGE", def);
conf.locale = new Locale(lang);
Log.v("myapp", lang+" = "+conf.locale+" = "+conf.locale.getDisplayName());
res.updateConfiguration(conf, res.getDisplayMetrics());
By setting a user preference named LANGUAGE to the two-letter code of the desired language, and then restarting the Activity, you manually override set the language. By removing the preference you get the system default.
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.
I'm porting my iPhone app to android and I'm having a problem with the string files now.
The app is a translation tool and users can switch the languages, so all the localized strings are in both languages and they are independent from what locale the OS is running.
For iOS version I have different files like de.strings, en.strings and fr.strings and so on. For every target with specified language pair I read the strings from the string tables, e.g. for de-fr I will include de.strings and fr.strings in project and set the name of the string tables in the info-list file and read strings from them. In the end I have one project containing different targets (with different info-list files) and all are well configured.
I'm intending to do the same on android platform, but
Is only one strings.xml allowed per project?
How do I set different build target? e.g. my de-fr and de-en translation app are actually two apps where the only difference is the language pairs. How can I set something so that I can use one project to generate the two apps? In XCode it's simple but I can't find a solution with eclipse.
How do I specify per target which strings.xml it should read?
Thank you for your answers but Please Note that I need OS locale independent language settings, i.e. if the user changes OS locale from en to de, my app still shows localized strings in English. What I'm asking is actually how I can set something like prebuild and load different string files for different targets.
Automatic locale selection, according to user settings
The strings.xml contains the original text, assuming for the English language.
To create translations into different languages you can create folders, for example:
values-gr, values.it, for the Greek end Italian.
Just copy strings.xml into those folders and translate it.
On application launch, OS automatically picks a language according to the user's preferences.
Manually locale selection, overriding user settings
To force Greek for example you can use:
Locale locale = new Locale("gr");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
You should, of course, provide a Greek translation for this to work.
Read more
You can check the documentation here:
Support Different Languages - Android
you have to put your localized strings in different folders like values-es, values-de, values-fr, etc.
The file must contain the same keys, for example in values folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello</string>
</resources>
in values-es folder:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hola</string>
</resources>
and so on.
You have to create one values folder for each language adding the language ISO code of the language you want to have a translation using this format: values-es, values-de, ... In each folder you have to add a strings.xml with strings of its language.
The values folder (withoud country code) will be the default language.
For choose the string language you want to use:
import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String languageToLoad = "fa"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);
}
}
ad 1. No you can have as many as you want. See ad 3 for more information.
ad 2. ????
ad 3. To make language selection in our app you should update context. Then proper xml will be selected automatically. Read about this to see how to do it.