问题
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.
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;
}
}
回答1:
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.
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.
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)
来源:https://stackoverflow.com/questions/57998708/runtime-localization-multi-language-not-working-on-some-devices-ex-oppo-f9-an