语言更改在奥利奥(Oreo)中不起作用

时间:2018-08-01 12:58:53

标签: android android-8.0-oreo

我在我的应用程序中使用了多种语言。在Android 8.0以下版本中,语言更改可以正常工作。在8.0中,当我更改语言时,Toast msg会转换为其他语言,但不会更改剩余的UI部分。这是下面的代码。

这是我更改应用程序语言的地方。

gc = GlobalClass.getInstance();
    lang=gc.getLaguageStatus(getApplicationContext());
    if (lang!=null && lang!="") {
        if (lang.equals("0")) {
            languageToLoad = "en";


        } else if (lang.equals("1")) {
            languageToLoad = "hi";

        }else if (lang.equals("2")) {
            languageToLoad = "gu";

        }
    }else{
        languageToLoad = "en";

    }
    gc.changelanguage(languageToLoad, getApplicationContext());

    english.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            languageToLoad = "en";
            gc.setLanguageStatus(getApplicationContext(),"0");
            gc.changelanguage(languageToLoad, getApplicationContext());
            Toast.makeText(getApplicationContext(), R.string.lang_apply, Toast.LENGTH_LONG).show();

            //setLanguage("en");
            Intent mIntent = new Intent(getApplicationContext(), MainActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent);
            finish();
        }
    });

    hindi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            languageToLoad = "hi";
            gc.setLanguageStatus(getApplicationContext(),"1");
            gc.changelanguage(languageToLoad, getApplicationContext());
            Toast.makeText(getApplicationContext(), R.string.lang_apply, Toast.LENGTH_LONG).show();
            //setLanguage("hi");
            Intent mIntent = new Intent(getApplicationContext(), MainActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent);
            finish();
        }
    });

    guj.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            languageToLoad = "gu";
            gc.setLanguageStatus(getApplicationContext(),"2");
            gc.changelanguage(languageToLoad, getApplicationContext());
            Toast.makeText(getApplicationContext(), R.string.lang_apply, Toast.LENGTH_LONG).show();
            //setLanguage("gu");
            Intent mIntent = new Intent(getApplicationContext(), MainActivity.class);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(mIntent);
            finish();
        }
    });

MainActivity。//SplashScren的代码也相同。

prefsWrapper = new PreferencesWrapper(getApplicationContext());
    lang = prefsWrapper.getPreferenceStringValue("change.language");

    gc = GlobalClass.getInstance();
    lang = gc.getLaguageStatus(getApplicationContext());

    System.out.println(TAG+"Language lang 1:"+lang);
    if (lang != null && lang != "") {
        if (lang.equals("0")) {
            languageToLoad = "en";
            gc.changelanguage(languageToLoad, getApplicationContext());
            System.out.println(TAG+"Language EN 2:"+languageToLoad);

        } else if (lang.equals("1")) {
            languageToLoad = "hi";
            gc.changelanguage(languageToLoad,getApplicationContext());
            System.out.println(TAG+"Language HI 3:"+languageToLoad);

        } else if (lang.equals("2")) {
            languageToLoad = "gu";
            gc.changelanguage(languageToLoad, getApplicationContext());
            System.out.println(TAG+"Language GU 4:"+languageToLoad);

        }
    } else {
        languageToLoad = "en";
        gc.changelanguage(languageToLoad, getApplicationContext());
        System.out.println(TAG+"Language EN 5:"+languageToLoad);
    }

全局类。

public void changelanguage(String languageToLoad, Context context) {
        System.out.println("GC "+"Change Language:"+languageToLoad);
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Resources resources = context.getResources();
        Configuration config = new Configuration(resources.getConfiguration());//resources.getConfiguration();
        context.createConfigurationContext(config);

        config.locale = locale;
        Locale.setDefault(locale);

        if (Build.VERSION.SDK_INT >= 17) {
            config.setLocale(locale);
            config.setLayoutDirection(config.locale);

        } else {
            config.locale = locale;
        }

        //context.createConfigurationContext(config);
        resources.updateConfiguration(config,resources.getDisplayMetrics());


    }

public void setLanguageStatus(Context ctx, String flag) {
        ContentValues values = new ContentValues();
        values.put(LanguageStatusProvider.Language_STATUS, flag);
        System.out.println("Language Flag GC::" + flag);

        // to check if database had already data or not
        Uri auto_uri = LanguageStatusProvider.CONTENT_URI;
        Cursor c = ctx.getContentResolver().query(auto_uri, null, null, null, "lang_status");
        System.out.println("Cursor::" + c);

        if (c != null) {
            System.out.println("This is if");
            //System.out.println("Check before insert for cusror count : "+c.getCount());
            if (c.getCount() == 0) {
                Uri uri = ctx.getContentResolver().insert(auto_uri, values);

            } else {
                c.moveToFirst();
                String selection = LanguageStatusProvider._ID + "=?";
                String[] selArgs = {c.getString(c.getColumnIndexOrThrow(LanguageStatusProvider._ID))};
                int up_id = ctx.getContentResolver().update(auto_uri, values, selection, selArgs);
                //System.out.println("updated values : "+ up_id);
            }
        } else {
            //System.out.println("Cursor is null");
        }
        //Uri uri = ctx.getContentResolver().insert(NumberRewritingProvider.CONTENT_URI, values);
    }


    public String getLaguageStatus(Context ctx) {
        Uri auto_uri = LanguageStatusProvider.CONTENT_URI;
        Cursor c = ctx.getContentResolver().query(auto_uri, null, null, null, "lang_status");
        String res = "";
        if (c != null) {
            if (c.moveToFirst()) {

                int column_index = c.getColumnIndexOrThrow("lang_status");
                res = c.getString(column_index);
                System.out.println("Result: " + res);
            }
            c.close();
        }
        return res;
    }

用于语言更改的广播接收器。

@Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("Language OnReciver " );

        GlobalClass gc = GlobalClass.getInstance();
        PreferencesWrapper prefsWrapper = new PreferencesWrapper(context);
        String sel_langague = Locale.getDefault().toString();
        String languageToLoad = "";
        System.out.println("Receiver Language selected 1:::::"+ sel_langague);

        if (sel_langague.startsWith("en")) {
            languageToLoad = "en";
            System.out.println("Receiver Language change 2"+ languageToLoad);
           // gc.setLanguageStatus(context,"0");
            prefsWrapper.setPreferenceStringValue("change.language","0");
        } else if (sel_langague.startsWith("en")){
            languageToLoad = "hi";
            System.out.println("Receiver Language change 3"+ languageToLoad);
           // gc.setLanguageStatus(context,"1");
            prefsWrapper.setPreferenceStringValue("change.language","1");
        }else {
            languageToLoad = "gu";
            System.out.println("Receiver Language change 4"+ languageToLoad);
            // gc.setLanguageStatus(context,"1");
            prefsWrapper.setPreferenceStringValue("change.language","2");
        }
        gc.changelanguage(languageToLoad, context);

    }

0 个答案:

没有答案