Android Google Map Localization - android

I would like to show google map in French, Italian in Android app.
How I can set language of map ?
I checked google map API documentation but didn't find anything to set map language.
I tried below code but doesn't work for me.
String languageToLoad = "zh_CN";
Locale locale = new Locale(languageToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
setContentView(R.layout.activity_maps);

Related

Is every android phone has en_US locale?

I wonder is every android phone has the en_US locale preinstalled? I have to pre-set the Locale in order to prevent comma separator issue.
Currently, I'm using the method below to force change, but I worried exceptions might happen on some devices.
Locale locale = new Locale("en_US");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
I wonder is every android phone has en_US locale preinstalled?
99.99% will have it.
They most likely have it.
In case you would like to check regardless, you could do something like this:
Locale locale = new Locale("en_US");
List availableLocales = Arrays.asList(Locale.getAvailableLocales());
if(!availableLocales.contains(locale)) {
// en_US locale not available, do your stuff here accordingly
}

How to change the application language by user choice?

In my project there is a option for the user to select language. The whole application should change by selecting the language. Language is fetched from the server side.
I referred various sites and links, but couldn't find a better solution. Localization is not possible, because it's a huge app and also language is not fixed , it is fetched from the server side and it can be varied.
Is any other solution is available? Please help...
You can use something like this:
Locale locale = new Locale(languageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, null);
This will set the locale of the app to the desired one, not changing the global locale set on the device. All of the native localisation mechanisms will work with the context locale.
Although its not recommended to use separate language for your app other than the Android system's . But you can still change it .
Below is the code :
private void setLocale (String localeCode , Bundle b ){
Log.d(TAG+"set location function: "+localeCode);
locale = new Locale(localeCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
UserDetail.this.getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
onCreate(null);
}
Use this method call on some user trigger:
setLocale("en-us",savedInstanceStat); // for english
setLocale("ar",savedInstanceStat); // for arabic
To learn more about android locals:
http://www.icanlocalize.com/site/tutorials/android-application-localization-tutorial/

Change language programatically in Android with country

I try to change my locale in an android application.
If I use only language all is ok, but today I added portuguese-br translation to my app.
Code:
Locale locale;
if(language.contains("-")) // In this case, the locale specify also the country
{
String[] country_locale = language.split("-");
locale = new Locale(country_locale[0], country_locale[1]);
}
else
locale = new Locale(language);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
All is ok untill last line.
I know because in a previous piece of my program i could get some string with the correct pt-br locale with this code:
Resources resources = new Resources(ctx.getAssets(), ctx.getResources().getDisplayMetrics(), new_config);
updateConfiguration set my locale to English if Locale was defined with a country code.
String.xml is in values-pt-rBR
While debugging Locale value is set to pt-BR.
EDIT: After further tests, this works on my Android phone, but doesn't work on my tablet (both Sambung with android 4.4.2).
What could be the reason?
EDIT 2: Also over the emulator work if I use a phone, not work if I use a tablet.
On your current Activity, try call the follow after your code:
finish();
startActivity(getIntent());
This will recreate your Activity and, then, reload the string resources.

Localization is not working in Lollipop

I am developing an app which is to be opened directly in Portuguese language and inside that app it has having the option of changing the language to English. So i have to change the language in the code . But when i tested my app in Lollipop, it is directly opening in English Language.
In all other versions except the lollipop , it is working fine.
Can some one help me or suggest me the steps which i have to take to solve this bug.
Thanks
Here is the code which i am using for the localization.
Have some methods been "depreciated" from lollipop?
public static Locale locale = new Locale("pt_BR");
Locale.setDefault(locale);
android.content.res.Configuration config = new android.content.res.Configuration();
config.locale = locale;
this.getResources().updateConfiguration(config, null);
String locale = this.getResources().getConfiguration().locale
.getDisplayName();
Log.i("System out", "(LogIn)Current Language : " + locale);
Due to some security issue --> java.lang.SecurityException: Permission Denial:
Language changing is not working, please try to change
Locale locale = Locale("en_US");
to
Locale locale = Locale("en", "US"); //Locale locale = Locale("language", "Country")

Changing the language manually to Simplifies Chinese

I use this code to change the language of my app manually:
Locale locale = new Locale(OneLanguageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
I set in OneLanguageCode the international code. Everything works find for every code I have tried like "en", "es", "fr" and so on... But I am driving myself crazy with simplified Chinese.
In that case I have the folder called values-zh-rCN where the strings are. It works well if I set the device language to Simplified Chinese. But there is no way to set it manually with the code above.
I have tried using "zh-CN" as the OneLanguageCode value, but no success either. It is displayed in English.
As I said all this work with other languages. I can have people with Dutch as their device default language but have French in my app. Why am I not able to have Dutch as the device default language but have traditional Chinese in my app?
Thanks for your time.
Locale locale = new Locale("en_CA");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
context.getApplicationContext().getResources().updateConfiguration(config, null);
Thanks for the help of user2968888 this is how I solved:
Locale locale;
if(OneLanguageCode.equals("schinese")) //any tag here to know it is Simplified Chinese
locale = Locale.SIMPLIFIED_CHINESE;
else
locale = new Locale(OneLanguageCode);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getApplicationContext().getResources().updateConfiguration(config, null);

Categories

Resources