Change locale in android app (onto Hindi)

user4702646 picture user4702646 · Jun 13, 2015 · Viewed 9.2k times · Source

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?

Answer

Chandrakanth picture Chandrakanth · Jun 13, 2015

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