代码之家  ›  专栏  ›  技术社区  ›  Naveen Kumar M

如何在android中获取其他语言的字符串值?[副本]

  •  0
  • Naveen Kumar M  · 技术社区  · 6 年前

    基本上我需要的是一个函数 getString(int id, String locale) 而不是 getString(int id)

    谢谢

    0 回复  |  直到 13 年前
        1
  •  146
  •   Carsten Hagemann    5 年前

    注意

    注意 如果您使用的是应用程序包,则需要确保禁用语言拆分或动态安装不同的语言。看到了吗 https://stackoverflow.com/a/51054393

    如果您有不同地区的不同res文件夹,可以执行以下操作:

    Configuration conf = getResources().getConfiguration();
    conf.locale = new Locale("pl");
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    Resources resources = new Resources(getAssets(), metrics, conf);
    String str = resources.getString(id);
    

    注意 呼叫 Resources 像这样的构造函数在Android内部改变了一些东西。您必须使用原始语言环境调用构造函数才能恢复原来的状态。

    编辑

    Resources res = getResources();
    Configuration conf = res.getConfiguration();
    Locale savedLocale = conf.locale;
    conf.locale = desiredLocale; // whatever you want here
    res.updateConfiguration(conf, null); // second arg null means don't change
    
    // retrieve resources from desired locale
    String str = res.getString(id);
    
    // restore original locale
    conf.locale = savedLocale;
    res.updateConfiguration(conf, null);
    

    从API级别17开始,您应该使用 conf.setLocale() 而不是直接设置 conf.locale . 如果您碰巧在从右向左和从左向右的区域设置之间切换,这将正确地更新配置的布局方向。(17年引入了布局方向。)

    创建一个新的 Configuration updateConfiguration 将更改通过调用 res.getConfiguration() .

    getString(int id, String locale) 方法,如果要为一个区域设置加载多个字符串资源。更改区域设置(使用任何一种方法)都需要框架重新绑定所有资源。最好只更新一次区域设置,检索所需的所有内容,然后重新设置区域设置。

    编辑 (感谢@Mygod):

    this answer 在另一个线程上。例如,可以创建多个 Resource 对象,每个区域设置对应一个,具有:

    @NonNull Resources getLocalizedResources(Context context, Locale desiredLocale) {
        Configuration conf = context.getResources().getConfiguration();
        conf = new Configuration(conf);
        conf.setLocale(desiredLocale);
        Context localizedContext = context.createConfigurationContext(conf);
        return localizedContext.getResources();
    }
    

    然后从本地化的 资源 此方法返回的对象。一旦检索到资源,就不需要重置任何内容。

        2
  •  32
  •   Johnson_145    7 年前

    下面是 Ted Hopp . 这样,代码适用于任何Android版本:

    public static String getLocaleStringResource(Locale requestedLocale, int resourceId, Context context) {
        String result;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { // use latest api
            Configuration config = new Configuration(context.getResources().getConfiguration());
            config.setLocale(requestedLocale);
            result = context.createConfigurationContext(config).getText(resourceId).toString();
        }
        else { // support older android versions
            Resources resources = context.getResources();
            Configuration conf = resources.getConfiguration();
            Locale savedLocale = conf.locale;
            conf.locale = requestedLocale;
            resources.updateConfiguration(conf, null);
    
            // retrieve resources from desired locale
            result = resources.getString(resourceId);
    
            // restore original locale
            conf.locale = savedLocale;
            resources.updateConfiguration(conf, null);
        }
    
        return result;
    }
    

    用法示例:

    String englishName = getLocaleStringResource(new Locale("en"), R.string.name, context);
    

    正如在最初的答案中已经指出的,用一次配置更改和多次调用替换上述代码的多次调用可能更有效资源.getString()个电话。

        3
  •  2
  •   Amr Jyniat    5 年前

    基于以上的答案,这是可怕的,但有点复杂。

    public String getResStringLanguage(int id, String lang){
        //Get default locale to back it
        Resources res = getResources();
        Configuration conf = res.getConfiguration();
        Locale savedLocale = conf.locale;
        //Retrieve resources from desired locale
        Configuration confAr = getResources().getConfiguration();
        confAr.locale = new Locale(lang);
        DisplayMetrics metrics = new DisplayMetrics();
        Resources resources = new Resources(getAssets(), metrics, confAr);
        //Get string which you want
        String string = resources.getString(id);
        //Restore default locale
        conf.locale = savedLocale;
        res.updateConfiguration(conf, null);
        //return the string that you want
        return string;
    }
    

    那就叫它: String str = getResStringLanguage(R.string.any_string, "en");

    快乐代码:)