I wanted to make android app which supports English and Swedish. I have gone through the localization concept. But i wonted to have two buttons on click on English button should load the strings according to English and on click on Swedish button should load the Swedish strings. how can i do this?
Sv=Swedish... en=English...
enter your language code in languageToLoad :
String languageToLoad = "Sv"; // 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);
Declare two buttons, one for english and another for swedish. your code for english en and for swedish sv and then you have use Localization,this way..
Locale myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
check this simple example to achieve your goal.
My answer is similar to others but I have added some extras that may be important
Warning
Everything I have read says that its not a good idea to let your app change the language because it isn't supported by the Android framework and it may cause problems.
With that being said, I have done it in my app and, while it has been a bit of a pain, it seems to be working so far. Here is how I did it in case you want to do it this way. You need a separate strings.xml file for each language. strings.xml in values folder as your default then maybe a strings.xml in say a values-es folder for Spanish strings. I have used the following code to change the configuration settings depending on a radio button that the user selects
`
final Configuration LANG_CONFIG = ChooseLocale.this.getResources().getConfiguration();
Locale newLocale = new Locale("English");
curLang = ChooseLocale.this.getLanguage();
if ((curLang.equals("English")) || (curLang.equalsIgnoreCase("Ingles")))
{
newLocale = new Locale("en_US");
}
else
{
newLocale = new Locale("es");
}
Toast.makeText(getApplicationContext(), newLangToast + " " + curLang , Toast.LENGTH_SHORT).show();
Configuration config = getBaseContext().getResources().getConfiguration();
Locale.setDefault(newLocale);
config.locale = newLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
SharedPreferences.Editor editor = langPref.edit();
editor.putString(LANG_PREF, curLang);
editor.commit();
`
With this line being the most important to update the config
getBaseContext().getResources().updateConfiguration(config,getBaseContext().getResources().getDisplayMetrics());
I get the local from my getLanguage() function which can be handled however you want. I also had to add
#Override public void onConfigurationChanged(Configuration newConfig) { newConfig = Globals.getUserLanguage(this); super.onConfigurationChanged(newConfig); to every activity that allows orientation change and add this to my onCreate() of each
final SharedPreferences langPref = getSharedPreferences (LANG_PREF, 0); if (Globals.langConfig != null) this.onConfigurationChanged(Globals.langConfig);
I also added android:configChanges="orientation|locale" to each activity in the manifest that allows orientation change. I store the users language preference in a DB that can be changed on the website or within the app. Hope this helps.
Related
I want to format all numbers in my app to arabic numbers (١ ٢ ٣ ٤ ٥ ٦ ٧ ٨ ٩) so I thought about setting the locale to a combination of arabic and region : Egypt instead of only arabic but unfortunately for a reason or another, the locale is not set.
I set my locale like so:
Locale myLocale = new Locale("ar_EG");
Locale.setDefault(myLocale);
Configuration config = new Configuration();
config.locale = myLocale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
And my strings.xml is put in a folder named "values-ar-rEG".
What am I getting wrong?
Thank you very much in advance.
Locale class can receive 1,2 or 3 parameters, but it is better to use 2 parameters (language, country) to change text and numbers format and direction for right to left languages (i.e.).
I resolved it in this way:
Locale myLocale = new Locale("ar","EG");
Locale.setDefault(myLocale);
Resources res = getApplicationContext().getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.setLayoutDirection(myLocale);
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
In my app I am having an option of switching from Chinese to traditional chinese.
I am using spinner, where position 1 is chinese and 2 is traditional chinese. When position 1 is selected, here is my code which switches the language
if (pos == 0)
{
langSelected ="en";
}
else if (pos == 1)
{
langSelected ="zh";
}
else if (pos == 2)
{
langSelected ="zh-rTW";
}
Locale locale = new Locale(lang);
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
#Override
public void onConfigurationChanged(android.content.res.Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
if (locale != null){
newConfig.locale = locale;
Locale.setDefault(locale);
getBaseContext().getResources().updateConfiguration(newConfig, getBaseContext().getResources().getDisplayMetrics());
}
}
While switching from english to chinese using spinner the correct language gets loaded, but when loading traditional chinese(zh-rTW), only the chinese text gets loaded
I have my simplified chinese text in values-zh, where as I have loaded the Traditional Chinese text in values-zh-rTW
The app name differs for each language, so I also tried changing the Language from device Settings, now also in Simplified Chinese it is loading correctly but traditional Chinese not loading. But here the app name gets changed for Traditional Chinese, i.e. app name loads from values-zh-rTW
Where I am going wrong, should I have to change the folder for Traditional Chinese ?
I know it's a late post, but hope it helps someone..
The solution is to simply create a Locale using the country name. What this means is that the Locale class already has some static locale declared. For example:-
China locale - https://developer.android.com/reference/java/util/Locale.html#CHINA
Taiwan locale - https://developer.android.com/reference/java/util/Locale.html#TAIWAN
So put simply, the solution is:-
Locale locale;
if(lang.equals("zh-rTW"))
locale = Locale.TAIWAN;
else(lang.equals("zh-rCN")
locale = Locale.CHINA;
else
//handle other languages
This code changes locales of Chinese simplified and traditional:
public void setAppLocale(Context context, String languageCode, String countryCode){
Resources resources = context.getResources();
Locale locale = new Locale(languageCode, countryCode);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.setLocale(locale);
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
From my experience it is better to have values-zh-rCN for simplified Chinese and values-zh-rTW for traditional. Also, and this might be already what you are doing somewhere further in you code, changing the locale manually requires to reload the activity in order to take affect. So a simple Finish() and StartActivity() should be enough.
I am creating an app which will support Hindi and Gujrati language. I am setting the application language from my app settings screen. like i given a option to user to select a language among English/Hindi/Gujrati.
I am setting Locale on radio button selection basis. I am saving the selection in persistence and on that basis i am changing the typeface of all of textviews in my application.
EVERYTHING IS WORKING FINE.. but it changes the language to english in between the app running. suppose i selected the hindi language from my settings screen and running my app. suddenly after 10-15 min it takes text values from "values" directory, not from "values-hi". I really don't understand why its taking from default values directory. my applications dynamic data is working fine. its coming in hindi and even my app drawables are also working fine but the problem is only that it takes the values from "values" directory.
THIS METHOD IS USED WHEN USER SELECT THE LANGUAGE FROM MY APP SETTINGS SCREEN.
public void setLocale(Context context, String lang) {
Locale myLocale = new Locale(lang);
Resources res = context.getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
}
THIS METHOD IS USED TO SETTYPEFACE OF TEXTVIEW IN ONCREATE METHOD
public static void setTypeface(TextView textView, Context context) {
SharedPreferences sp = context.getSharedPreferences("language_selection", context.MODE_PRIVATE);
String language = sp.getString("language", "English");
if (language != null) {
if (language.equalsIgnoreCase("Hindi")) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "gargi.ttf"));
}
if (language.equalsIgnoreCase("Gujrati")) {
textView.setTypeface(Typeface.createFromAsset(context.getAssets(), "SHRUTI.TTF"));
}
}
}
Try to set your selected Language on this way:
Locale locale = new Locale("YourSelectedLang");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
I need to change localization of labels in my app. I have created two string folders for English and Hindi language. When I change Localization it’s working fine and all label show in hindi.
private void setLanguage(Boolean changeLang) {
if (changeLang) {
Locale.setDefault(new Locale("hi", "IN"));
Configuration config = new Configuration();
config = viewDashboard.getBaseContext().getResources().getConfiguration();
config.locale = new Locale("hi", "IN");
viewDashboard
.getBaseContext()
.getResources()
.updateConfiguration(
config,
viewDashboard.getBaseContext().getResources()
.getDisplayMetrics());
} else {
Locale.setDefault(new Locale("en", "EN"));
Configuration config = new Configuration();
config = viewDashboard.getBaseContext().getResources().getConfiguration();
config.locale = new Locale("en", "EN");
viewDashboard
.getBaseContext()
.getResources()
.updateConfiguration(
config,
viewDashboard.getBaseContext().getResources()
.getDisplayMetrics());
}
}
But It also change date picker language to Hindi which I don’t need.
In same way few textviews also show Hindi fonts i.e where I have set date from date picker or show total amount after calculation and Dialog box ‘OK’ and ‘Cancel’ convert into Hindi that I don’t want.
My question is how can I prevent date picker and dialog box button label from converting to Hindi after changing localization programmatically.
I have done it like this i think it will be help full for u.Just pass two parameter to this method one is language and other is activity where you are calling this method.
public static void setLocale(String language, Activity activity)
{
Locale locale= new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
thisActivity = activity;
thisActivity.getBaseContext().getResources().updateConfiguration(config, thisActivity.getBaseContext().getResources().getDisplayMetrics());
}
For example: I have a multi-language app and I want to get some string from random language and user have to guess what language it is.
How to get single string from other language?
The language-specific strings the app uses depends on the system local which is set up in the android system (Link). So Android automatically pics up the correct value. You cannot hardcode which specific (or random) value to be used.
One way to get things done is to change the local setting of android programmatically:
Locale locale = new Locale("fr");
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, null);
getResources().getString(R.string.local_string);
In this case the only thing to do is to shuffle the local identifier. Of course you should safe an instance of local right before changing it, so that you can reset it to original state. Good luck ;)
edit:
here is some dirty code I wrote to verify:
Locale orgLocal = getResources().getConfiguration().locale;
String[] availLocales = { "fr", "de", "en" };
Locale tempLocale = new Locale(availLocales[new Random().nextInt(availLocales.length)]);
Configuration config = new Configuration();
config.locale = tempLocale;
getResources().updateConfiguration(config, null);
String randLangString = getResources().getString(R.string.local_string);
config.locale = orgLocal;
getResources().updateConfiguration(config, null);
Toast.makeText(this, randLangString, Toast.LENGTH_LONG).show();
First Use this Code to change the Language Setting
Locale locale = new Locale("kn");
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, null);
getResources().getString(R.string.local_string);
Then For Support all type of language use
Unicode Fonts like-- "ARIALUNI.TTF" Arial Unicode Font(copy into asset folder)
Typeface font= Typeface.createFromAsset(getAssets(), "ARIALUNI.TTF"); TextView tv1.setTypeface(font);
tv1.setText(R.string.txt);
*Take R.string.text value from values-en-for english and values-kn folder for kannada language *
Its quite surprising and belive to that we can only alter configuration by to enable which language we want . Depending on need
Locale locale;
locale = new Locale("fr","FR");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
this.getActivity().getResources().updateConfiguration(config, this.getActivity().getResources().getDisplayMetrics());