使用导航组件时更改语言环境

时间:2019-12-22 09:02:14

标签: android locale android-architecture-components

我在我的应用程序中使用了导航组件,我有一个使用两种语言(阿拉伯语和英语)的应用程序。 要更改语言环境,我正在使用此类:

service cloud.firestore {
  match /databases/{database}/documents {
  //allows all users to read and write, but dangerous as any one can flood your database
    match /public_collection/{document=**} {
        allow read, write: if true;
    }
//only read access
    match /public_read_collection/{document=**} {
        allow read: if true;
        allow write: if false;
    }
//prefered for storing users personal info, users can access only their data
    match /users/{userId} {
      allow read, write: if request.auth.uid == userId;
    }
//any authenticated user can access or write data
    match /posts/{documentId} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

活动中:

public class ContextWrapper extends android.content.ContextWrapper {

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

    public static ContextWrapper wrap(Context context, Locale newLocale) {

        Resources res = context.getResources();
        Configuration configuration = res.getConfiguration();

        if (Build.VERSION.SDK_INT >= 24) {
            System.out.println(newLocale.getLanguage() + "API > 24");
            configuration.setLocale(newLocale);

            LocaleList localeList = new LocaleList(newLocale);
            LocaleList.setDefault(localeList);
            configuration.setLocales(localeList);

            context = context.createConfigurationContext(configuration);

        } else if (Build.VERSION.SDK_INT >= 17) {
            configuration.setLocale(newLocale);
            context = context.createConfigurationContext(configuration);

        } else {
            System.out.println(newLocale.getLanguage() + "API other");
            configuration.locale = newLocale;
            res.updateConfiguration(configuration, res.getDisplayMetrics());
        }

        return new ContextWrapper(context);
    }
}

此代码在没有导航组件的情况下非常有效,但是对于导航组件,它不适用于<24的Android API。 我在开始的活动中使用了此代码。

我该如何解决?

0 个答案:

没有答案