Search code examples
androidlocale

Locale works for some languages


The locale helper works but only for some languages. When the app starts i check for default locale and it is the correct language but the app is still in english. From 40 languages that my app uses only 3 work. I checked for missing string in the files but all the strings.xml files are the same. The wierd thing is that it worked fine and then just before publishing my app i did a final check and i get this behaviour even tho i didnt change anything since last time. My app is api 24+. This is my Locale Helper:

public class LocaleHelper {
private static final String SELECTED_LANGUAGE = "language";

public static Context onAttach(Context context) {
    String lang = getPersistedData(context, Locale.getDefault().getLanguage());
    return setLocale(context, lang);
}

public static Context onAttach(Context context, String defaultLanguage) {
    String lang = getPersistedData(context, defaultLanguage);
    return setLocale(context, lang);
}

public static String getLanguage(Context context) {
    return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static Context setLocale(Context context, String language) {
    persist(context, language);

    return updateResources(context, language);
}

private static String getPersistedData(Context context, String defaultLanguage) {
    SharedPreferences preferences = context.getSharedPreferences("DriveDriver", Context.MODE_PRIVATE);
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
    SharedPreferences preferences = context.getSharedPreferences("DriveDriver", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();

    editor.putString(SELECTED_LANGUAGE, language);
    editor.apply();
}


private static Context updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Configuration configuration = context.getResources().getConfiguration();
    configuration.setLocale(locale);
    configuration.setLayoutDirection(locale);

    return context.createConfigurationContext(configuration);
}

}

and this is the attach method on mainpage:

 @Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base));
}

and this is MyApp page:

public class MyApp extends Application {
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleHelper.onAttach(base, "en"));
}

Solution

  • It was doing that because app_name in v21 was not the same in v27 with one letter. Unreal to think that i spent a whole day for one mistaken letter and didnt get any warning or error anywhere. UPDATE: I found another user that had the same problem and it turns out that sometimes running the app directly from Android Studio stops your app from changing languages or allows you to use only some of them. Install the apk on your device and it should work.