代码之家  ›  专栏  ›  技术社区  ›  SHIDHIN TS

将应用程序中的区域设置自动更改为英语

  •  0
  • SHIDHIN TS  · 技术社区  · 3 年前

    I综合区域设置( en ar )根据我的申请。一旦我将应用程序更改为阿拉伯语( 应收账 )然后关闭并打开我的应用程序,它显示布局方向显示为阿拉伯语,但字符串正在加载英语。

    这是我在点击按钮时更改语言的代码,

    public static void setLocale(final Context ctx, final String lang) {
        Log.d(TAG, "##Changing Language to: " + lang);
        AppSettings.getInstance(ctx).save(PrefKeys.language, lang);
        final Locale loc = new Locale(lang);
     
        Resources resources = ctx.getResources();
        Configuration configuration = resources.getConfiguration();
        DisplayMetrics displayMetrics = resources.getDisplayMetrics();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
            configuration.setLocale(loc);
        else
            configuration.locale = loc;
    
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N)
            ctx.createConfigurationContext(configuration);
         else
            resources.updateConfiguration(configuration, displayMetrics);
    }
    

    同样在Application类上,我添加了应用该语言的代码,这是Application类上的代码

     @Override
    public void onCreate() {
        super.onCreate();
        String lanuage = AppSettings.getInstance(getApplicationContext()).getLanguage();
        setLocale(new Locale(lanuage));
    }
    
    
    private void setLocale(Locale locale){
            Log.d(TAG,  "##Changing Language to: " + locale.getLanguage());
            Resources resources = getResources();
            Configuration configuration = resources.getConfiguration();
            DisplayMetrics displayMetrics = resources.getDisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1){
                configuration.setLocale(locale);
            } else{
                configuration.locale=locale;
            }
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N){
                getApplicationContext().createConfigurationContext(configuration);
            } else {
                resources.updateConfiguration(configuration,displayMetrics);
            }
        }
    
    0 回复  |  直到 3 年前
        1
  •  0
  •   Anwar Elsayed    3 年前

    创建一个基础活动并用您的活动扩展它,然后在基础活动上添加语言更改,怎么样

    如下所示

    @SuppressLint("Registered")
    open class BaseActivity : AppCompatActivity() {
    
    
    companion object {
        var lang: String? = null
    }
    
    
    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(localeManager!!.setLocale(base!!))
    
    }
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // AndroidThreeTen.init(this)
      Utility.resetActivityTitle(this)
    }
    

    }

        2
  •  0
  •   SHIDHIN TS    3 年前

    我创建了新 BaseActivity.class 并将该类扩展到主屏幕,

    public abstract class BaseActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(getLayoutResourceId());
        }
    
        protected abstract int getLayoutResourceId();
    
    
        @Override
        protected void attachBaseContext(Context newBase) {
            String language = AppSettings.getInstance(newBase).getLanguage();
            super.attachBaseContext(LocaleHelper.wrap(newBase, language));
        }
    
    
        private static class LocaleHelper extends ContextWrapper {
    
            public LocaleHelper(Context base) {
                super(base);
            }
    
            public static ContextWrapper wrap(Context context, String language) {
                if (TextUtils.isEmpty(language.trim())) {
                    return new LocaleHelper(context);
                }
                Configuration config = context.getResources().getConfiguration();
                Locale locale = new Locale(language);
                Locale.setDefault(locale);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    config.setLocale(locale);
                } else {
                    //noinspection deprecation
                    config.locale = locale;
                }
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    config.setLayoutDirection(locale);
                    context = context.createConfigurationContext(config);
                } else {
                    //noinspection deprecation
                    context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
                }
                return new LocaleHelper(context);
            }
    
        } // LocaleHelper
    
    }
    

    这是HomeActivity上的代码修改,

    public class HomeActivity extends BaseActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_home);
            ButterKnife.bind(this);
        }
    
        @Override
        protected int getLayoutResourceId() {
            return R.layout.activity_home;
        }
    
    
    }