对不起,英语不好。
我正在编写(或查找,复制)一些代码以更改应用程序语言。当我通过android studio进行构建时可以使用。但是,当我将其推入Google Play商店后,代码失败了!
我已经非常完整地记录了它,并且95%的人确定我的代码中没有错误。
第一部分是从stackoverflow复制的。
Renderer
第二部分是我的使用方式。
package com.my.package;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.Log;
import java.util.Locale;
/**
* Created by devdeeds.com on 18/4/17.
* by Jayakrishnan P.M
*/
public class LocaleHelper {
private static final String DEFAULT_LANGUAGE = "en";
private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";
public static String getLanguage(Context context) {
Log.i("lang", "getLanguage");
String lang = getPersistedData(context);
Log.i("lang", "getLanguage, lang = "+lang);
return lang;
}
public static Context setPrevLanguage(Context context) {
Log.i("lang", "setPrevLanguage");
String lang = getLanguage(context);
Log.i("lang", "setPrevLanguage, prev lang = "+lang);
return _setLang(context, lang);
}
public static Context setLanguage(Context context, String language) {
Log.i("lang", "setLanguage");
getPersistedData(context);
persist(context, language);
getPersistedData(context);
return _setLang(context, language);
}
private static Context _setLang(Context context, String lang) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return updateResources(context, lang);
}
return updateResourcesLegacy(context, lang);
}
private static String getPersistedData(Context context) {
Log.i("lang", "getPersistedData");
Log.i("lang", "getPersistedData, applic ID = "+BuildConfig.APPLICATION_ID);
SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Activity.MODE_PRIVATE);
String lang = prefs.getString(SELECTED_LANGUAGE, DEFAULT_LANGUAGE);
Log.i("lang", "getPersistedData, lang = "+lang);
return lang;
}
private static void persist(Context context, String language) {
SharedPreferences prefs = context.getSharedPreferences(BuildConfig.APPLICATION_ID, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}
@TargetApi(Build.VERSION_CODES.N)
private static Context updateResources(Context context, String language) {
Resources resources = context.getResources();
Configuration activityConf = resources.getConfiguration();
Locale newLocale = new Locale(language);
activityConf.setLocale(newLocale);
resources.updateConfiguration(activityConf, resources.getDisplayMetrics());
Resources applicationRes = context.getApplicationContext().getResources();
Configuration applicationConf = applicationRes.getConfiguration();
applicationConf.setLocale(newLocale);
applicationRes.updateConfiguration(applicationConf, applicationRes.getDisplayMetrics());
return context.createConfigurationContext(activityConf);
}
@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());
return context;
}
}
运行时
//when changing app language based on previous setting
LocaleHelper.setPrevLanguage(StartActivity.this);
LocaleHelper.setPrevLanguage((HomeActivity)this);
//when setting app language through Setting/Language
String lang = "fil"; //its an app for fillipino
LocaleHelper.setLanguage(getContext(), lang);
,它可以正确记录调试(通过android studio构建)和播放存储版本。 但是只有调试版本才能很好地更改应用程序版本,而Play商店版本则不会。
是否有人也遇到过相同的问题,不同的代码在调试和Play商店版本中也可以工作吗?
答案 0 :(得分:0)
这只是一个猜测。如果您使用的是Android App Bundle,则会拆分您的应用以减小下载大小。它执行此操作的方法之一是仅提供电话上使用的语言环境。它检测用户何时通过操作系统更改语言环境并下载额外的字符串。但是当您使用代码时,它可能不会发生。
要查看是否存在此问题,可以尝试使用APK而不是捆绑软件,或者不使用enable splits。