My country (Spain) has several languages (es-ES, ca-ES, gl-ES, eu-ES). We won't add all the languages for now so we would like to use main language in Spain, i.e. Spanish (es). We would like to display the /values-es/strings.xml when the user has selected one of the other languages in the country. How can we do that?
Oh, and we would like to use English as the default language (/values/strings.xml).
It would be great to have something like /values-ES/strings.xml, but I suppose that can't be done because the first code should be the language code.
Now we are copying the /values-es/strings.xml file to the other folders (values-ca, values-gl and values-eu) but we'd like to avoid that.
I think that you should have only 2 folders with 1 strings.xml for each other:
res/values/strings.xml this resource will contains your text in English ;
res/values-es/strings.xml this resource will contains your text in Spanish .
When your app is installed on a device which is configured with the Italian language, it will use the file resource on case 1.
When your app is installed on a device which is configured with a Spanish language (and there are a lot of Spanish language out there, think about South America countries), it will use the file resource on case 2.
You can do it easily with Android Studio:
right-click on res folder
go to New > Android resource directory
a window will show you some options; pick Locale and then click on the button with those symbols "> >"
then on the Language list, pick es: Spanish and then click OK, as showed in the image below (note that by default the Specific Region Only has Any Region selected!)
By experience: I never faced up a Breton, Corsican or Provençal users claiming for a full translation of the application in their language (by default the app has English as default and French).
I would say you want to do something like this.
if (Locale.getDefault().getISO3Country().equals("ESP"))
{
Locale[] locales = Locale.getAvailableLocales();
for (Locale loc : locales)
if (loc.getISO3Language().equals("SPA"))
{
Locale.setDefault(loc);
break;
}
}
Note: I'm not sure if I got the ISO3 language and country codes right. And you'll also have to do something for the (rare?) situation that the es-ES locale is not available.
If you are trying to override Catalan with Spanish, you should probably have that in the values-ca/strings.xml file.
The way to do what you are asking is to provide the resources in the appropriate mobile country code resource folder, which takes precedence over language-region resources.
Assume that you have the following situation:
The application code calls for R.string.text_a
Two relevant resource files are available:
res/values-mcc404/strings.xml, which includes text_a in the application's default language, in this case English.
res/values-hi/strings.xml, which includes text_a in Hindi.
The application is running on a device that has the following configuration:
The SIM card is connected to a mobile network in India (MCC 404).
The language is set to Hindi (hi).
Android will load text_a from res/values-mcc404/strings.xml (in English), even if the device is configured for Hindi. That is because in the resource-selection process, Android will prefer an MCC match over a language match.
The MCC for Spain is 214.
(See Localization)
I found another tricky solution: hard links. Although it doesn't remove whole problem completely, at least it protects you from routine task of copying file across multiple directories or making equal changes in all existed files with risk of miss something.
But I must admit that there is some caveats:
1) Some IDE does not support working with hard links by-default. Intellij IDEA and Android Studio will break your hard links if you don't disable "safe write" option in settings.
2) Most version control systems also doesn't support hard links by default. Let's take git for example. It will break existing hard links after reverting or merging changes.
Currently, I'm using batch file for restoring hard links after they get broken by git.
In a general term, there should be only one strings.xml file under values folder containing the relevant data.
If we explicitly specify different values folder like values-ca,values-es, whenever there are setting changes in the android device, it will look to the particular folder and take the appropriate strings value.
If the requirement is keep uniform text means better have only values->strings.xml file alone with the required data.
But with this approach multilingual apk is not possible i.e. for other country different language is expected, there will be variations again. So wherever we need uniform language, lets go with single folder alone and wherever multilingual is preferred, we can have multiple values-es,values-ca folder like that.
Hope that helps
How about trying to set it in java, instead of using strings.xml.
As doing it programatically gives you more flexibility at run-time.
Configuration config = new Configuration(getResources().getConfiguration());
Locale locale = Locale.getDefault();
String country = locale.getCountry();
String language = locale.getLanguage();
if (country.equalsIgnoreCase("ES") && (language.equalsIgnoreCase("ca") || language.equalsIgnoreCase("gl") || language.equalsIgnoreCase("eu"))) {
locale = new Locale("es");
}
config.setLocale(locale);
And then you can simply have one /values-es/strings.xml for all the ES country languages.
If I switch my phone's locale to German for example, I still want the option to be able to access the english / default resources using something along the lines of:
getString(R.string.some_text, locale);
I've tried switching the default locale using
Locale.setDefault
but as expected, when using R.getString(x), my resource was still in German.
Answer:
Changing Locale within the app itself
I am making an android app in which i want to change the language of app on selecting a particular language in spinner without changing the language of device.I have made different string files for all language. Now what to do next??
Can anyone please help me over this?
thanks
Most simple way is just to change VM's locale, like:
Locale locale=new Locale("zh"); //Chinese
Locale.setDefault(locale); //set VM's default locale
Once I wanted to implement a multiple language feature for my application. Where that language was not even supported by android.
I made the appropriate strings for all the application and stored them in my resources.
Then I kept the language selection in my shared preferences, so that when user opens the app the next time, we can show the previously selected language. I implemented the code and language changing in my OnResume() function of Activity. the code was like:
if(SelectedLanguage.compareTo("ar")==0)
{
String text = getString(R.string.ar_Options);
tv_Options.setText(ArabicUtilities.reshape(text));
text = getString(R.string.ar_Minimize);
tv_Minimize.setText(ArabicUtilities.reshape(text));
}
else
{
String text = getString(R.string.en_Options);
tv_Options.setText(text);
text = getString(R.string.en_Minimize);
tv_Minimize.setText(text);
}
I hope you get the basic idea. In this way you don't have to change the lanuguage of your device and you can provide multiple languages for your application.
In my application, I need to display strings according to user locale selection. So, I put my strings.xml in values-en, values-ko, etc. How can I set locale us, australia i.e; values-en_US, values-en_AU? But it's throwing an error? Can any one tell me how to set these locales in my code?
Use res/values-en-rUS/ (replacing the _ with -r).
I am not clear with your question but if you are not sure as what name you need to for a specified locale refer to these links
http://developer.android.com/intl/de/reference/java/util/Locale.html
http://groups.google.com/group/android-developers/web/localizing-android-apps-draft
Android by default will take the resources from respective language that has been selected on the device/stimulator.
There is International Standard for Locale.....like values-en for English....values-ko for Korean...You cant modify the standard..It would work if you keep the folder name as values-ko.
This question already has answers here:
Changing Locale within the app itself
(6 answers)
Closed 8 years ago.
Ok i have an application with 2 different languages (english and german), how to change them from my application?
When i click the Language button im using intent to com.android.settings.LocalePicker and from there i select the language.
So instead of that i want to select English and German options from dialog box.
I know how to create the dialog box, but don't know how to change the locale.
Application resources are fetched using the system local which isn't changeable from within an application.
The system settings screen uses a class (ActivityManagerNative) which isn't available via the SDK and thus can not be guaranteed to work between releases, and hence shouldn't be used in your code.
So your options are;
Don't offer the functionality in your app
Implement your own system for determining what setting the user has selected in your app and pulling the appropriate resources using your own code.
try this :
create a normal android dialog with radio button selection of English & German
OnCheckedChange ()
write this to change the Language
Locale myLocale = new Locale(/*String selected*/);
Locale.setDefault(myLocale );
Configuration config2 = new Configuration();
config2.locale = myLocale ;
getBaseContext().getResources().updateConfiguration(config2,
getBaseContext().getResources().getDisplayMetrics());