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.
Related
The Serbian language has Latin and Cyrillic alphabets. In Android's Date and Time Picker widgets, the displayed alphabet for Serbian locales seems to be Cyrillic, as seen here.
I wanted to change the locale so that the android widgets are using the Latin Serbian alphabet.
The current language/country code (yielding Cyrillic) are sr and RS respectively. Therefore, my setLocale function is called as
setLocale("sr", "RS");
This is the part im not sure about - according to localeplanet.com, the local code for latin serbian is sr_Latn_RS. However, I tried both
setLocale("sr_Latn", "RS");
//and
setLocale("sr_Latn_RS", "RS");
neither of which work (no change occurs, default to english). According to the Android documentation, it looks like setLocale expects two letter codes.
The language codes are two-letter lowercase ISO language codes (such
as "en") as defined by ISO 639-1. The country codes are two-letter
uppercase ISO country codes (such as "US") as defined by ISO 3166-1.
The variant codes are unspecified.
So how do I specify a Latin serbian locale code? Or does it not exist?
The previous answer works well if you only support Lollipop or above. However, if you're coding in Serbian a lot of your user base probably won't have it. Here's a solution that works for old and new versions.
private static Locale serbianLatinLocale(){
Locale locale = null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
for (Locale checkLocale : Locale.getAvailableLocales()) {
if (checkLocale.getISO3Language().equals("srp") && checkLocale.getCountry().equals("LATN") && checkLocale.getVariant().equals("")) {
locale = checkLocale;
}
}
} else {
locale = new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
}
return locale;
}
For getting latin locale I first used code below.
new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
But this solution didn't work on my Android 5.1.1 device (it was still in cyrillic). So I removed setting of region like this:
new Locale.Builder().setLanguage("sr").setScript("Latn").build();
And you have to put your string for serbian resources in b+sr+Latn folder.
Please search for your query before posting a question. It may be answered in some other related form.
Locale newLocale = new Locale("sr","RS");
Configuration config = new Configuration();
config.setLocale(newLocale);
// using this to reference my Activity
this.getBaseContext().getResources().updateConfiguration(config, this.getBaseContext().getResources().getDisplayMetrics());
i found these two answers suitable to your query
android custom date-picker SO and locale from english to french.
EDIT
Locale[] locales = Locale.getAvailableLocales();
for(Locale locale : locales){
if(locale.getCountry().equalsIgnoreCase("RS")
&& locale.getScript().equalsIgnoreCase("Latn"))
{
Configuration config = new Configuration();
config.setLocale(locale);
// using this to reference my Activity
this.getBaseContext().getResources().updateConfiguration(config, this.getBaseContext().getResources().getDisplayMetrics());
break;
}
}
I know there will be an efficient way to do it, however you may get the direction that you need to get the list of available locales and get the locale you desire. Hope it helps
EDIT-2 (Final)
you can construct the locale using:
Locale locale = new Locale.Builder().setLanguage("sr").setRegion("RS").setScript("Latn").build();
setLocale(locale);
Can you please use below one ?
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Resources res = this.getResources();
Configuration conf = res.getConfiguration();
boolean isLatinAlphabet = PreferenceManager.getDefaultSharedPreferences(this);
if(conf.locale.getLanguage().equals("sr") && isLatinAlphabet) {
conf.locale = new Locale("sr", "YourContryCode");
res.updateConfiguration(conf, res.getDisplayMetrics());
}
}
}
Note: Replace your YourContryCode in conf.locale = new Locale("sr", "YourContryCode"); line.
Manifest.xml:
<application
android:name=".MyApplication"
android:icon="#drawable/ic_launcher"
android:label="#string/application_name"
android:theme="#style/AppTheme">
...
</application>
Hope this will help you.
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 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.
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());
I develop finance app that should use different currency symbol which depends on the country. I'm using this code:
Float value = 100;
TextView tv = (TextView) view;
NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
tv.setText(currencyFormat.format(value));
I have several reports that my app is returning Euro symbol instead the right currency (in Malysia or India for example). Those users state that their system language is native to their country. I can't prove that myself because there are no such languages avalaible on my phone or emulator.
I suppose you could try and force a locale in your app since this seems to be related to how the default locale is being set on the device. See these links -
how to choose serbain language from android Emulator settings?
http://www.tutorialforandroid.com/2009/01/force-localize-application-on-android.html
This would probably help you replicate the issue that users reported on their devices.
EDIT:
I have not tested this myself but it could work in your case.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String languageToLoad = "en";
String countryToLoad = "MY"; // 2 digit country code for Malaysia
Locale locale = new Locale(languageToLoad, countryToLoad);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.main);
}