Multilanguage app from spinner android - android

I am doing multilanguage Android app. I have a spinner to select from the app the different languages to the app and save the selected language in shared preferences. I am novice and I have seen that there isn´t Locale.SPANISH, I am doing well?, here is my code:
Configuration config = new Configuration();
switch (position) {
case 0:
Locale spanish = new Locale("es", "ES");
savePreferences("idioma", spanish.toString());
break;
case 1:
config.locale = Locale.FRENCH;
savePreferences("idioma", config.locale.toString());
break;
case 2:
config.locale = Locale.ENGLISH;
savePreferences("idioma", config.locale.toString());
break;
default:
break;

You can manipulate your translates with help of strings.xml, and there will be no need to handle locale by your own. Look on this article at Android Developers.
And this is how you can get Spanish locale:
Locale spanish = new Locale("es", "ES");
(see Localization Android)

To force the locale of your app you can use this in onCreate or when you press a button or whereever you need to change the lenguage of your app:
Locale locale = new Locale("es", "ES");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
Hope it helps!
EDIT:
You will also need to have a strings.xml for each locale stored in the proper folder (ex: values-es, values-en, values-fr, etc...)

Related

Change android keyboard language to hindi on button click

In my app, I want to open keyboard with Hindi language. I tried this-
String languageToLoad = "in"; // your language
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
But its not helping me, as the keyboard still remains in english language.
You need to change this
String languageToLoad = "hi_IN";

change Language in android using Localization

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());
}

load resources according to users preference

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.

How to change "keyboard language text input" when I change language in my app

I wanna create an app, and in my app support many language. But I can't change keyboard language when i change the language.
Here is the code to change language in my app...
String [] lCode = getResources().getStringArray(R.array.language_code);
Locale locale = new Locale(lCode[position]);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
and here the language_code values xml...
en-us zh_TW fr de ru es

How to get string from specific "values" folder?

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());

Categories

Resources