英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论