运行时本地化(多语言)在某些设备上不起作用,例如. Oppo F9和华为y9 [英] RunTime Localization (Multi-language) not working on some devices ex. Oppo f9 and Huawei y9

查看:250
本文介绍了运行时本地化(多语言)在某些设备上不起作用,例如. Oppo F9和华为y9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了以下本地化逻辑,以便能够将应用程序的语言从英语动态更改为阿拉伯语,反之亦然.它适用于从5.1到9.0的所有仿真器版本,以及我的所有7种物理设备.

I implemented the following Localization logic in order to be able to change the language of my app dynamically from English to Arabic and vice-versa. It is working on all emulator versions from 5.1 up till 9.0, and on all of my 7 physical devices.

问题是,我收到用户的抱怨,特别是使用Oppo f9和Huawei y9(2019)等设备的用户抱怨说,当尝试更改语言时,布局方向会更改,但是该应用程序却没有使用其他字符串资源.它总是英文!

The problem is that i get complains from users specifically using devices like Oppo f9 and Huawei y9 (2019) that when attempting to change the language, the layout direction changes, however the app doesn't use the other string resources. It is always on English!

我对此一无所知,是否想念一些东西?

I am losing my mind on this, am I missing something?

public class App extends Application {

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(LocaleManager.setLocale(base));
}

//Handles screen rotation only up till API 28 pie
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    LocaleManager.setLocale(this);
    if (Build.VERSION.SDK_INT >= 26) {
        ProcessPhoenix.triggerRebirth(this); //Restart app!
    }
  }
}


public class BaseActivity extends AppCompatActivity {

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (Manager.isFirstLaunch(this)) {  //Check if it is the first time to launch the app
        startActivity(new Intent(this, ChooseLanguageActivity.class));
        finish();
    } else {
        if (Build.VERSION.SDK_INT >= 26) {
            LocaleManager.setLocale(this);
        }
        startActivity(new Intent(this, AuthenticationActivity.class));
        finish();
    }
  }
}


public class ChooseLanguageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_choose_language);
    Crashlytics.log("ChooseLanguageActivity created");
}

public void EnglishButton(View view) {
    LocaleManager.persistLanguage(this, "en");  //In case the mobile locale is Arabic for API >=Oreo
    LocaleManager.setLocale(this);
    Manager.firstLaunchCompleted(this);
    ProcessPhoenix.triggerRebirth(this); //Restart app!
    finish();
}

public void ArabicButton(View view) {
    LocaleManager.persistLanguage(this, "ar");
    LocaleManager.setLocale(this);
    Manager.firstLaunchCompleted(this);
    ProcessPhoenix.triggerRebirth(this); //Restart app!
    finish();
  }
}


public class LocaleManager {
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
private static final String DEFAULT_LANGUAGE = "en";

public static Context setLocale(Context c) {
    return setNewLocale(c, getLanguage(c));
}

public static Context setNewLocale(Context c, String language) {
    persistLanguage(c, language);
    return updateResources(c, language);
}

public static String getLanguage(Context c) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    return preferences.getString(SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
}

public static void persistLanguage(Context c, String language) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(c);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(SELECTED_LANGUAGE, language);
    editor.commit(); //U must use Commit and not apply, to avoid opening a new thread, causing a delayed writting in a separate thread!
    Manager.appLanguage=language;//edited upon lang selection screen/future app launches/lang change through menu
}


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

    Resources res = context.getResources();
    Configuration config = new Configuration(res.getConfiguration());

    if(Build.VERSION.SDK_INT >= 17) {
        config.setLocale(locale);
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

    return context;
}

}

推荐答案

我能够通过生成签名的APK (就像在签名包存在之前一样)来解决此问题,然后将其上传到Google Play.

I was able to solve this issue by generating a signed APK (as I usually did before signed bundles existed), then uploading it to google play.

在某些设备上,当他们从Google Play下载该应用程序时,使用生成已签名的捆绑包会导致此问题,而其他设备则没有问题,因此令人困惑.

Using the Generate signed bundle was causing this issue on some devices when they download the app from google play, while others had no problem so it was confusing.

我的应用程序大小增加了3MB,但至少当用户从google play(我的应用程序和抱怨有问题的用户的设备)上下载该应用程序时,它在所有设备上都能正常工作

My app size got somehow larger by an extra 3MB, but at least it is working properly on all devices right now when users download it from google play (Mine and on the devices of users that complained to have problems)

这篇关于运行时本地化(多语言)在某些设备上不起作用,例如. Oppo F9和华为y9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆