Rohit
Rohit

Reputation: 1001

Localization - Only few strings are converted to Hindi when performing localization in Android App

I have a method in base activity called setLanguage(Context context, String language).It takes context of the activity and the language string in which the localization has to be done.The problem is that only android system's hindi strings are being displayed and not of strings.xml(hi). For example i am able to see the translation of monday - सोमवार but not of any other strings i have defined in strings.xml(hi).

public final void setLanguage(Context context, String language) {
        Locale locale = new Locale(language);
        setLanguage(context, locale);
}


public final void setLanguage(Context context, Locale locale) {       
        LanguageSetting.setLanguage(activity, locale);
        notifyLanguageChanged();
}


public class LanguageSetting {
private static final String PREFERENCE_LANGUAGE = "pref_language";
private static final String KEY_LANGUAGE = "key_language";
private static Locale DEFAULT_LANGUAGE = Locale.ENGLISH;

public static void setDefaultLanguage(Locale locale) {
    DEFAULT_LANGUAGE = locale;
}

public static Locale getDefaultLanguage() {
    return DEFAULT_LANGUAGE;
}

public static void setLanguage(Context context, Locale locale) {
    Locale.setDefault(locale);
    SharedPreferences.Editor editor = getLanguagePreference(context).edit();
    editor.putString(KEY_LANGUAGE, locale.toString());
    editor.apply();
}

public static Locale getLanguage(Context context) {
    String[] language = getLanguagePreference(context)
            .getString(KEY_LANGUAGE, DEFAULT_LANGUAGE.toString())
            .split("_");
    Locale locale;
    if (language.length == 1) {
        locale = new Locale(language[0]);
    } else if (language.length == 2) {
        locale = new Locale(language[0], language[1].toUpperCase());
    } else {
        locale = DEFAULT_LANGUAGE;
    }
    return locale;
}

private static SharedPreferences getLanguagePreference(Context context) {
    return context.getSharedPreferences(PREFERENCE_LANGUAGE, Activity.MODE_PRIVATE);
}
}

Upvotes: 0

Views: 354

Answers (2)

Rohit
Rohit

Reputation: 1001

Finally found the answer.I had hard coded the locale in the gradle file.

        android { 
   defaultConfig {
        ...
        resConfigs "en" 
        ...
   }
}

I just needed to remove resConfigs "en"

Upvotes: 1

jakepurple13
jakepurple13

Reputation: 151

I think this link might have the answer your looking for.

https://stackoverflow.com/a/2900144/2781626

Here's the code from the post if your in a hurry and just need the code and you plan on checking the link out at another time to learn more why this code might be your solution. I believe you can keep some/a lot of your current code, but you might need to change some of it around in order to do an app-wide locale change.

`Resources res = context.getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.setLocale(new Locale(language_code.toLowerCase())); // API 17+ only.
// Use conf.locale = new Locale(...) if targeting lower versions
res.updateConfiguration(conf, dm);`

Upvotes: 0

Related Questions