How to add new locale for a language android? - android

I want to use new locale for a language example:
French language for Japan locale or French language for China locale
("fr-rJP" or "fr-rCN"), is it possible?

You will have to create another values folder and name it according to your new language. values is the default, and keep your default strings there, and the new one name it values-jp or fr depending on what you need.
Copy in the new values folder the strings.xml from the default values folder, and simply translate the words accordingly, but dont change the variable name of each string.
And this is what i use to change the locale within my app:
Locale myLocale = new Locale(THE LANGUAGE U WANT HERE); example ('jp' or 'fr')
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);

Related

How to change android app language without changing phone language?

I want user to select a language inside the app. Once the language is selected, I want the strings to use the particular language.
If I change the phone language, then my app runs on the set language.
I am not able to find any way to set a language without changing the phone language. In addition, the changes should be reflected once the language is set.
Could anyone please suggest a way to do it?
Try this
public static void changeLang(Context context, String lang) {
Locale myLocale = new Locale(lang);
Locale.setDefault(myLocale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = myLocale;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}
Lang parameter should be "en" for English, "it" for Italian... After that you should restart your activity/fragment

Changing app's default language for a specific flavor

I'm looking for a way to "swap" the default language with the secondary language that are already defined by the string.xml files in the main project. This should only affect a specific flavor.
Example situation: An app's flavor is targeted to a different region than all other flavors, where the default language does not make sense for users anymore.
Note: Copy-Pasting the string files from the main project is not a solution.
maybe you can try
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
reloadUI(); // you may not need this, in my activity ui must be refreshed immediately so it has a function like this.
}
take from here
The solution I went for, based on MTZ4's solution, was calling this on onCreate() in my application's singleton:
/**
* Sets the locale to {#link Locale#ENGLISH}
* if the device locale is not {#link Locale#GERMAN}.
*/
public void setLocale() {
if(BuildConfig.FLAVOR.equals("flavorWithSwapedLocale")) {
Resources res = getResources();
Configuration conf = res.getConfiguration();
if (!conf.locale.equals(Locale.GERMAN)) {
conf.locale = Locale.ENGLISH;
res.updateConfiguration(conf, res.getDisplayMetrics());
}
}
}
Note that the default language in the main project is German, this swap makes the default flavor language English, unless the device is in German.
With this approach, an app restart might be needed for the changes to show after changing the device's language.
This is how I did it, say you have:
main/
res/
values/
strings.xml
values-es/
strings.xml
where under main/values you have your default language (let's assume English), and under_main/values-es_ the Spanish translation of it. Now you want one of your flavor to default to Spanish instead of English, even when the user select US or UK locales:
main/
res/
values/
strings.xml
values-es/
strings.xml
a_flavor/
res/
values/
strings.xml // <-- this is a symbolic link
so I introduced in the flavor a symbolic link strings.xml to point to the Spanish translation, overwriting the default file under values.
Symlink was created with a command like:
$ cd app/src/a_flavor/res/values
$ ln -s ../../../main/res/values-es/strings.xml strings.xml
Use symlinks to different languages in flavour folders

Can you programmatically switch between xml resource files on android?

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.

Changing strings.xml on the fly

i am trying to post my question understable.
I have 2 strings.xml one is for english and another is for Indian Local Language, let it be Tamil.
I have kept the meanings of all the attributes of strings.xml(english) in my tamil strings.xml
Initially my application will load english strings.xml (as normal)
I have button somewhere which should be used to change the language (tamil in this case)
Upon clicking that - my whole app should be reading my tamil strings.xml ..
I know below code only works for default LOCALE
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = Locale.GERMANY;
res.updateConfiguration(conf, dm);
Can this be tweaked to read the my customized strings.xml on the fly?
Any ideas are greatly appreciated.
That is not possible. It's only possible to change the language of your phone and than you will change the language for your app.
why don't you make a settings activity and in there you put an option to change the language that will transfer you user to a locale and text in the settings page on phone?
Tell me if you need a code or something like that.
Do you mean you want to change your language on demand from English to Tamil within your app?
I call this method in my activity onCreate:
public void setupLocale(Activity c, String NewLocale) {
Resources res = c.getBaseContext().getResources();
Configuration newConfig = new Configuration(res.getConfiguration());
newConfig.locale = new Locale(NewLocale);
res.updateConfiguration(newConfig, null);
}
Where c is my activity and NewLocale is the locale string I want to use. e.g "fr" for french, "da" for Danish. Don't know what Tamil is but you probably do.

How can I change language of my application [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change language programatically in Android
I am new to Android. In my application user can select a language from three languages. Based on the language selected by user, the entire application's language should be change. How can I do this?
Use this to change the language programmatically:
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
Write the country code of the language in place of "en_US" for whatever language you want. For example, for Japanese, ja_JP; for Arabic, ar. Check this link for a list.
And make a folder in res/values-ja for Japanese or res/values-ar for Arabic..
And make a string.xml file, and put whatever languages you want on your layout. It will fetch the default language from values folder otherwise if you want it manually, then it will fetch from your external folder values-ar, etc.
An example of res/values-ar for Arabic:
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string name="label">حسب</string>
<string name="name">بحث</string>
<string name="search">بحث :</string>
</resource>
You can set the locale.
Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale(language_code.toLowerCase());
res.updateConfiguration(conf, dm);
If you have language specific content - you can change that base on the setting.
for more detail you can see Locale and this also

Categories

Resources