Update Locale not changing the Locale for Android version lower than Build.VERSION_CODES.N (API 24)

huangapple go评论66阅读模式
英文:

Update Locale not changing the Locale for Android version lower than Build.VERSION_CODES.N (API 24)

问题

在你提供的代码中,有一些 HTML 转义字符,我会为你提供不带转义字符的代码翻译:

for a few days I have been struggling with changing the language in the application, it seems like it's still good but if the API is below 24 (VERSION_CODES.N) it doesn't want to update Locale. I found the solution here: https://gunhansancar.com/change-language-programmatically-in-android/
I tried to implement it, API from 24 onwards works well but API 23, 22, 21, 19 .. do not update Locale. Have an 4 string.xml (four language) and:
>**LocaleHelper.java**
```java
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 Context setLocale(Context context, String lang){
        persist(context, lang);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
            return updateResources(context, lang);
        return  updateResourceLegacy(context, lang);
    }

    @TargetApi ( Build.VERSION_CODES.N )
    private static Context updateResources (Context context, String lang) {
        Locale locale = new Locale ( lang );
        Locale.setDefault ( locale );
        Configuration config = context.getResources ().getConfiguration ();
        config.setLocale ( locale );
        config.setLayoutDirection ( locale );

        return context.createConfigurationContext ( config );
    }

    private static Context updateResourceLegacy (Context context, String lang) {
        Locale locale = new Locale ( lang );
        Locale.setDefault ( locale );
        Resources resources = context.getResources ();
        Configuration config = resources.getConfiguration ();
        config.locale = locale;
        config.setLayoutDirection ( locale );
        resources.updateConfiguration ( config, resources.getDisplayMetrics () );
        return context;
    }

    private  static void persist (Context context, String lang){
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences ( context );
        SharedPreferences.Editor editor = pref.edit();
        editor.putString ( SELECTED_LANGUAGE, lang );
        editor.apply ();
    }

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

MainAplication.java

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

MainActivity.java

@Override
protected void attachBaseContext (Context newBase) {
    super.attachBaseContext ( LocaleHelper.onAttach ( newBase, "en" ) );
}

private void updateView (String lang) {
    Context context = LocaleHelper.setLocale(this, lang);
    context.getResources ();
}

@Override
public boolean onMenuItemClick (MenuItem menuItem) {
    click_efx.start ();
    switch (menuItem.getItemId ()){
        case R.id.ro_lang:
            updateView("ro");
            recreate ();
            return true;
        case R.id.eng_lang:
            updateView("en");
            recreate ();
            return true;
        case R.id.ru_lang:
            updateView("ru");
            recreate ();
            return true;
        case R.id.fr_lang:
            updateView("fr");
            recreate ();
            return true;
        default:
            return false;
    }
}

AndroidManifest.xml

android:name=".MainApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="@style/NewStyle"
android:usesCleartextTraffic="true"
android:windowSoftInputMode="adjustResize"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"

希望这可以帮助你找到问题,如果还有其他问题,请随时提问。

英文:

for a few days I have been struggling with changing the language in the application, it seems like it's still good but if the API is below 24 (VERSION_CODES.N) it doesn't want to update Locale. I found the solution here: https://gunhansancar.com/change-language-programmatically-in-android/
I tried to implement it, API from 24 onwards works well but API 23, 22, 21, 19 .. do not update Locale. Have an 4 string.xml (four language) and:
>LocaleHelper.java

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 Context setLocale(Context context, String lang){
persist(context, lang);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
return updateResources(context, lang);
return  updateResourceLegacy(context, lang);
}
@TargetApi ( Build.VERSION_CODES.N )
private static Context updateResources (Context context, String lang) {
Locale locale = new Locale ( lang );
Locale.setDefault ( locale );
Configuration config = context.getResources ().getConfiguration ();
config.setLocale ( locale );
config.setLayoutDirection ( locale );
return context.createConfigurationContext ( config );
}
private static Context updateResourceLegacy (Context context, String lang) {
Locale locale = new Locale ( lang );
Locale.setDefault ( locale );
Resources resources = context.getResources ();
Configuration config = resources.getConfiguration ();
config.locale = locale;
config.setLayoutDirection ( locale );
resources.updateConfiguration ( config, resources.getDisplayMetrics () );
return context;
}
private  static void persist (Context context, String lang){
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences ( context );
SharedPreferences.Editor editor = pref.edit();
editor.putString ( SELECTED_LANGUAGE, lang );
editor.apply ();
}
private static String getPersistedData(Context context, String language){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences ( context );
return  preferences.getString ( SELECTED_LANGUAGE, language );
}
}

MainAplication.java

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

MainActivity.java

    @Override
protected void attachBaseContext (Context newBase) {
super.attachBaseContext ( LocaleHelper.onAttach ( newBase, "en" ) );
}
private void updateView (String lang) {
Context context = LocaleHelper.setLocale(this, lang);
context.getResources ();
}
@Override
public boolean onMenuItemClick (MenuItem menuItem) {
click_efx.start ();
switch (menuItem.getItemId ()){
case R.id.ro_lang:
updateView("ro");
recreate ();
return true;
case R.id.eng_lang:
updateView("en");
recreate ();
return true;
case R.id.ru_lang:
updateView("ru");
recreate ();
return true;
case R.id.fr_lang:
updateView("fr");
recreate ();
return true;
default:
return false;
}
}

AndroidManifest.xml

 android:name=".MainApplication"
android:allowBackup="true"
android:fullBackupContent="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true"
android:theme="@style/NewStyle"
android:usesCleartextTraffic="true"
android:windowSoftInputMode="adjustResize"
tools:ignore="AllowBackup,GoogleAppIndexingWarning,UnusedAttribute"

I guess there's an escape in LocaleHelper.java but I can't figure out where ... Help me please, Thanks in advance!

答案1

得分: 2

代码正确且功能正常,一切都很简单。我创建了没有语言区域的新字符串文件,一切都自然而然地解决了。Update Locale not changing the Locale for Android version lower than Build.VERSION_CODES.N (API 24)

英文:

So the code is correct and functional, everything was simple. I created new string files without language regions, and everything worked itself out.Update Locale not changing the Locale for Android version lower than Build.VERSION_CODES.N (API 24)

huangapple
  • 本文由 发表于 2020年8月8日 21:39:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316051.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定