How can I change language of my application [duplicate] - android

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

Related

Store data for dif . language

I'm new to Android. I'm planning quiz app in several languages. Pictures will be same for all languages, but questions, answers and add. Information will be different. It can be 300 - 500 sentences for every language.
Should I use string res in dif directories for dif languages or maybe better way exists? SQL or something?
you can do that with a string.xml file in your resources folder. Create different folders for each language (values-en, values-es, etc) and inside these folders add the strings.xml file with your text resources. Use the same tags in all files and store a different value for each language. Then populate your views with the corresponding xml as you please
example:
\res\values-en\strings.xml:
<resources>
<string name="greeting">Hello</string>
</resources>
\res\values-es\strings.xml:
<resources>
<string name="greeting">Hola</string>
</resources>
and this is how you change the language 'locale' programmatically:
Locale locale = new Locale("es");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
make sure in your manifest you include the following under your activity:
android:configChanges="locale"
(source)

Android. How change default locale fo string.xml in values

I have an application with only russian locale. If I am not mistaken, the string.xml in the res/values is the english locale by default. But english and russian have different plurals. For example:
In russian:
1,21,31..x1 книга
2-4, 2(2-4), 3(2-4), .., x(2-4) книги
in other cases - книг
In english:
1 book
n books
Problems begin when the user changes system language from russian to other language.
How can I change default language for my application? Or maybe it is possible to force the application to use the russian plurals?
I finally found the answer:
https://developer.android.com/studio/write/tool-attributes#toolslocale
<resources xmlns:tools="http://schemas.android.com/tools"
tools:locale="es">
From the doc:
This tells the tools what the default language/locale is for the resources in the given element (because the tools otherwise assume English) in order to avoid warnings from the spell checker. The value must be a valid locale qualifier.
you can create different String.xml files
like this
values/
strings.xml
values-es/
strings.xml
values-fr/
strings.xml
Check google official document https://developer.android.com/training/basics/supporting-devices/languages.html
& https://developer.android.com/guide/topics/resources/localization.html
create new value folder values-ru and place string.xml in that.
in java change your app language programatically with this code. for change to rushion call method with ru parameter like this changeLanguage("ru").
public void changeLanguage(String languageToLoad) {
//for language change
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
}

How to load alternative strings.xml from resources programmatically

I want to provide three different langauges. The default language is english so all my string values in the values folder are in enlish. I created two other folders
values-de for the german language
values-cn for the chinese language
each folder contains a strings.xml file where i defined the values in german for the de folder and in chinese for the cn folder.
My question now is: How can i load a different language programmatically because i want to provide buttons in my app interface where the user can switch the language. The settings of the device wont be editable for our users. Our users can just see the app itself and nothing else so i have to provide the language switching from within my application.
You can change the configuration at runtime
Do this in onCreate of the activity
String languageToUse = "de"; // The language you want to change
Locale locale = new Locale(languageToUse);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());

How to add new locale for a language 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);

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.

Categories

Resources