Change locale in android app (onto Hindi) - android

Creating multi languages support for the app with possibility to change language in App settings.
As for now everything works fine for English, Spanish, Franch, Russian languages,
but doesn't work for Hindi and Chineese.
1) I specify the language name in original language
but in the app instead of हिन्दी I can see "Hindi".
\res\values-hi\arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="languages">
<item name="english">English</item>
<item name="russian">Русский</item>
<item name="spanish">Espagnol</item>
<item name="russian">Français</item>
<item name="chineese">中国</item>
<item name="hindi">हिन्दी</item>
</string-array>
<string-array name="languagesValues">
<item name="english">en</item>
<item name="russian">ru</item>
<item name="spanish">es</item>
<item name="russian">fr</item>
<item name="chineese">zh-CN</item>
<item name="hindi">hi</item>
</string-array>
</resources>
2) End after selecting "Hindi" - actually default (English) is being selected.
\res\values-hi\strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">लकी बीनने</string>
<string name="score">0000</string>
<string name="settings">सेटिंग</string>
<string name="start_button">प्रारंभ</string>
<string name="about_button">के बारे में</string>
<string name="about">लियोनिद द्वारा बनाया गया</string>
<string name="feedback">प्रतिक्रिया भेजें </string>
<string name="high_score">उच्च स्कोर के</string>
<string name="score_set">स्कोर निर्धारित किया गया है \r\nमें:</string>
<string name="game_over">खेल खत्म</string>
....
Saving locale in Preferences.
public class Settings extends PreferenceActivity {
Locale myLocale;
static boolean localeChanged;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
// language changing
Preference langPreference = getPreferenceScreen().findPreference(
"language");
langPreference.setOnPreferenceChangeListener(languageChangeListener);
}
Preference.OnPreferenceChangeListener languageChangeListener = new OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
switch (newValue.toString()) {
case "en":
setLocale("en");
break;
case "ru":
setLocale("ru");
break;
case "fr":
setLocale("fr");
break;
case "es":
setLocale("es");
break;
case "zh-CN":
setLocale("zh-CN");
break;
case "hi":
setLocale("hi");
break;
}
localeChanged = true;
return true;
}
};
// * manually changing locale/
public void setLocale(String lang) {
myLocale = new Locale(lang);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, dm);
Intent refresh = new Intent(this, Settings.class);
startActivity(refresh);
finish();
}
What is wrong? Is something wrong in my code, or in my device?

It depends on the device. Also, please check whether your device supports Hindi and Chinese languages by going to Settings --> Language & input.

I. First thing first:
Your post has lots of logical and spelling mistakes. Lint will report errors and warnings for such, and code will not compile.
Please see the corrections:
Creating multi languages support for the app with possibility to change language in App settings.
As for now everything works fine for English, Spanish, French, Russian languages,
but doesn't work for Hindi and Chinese.
1) I specify the language name in original language
but in the app instead of हिन्दी I can see "Hindi".
\res\values-hi\arrays.xml
<string-array name="languages">
<item name="english">English</item>
<item name="russian">Русский</item>
<item name="spanish">Espagnol</item>
<item name="french">Français</item>
<item name="chinese">中国</item>
<item name="hindi">हिन्दी</item>
</string-array>
<string-array name="languagesValues">
<item name="english">en</item>
<item name="russian">ru</item>
<item name="spanish">es</item>
<item name="french">fr</item>
<item name="chinese">zh-CN</item>
<item name="hindi">hi</item>
</string-array>
[COMMENTS]
Spelling mistakes: in the words chinese, french.
Logical mistake: repetition in the key russian, in both the arrays, where on the second repeat, it should be french.
II. Improvements for #Chandrakanth's answer:
It depends on the device, and your device seems to be old one, while your code is correct. Giving you the reasons why:
Beginning with my answer, I want to quote from your question:
As for now everything works fine for English, Spanish, French, Russian
languages, but doesn't work for Hindi and Chinese.
When you are using Chinese and Hindi, you are using following locales, as from your string resources:
<item name="chineese">zh-CN</item>
<item name="hindi">hi</item>
Please refactor it to:
<item name="chinese">zh</item>
<item name="hindi">hi</item>
So you should be using the values folders as:
values-zh and values-hi
If the above is correct then your application is not accepting or recognizing values-zh and values-hi in some old device.
Please share your device make, manufacturer and model, so that more
investigations can be done.
Your code is OK, hence you can test your application in any other
and newer device.
Please call the method where it changes the locale, just before
calling "setContentView" inside "onCreate" call back.
Maybe you can check whether your device supports Hindi and Chinese
language by going Settings --> Language & input.
I have devices that don't have Hindi and Chinese in the device
settings, but still the locales hi and zh are recognized within the
application, though I have to install corresponding fonts to prevent
the fallback.
Please note that Hindi and Chinese are locales from different time zones, so most likely, they can not be found in the same devices settings.
Please see that your code is working on other new devices. If not, then you have to follow steps below. There are some basic understandings first:
Firstly, if you change locale in your device settings, it should have no effect on your application level locale settings. That is, if your device locale is set to Spanish, and your Application level locale preference is set to Hindi, Hindi should continue to show on every fragment/activity of your application if each one of these components are have the strings in them localized. Now if you change your locale from Spanish to Chinese, and your application or device is restarted, since zh is stored in your preferences, your method changes the locale to Chinese, on Application load and it continues to show text in Chinese.
Secondly, if your language depends upon font for example: since you have English, Hindi and Chinese locale support in your application, you have to have all TTF fonts in your asset folder and apply the Font-face to TextView, EditText, Radio Buttons, Check Box, Spinners etc. appropriately, when you change the language from your application settings. Here Hindi, and Chinese locales need TTF fonts to be there for each, in assets folder and has to be used in every component's font-face as mentioned above.
Thirdly, and most importantly, the best place to change your locale is just before setContentView(layout_id) of an activity is called in onCreate(Bundle restoreState) method. Therefore as soon as locale is changed, you have to send the control back to your dashboard/home screen so as to immediately see the effect.
Please see Android get hosting Activity from a view
You can also see How to use font assets using XML resources, in Android
and Using styles for your components in Android
Happy Coding :-)

Related

Localization strings not shown properly

I have an app we need to translate to several languages. So, I made values-en, values-pt, etc. One of the languages is turkish. So, I made my folder values-tr, and inside that, strings.xml file. Everything goes ok, except for one particular Activity. The strings that appear there are the ones in spanish (the default language in our app). BUT, if I change to another language (say, russian) and back to turkish, the strings are shown ok.
TextView mTitle = toolbarTop.findViewById(R.id.toolbar_title);
mTitle.setText(getString(R.string.mapproviders));
(In the strings.xml in values-tr):
<string name="mapproviders">Tedarikçi Haritası</string>
It works ok for other languages, it only fails in turkish and in that particular Activity.
Anyone has faced a similar problem?
UPDATE:
This is the way I am changing Locale:
public static void changeLang(String lang, Context ctx) {
Configuration config = ctx.getResources().getConfiguration();
if (!"".equals(lang) && !config.locale.getLanguage().equals(lang)) {
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Configuration conf = new Configuration(config);
conf.locale = locale;
ctx.getResources().updateConfiguration(conf, ctx.getResources().getDisplayMetrics());
}
}

Android getString() with multilingual not working

I've create a short test Application with the following pre conditions:
My Smartphone run on 7.0 (and up). I've setup multiple languages in my settings English (United States) & Deutsch (Deutschland).
This is my App setup:
compile-, target-, min-SdkVersion 24
String-Resources:
The content from strings.xml ("default") is:
<resources>
<string name="app_name">Test</string>
<string name="test">default</string>
</resources>
And the content from string.xml (values-en-rGB) is:
<resources>
<string name="test">GB Fallback!</string>
</resources>
The MainActivity have only a onCreate() with the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String string = getString(R.string.test);
Log.d("TestString", string);
final LocaleList locaList = LocaleList.getDefault();
for (int i = 0; i < locaList.size(); i++) {
Log.d("AvailableLocale", locaList.get(i).toLanguageTag());
}
}
When I run my App I expect the output from "TestString" is GB Fallback!. Why? Because Android 7.0 support multilingual. * (See update)
Instead of GB Fallback! I get always the default output. That means default.
The output from AvailableLocale is always up to date with the setup from my phone settings.
So. Why I got default from getString(R.string.test). Do I miss understand something with the multilingual stuff from the Android docs?
* Update
Why I claim I got GB Fallback!. The reason is the multilingual feature. The documentation shows a nice table:
I will try it to map it to my situation:
(Sorry, my Image is wrong. I don't mean de-GB. It's en-GB of course)
So, what is wrong here?

make multi language android application

I created multi language (English, Russian, Uzbek) app. I put 4 string resoureses in 4 folders (values, values-en, values-ru, values-uz) as docs. When I change app language updates resourses configuration in App Controller like below:
Settings.LANGUAGE = prefs.getString(User.LANG, Settings.RUSSIAN);
Locale locale = new Locale(Settings.LANGUAGE);
Locale.setDefault(locale);
Configuration configuration = new Configuration();
configuration.locale = locale;
getBaseContext().getResources().updateConfiguration(configuration,
getBaseContext().getResources().getDisplayMetrics());
After that App restarts by calling App controller's method like below:
public void reStart() {
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
After them It works well almost all devises. But on Samsung Galaxy S6 (SM-G920F), it works as crazy. Some words are in english and others are in Uzbek and ets.
So, How to fix this error? isn't the concepts of "Supporting Different Languages" supported by (applicable to) all devices?
By the way, I have checked that all resources are given in corresponding languages (as shown in attached image):
From my observations, weird behaviour was affecting only Activity titles, and I found that I was setting translations of activity titles in Manifest file. Only these translations were misbehaving. All other dynamically set translations were working fine.
So, to fix the problem, I removed all activity labels from Manifest file, then set activity titles in onCreate method as below:
getSupportActionBar().setTitle(R.string.title_activity_followers);
Problem solved.

How to include 2 variants of Serbian Language? with Latin letters and with Cyrillic letters

I have an android app and I want to translate to Serbian and I want both variants of the language: with Latin letters and with Cyrillic letters.
I tried this variants: value-sr-rRS-Latn , value-sr-Latn , value-sr-rRS-Cyrl , value-sr-Cyrl
but not of that is working.
I get this error: android-apt-compiler: [NAMEOFAPP] invalid resource directory name: [path]\res/value-sr-rRS-Latn
On Android documentation about res dirs and Locale I can't find this option.
Can I make 2 dirs with 2 variants of the language? And how?
Thank you
Since Android 7.0, Serbian with Latin script has officially been included. values-sr is still used for the Cyrillic script, and values-b+sr+Latn is used for the Latin script.
values-sr for Cyrillic
values-b+sr+Latn for Latin
I just tested Android localization and I found out that you can use any arbitrary region and it will work.
Add a folder to the project with name like values-sr-rZZ where ZZ is a fictitious region which never existed.
Then add the following code to the Application class, I got it from here and slightly changed:
public class MainApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
Resources res = this.getResources();
Configuration conf = res.getConfiguration();
boolean isLatinAlphabet = PreferenceManager.getDefaultSharedPreferences(this)... // get a value from the application settings
if(conf.locale.getLanguage().equals("sr") && isLatinAlphabet) {
conf.locale = new Locale("sr", "ZZ");
res.updateConfiguration(conf, res.getDisplayMetrics());
}
}
}
In this code the locale will be changed only if the user has chosen the serbian language as the default (conf.locale.getLanguage().equals("sr")) and also checked some checkbox in the app preferences (isLatinAlphabet).
You can use a different condition and change it as you like.
Also such dynamic way of changing language can have bugs with menu items on older devices, but it isn't reproduced on newer devices.

How to make a switch for locales using Appaccelerator?

I have an iPhone/ Android app built using Appaccelerator. I have translated all text by placing it into locales (i18n//strings.xml) ,and I have also implemented a switch :
<label for="flip-1" class="username">Language:</label>
<select name="flip-1" id="flip-1" data-role="slider">
<option value="off">EN</option>
<option value="on">NL</option>
</select>
which works reasonably well on Android and iPhone.
How can I enable this select element to update active language locale within the app, so the language of the application would change, and is this possible without restarting the app?
There is no way to change current locale in runtime. Locale depends on current language of a cell phone, but you can still do some work around.
Create strings.xml file in your i18n directory and put all your texts there like this:
<resources>
<string name="en_car">Car</string>
<string name="de_car">Auto</string>
</resources>
The you can implement your own L() function:
Ti.App.defaultLang = "en"; // global variable with default language (set it in app.js)
function myL(str, lang) {
if(lang && str) {
return L(lang + '_' + str);
} else {
return L(Ti.App.defaultLang + '_' + str);
}
}
I haven't test it, just want to give you some example to deal with it. You can maybe pass also hint option like in Ti docs: http://docs.appcelerator.com/titanium/2.1/index.html#!/api/Titanium.Locale-method-getString

Categories

Resources