首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RunTime本地化(多语言)不适用于某些设备。Oppo f9与华为y9

RunTime本地化(多语言)不适用于某些设备。Oppo f9与华为y9
EN

Stack Overflow用户
提问于 2019-09-18 18:18:44
回答 1查看 1.5K关注 0票数 3

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

problem是,我收到用户的抱怨,他们专门使用Oppo f9和华为y9 (2019)等设备,他们说当试图改变语言时,布局方向会发生变化,但是应用程序不使用其他字符串资源。总是写在英语上!

我在这件事上失去了理智,是不是遗漏了什么?

代码语言:javascript
复制
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!
    }
  }
}

代码语言:javascript
复制
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();
    }
  }
}

代码语言:javascript
复制
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();
  }
}

代码语言:javascript
复制
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;
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-22 10:45:31

我能够通过生成一个签名的APK来解决这个问题(就像我在签名包存在之前通常所做的那样),然后上传到google。

当一些设备从google下载应用程序时,使用生成有符号包是导致这个问题的原因,而另一些设备则没有问题,所以让人感到困惑。

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57998708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档