2017-11-20 17 views
0

私は適切なフォルダにあるいくつかのlanguage.Stringファイルをサポートするアンドロイドプロジェクトを持っています。たとえば、フランス語のファイルstringは、 'values-fr'というフォルダにあります。ランタイムで私は言語を変更したい。私はあまりAndroid7その後、クラスLocaleHelper Android_Version上Android 7ローカリゼーションの問題

public class LocaleHelper { 

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.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, final String language) { 
    String tmpLang = language; 
    if (language.length() > 2) { 
     tmpLang = language.substring(0, 2); 
    } 
    persist(context, tmpLang); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { 
     return updateResources(context, tmpLang); 
    } 

    return updateResourcesLegacy(context, tmpLang); 
} 

private static String getPersistedData(Context context, String defaultLanguage) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    return preferences.getString(SELECTED_LANGUAGE, defaultLanguage); 
} 

private static void persist(Context context, String language) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor editor = preferences.edit(); 

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

@TargetApi(Build.VERSION_CODES.N) 
private static Context updateResources(Context context, String language) { 
    Locale locale = new Locale(language); 
    Locale.setDefault(locale); 

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

    Resources.getSystem().updateConfiguration(configuration, context.getResources().getDisplayMetrics()); 

    return context.createConfigurationContext(configuration); 
} 

@SuppressWarnings("deprecation") 
private static Context updateResourcesLegacy(Context context, String language) { 
    Locale locale = new Locale(language); 
    Locale.setDefault(locale); 

    Resources resources = context.getResources(); 

    Configuration configuration = resources.getConfiguration(); 
    configuration.locale = locale; 

    resources.updateConfiguration(configuration, resources.getDisplayMetrics()); 

    Resources.getSystem().updateConfiguration(configuration, resources.getDisplayMetrics()); 

    return context; 
} 

public static void clearPersist(Context context) { 
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 
    SharedPreferences.Editor editor = preferences.edit(); 
    editor.remove(SELECTED_LANGUAGE); 
    editor.apply(); 
} 

}

そしてBaseActivityで

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

i 'はフランス語モード' で私のアプリを起動し

LocaleHelper.setLocale(this, "fr"); 

を持っていますすべて正常に動作しますが、Android_Version> = Android7の場合は部分的にローカライズされたアプリがあります。英語の文字列の一部とフランス語の部分。ツールバーの
P.S
タイトルは、あなたが2番目のパラメータとして国コードとLocaleクラスを初期化する必要が正しいロケール

+0

http://wptrafficanalyzer.navigation-drawer-with-icons/ – Ankita

答えて

2

を持っています。例えば。私のコード:

if (language.equals("ru") { 
    locale = new Locale(language, "RU"); 
} else if (language.equals("en")) { 
    locale = new Locale(language, "US"); 
} 
関連する問題