Многоязычная базовая локаль - Android - PullRequest
0 голосов
/ 27 октября 2018

Мое приложение работает на нескольких языках, например:

activity_al_main.xml(en)
activity_al_main.xml(en-rUS)

И я установил, как показано ниже, activity:

View view = SetCustomLayouts.setCustomLayout(MainActivityMou.this, "en-rUS", R.layout.activity_al_main);
this.setContentView(view);

И:

public class SetCustomLayouts {
    public static View setCustomLayout(Context context, String LangID, int layout) {
        Context contexts = ContextWrapper.wrap(context, LangID);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        return Objects.requireNonNull(inflater).inflate(contexts.getResources().getLayout(layout), null);
    }
}

Нопоймите меня ниже ошибка:

android.content.res.Resources$NotFoundException: Resource ID #0x7f0d001e

Примечание: У меня нет проблем с en, sp, fr, tr .... но когдаЯ использую с en-rUS получите мне ошибку.

1 Ответ

0 голосов
/ 27 октября 2018

Решил мою проблему:

public class ContextWrapper extends android.content.ContextWrapper {

    private ContextWrapper(Context base) {
        super(base);
    }

    @SuppressWarnings("deprecation")
    public static android.content.ContextWrapper wrap(Context context, String language) {
        Configuration config = context.getResources().getConfiguration();
        if (language != null && !TextUtils.isEmpty(language)) {
            Locale locale;
            if (language.contains("-")) {
                String[] arrSplit = language.split("-r");
                String lang = arrSplit[0];
                String country = arrSplit[1];
                locale = new Locale(lang, country);
            } else {
                locale = new Locale(language);
            }
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                context = context.createConfigurationContext(config);
            } else {
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
        } else {
            Locale locale = new Locale("en");
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                context = context.createConfigurationContext(config);
            } else {
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
        }
        return new ContextWrapper(context);
    }

    @SuppressWarnings("deprecation")
    private static void setSystemLocaleLegacy(Configuration config, Locale locale) {
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)
    private static void setSystemLocale(Configuration config, Locale locale) {
        config.setLocale(locale);
    }

}
...