【问题标题】:The locale is not updated in my library for android app我的 android 应用程序库中的语言环境未更新
【发布时间】:2014-11-21 07:26:03
【问题描述】:

我有一个项目,它具有核心和库

我在核心项目的运行时更改了我的语言环境。

问题是,在库项目中有一个

Locale.getDefault(); 

它只返回设备的语言环境,而不是应用程序的语言环境。这意味着,当我在我的应用程序中将语言环境更改为法语时,如果我的设备使用英语,则库项目获取的语言环境仍然是英语。如何解决?谢谢

【问题讨论】:

    标签: android android-activity sharedpreferences locale setlocale


    【解决方案1】:

    您可以更改默认语言环境。

    你可以试试下面的代码:

    Locale locale = new Locale("fr"); //if you want to change to French
    Locale.setDefault(locale);
    Configuration config = new Configuration();
    config.locale = locale;
    getBaseContext().getResources().updateConfiguration(config,
          getBaseContext().getResources().getDisplayMetrics());
    

    或者制作一个区域设置方法:

     public void setLocale(String newLocale) {
        Locale locale = new Locale(newLocale);
        Locale.setDefault(locale);
    
        Configuration config = new Configuration();
        config.locale = locale;
    
        context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics() );
     }
    

    【讨论】:

    • 区域设置 locale = new Locale(newLocale); Locale.setDefault(locale);解决问题,谢谢
    猜你喜欢
    • 2018-08-09
    • 2011-03-24
    • 1970-01-01
    • 2018-04-15
    • 2013-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 2014-04-19
    相关资源
    最近更新 更多