Android的:如何得到字符串中的特定语言环境不改变当前的语言环境 [英] Android: How to get string in specific locale WITHOUT changing the current locale

查看:1716
本文介绍了Android的:如何得到字符串中的特定语言环境不改变当前的语言环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用案例:作为向用户显示记录的错误信息

Use Case: Logging error messages as displayed to the user.

但是你不希望有依赖于用户的设备的语言环境中的日志信息。 在另一方面,你不想改变用户的设备的语言环境只为你的(技术)记录。 才能实现这一目标? 我发现一对夫妇可能的解决方案在这里计算器:

However you don't want to have messages in your log that depend on the locale of the user's device. On the other hand you don't want to change the locale of the user's device just for your (technical) logging. Can this be achieved? I found a couple of potential solutions here on stackoverflow:

  • <一个href="http://stackoverflow.com/questions/9475589/how-to-get-string-from-different-locales-in-android">how让来自不同区域设置字符串中的Andr​​oid?
  • <一个href="http://stackoverflow.com/questions/10874589/can-i-access-to-resources-from-different-locale-android">Can我从不同的语言环境的Andr​​oid获得资源?
  • <一个href="http://stackoverflow.com/questions/6526201/get-string-from-default-locale-using-string-in-specific-locale">android - 使用字符串中的特定区域,从默认的语言环境获取字符串
  • 甚至机器人 - 更改区域设置的应用程序本身
  • how to get string from different locales in Android?
  • Can I access to resources from different locale android?
  • android - Get string from default locale using string in specific locale
  • maybe even android - Changing Locale within the app itself

不过,他们都因此改变我的设备的区域(直到下一次配置更改)。

However, they all result in changing the locale of my device (until the next configuration change).

不管怎样,我目前的解决办法是,像这样:

Anyway, my current workaround is like so:

public String getStringInDefaultLocale(int resId) {
    Resources currentResources = getResources();
    AssetManager assets = currentResources.getAssets();
    DisplayMetrics metrics = currentResources.getDisplayMetrics();
    Configuration config = new Configuration(
            currentResources.getConfiguration());
    config.locale = DEFAULT_LOCALE;
    /*
     * Note: This (temporiarily) changes the devices locale! TODO find a
     * better way to get the string in the specific locale
     */
    Resources defaultLocaleResources = new Resources(assets, metrics,
            config);
    String string = defaultLocaleResources.getString(resId);
    // Restore device-specific locale
    new Resources(assets, metrics, currentResources.getConfiguration());
    return string;
}

说实话,我不喜欢这种方式的。它的效率不高, - 考虑并发性和一切 - 它可能会导致错误的区域设置一些看法

To be honest, I don't like this approach at all. It's not efficient and - thinking about concurrency and all that - it might result in some view in the "wrong" locale.

所以 - 任何想法?也许这可以通过实现资源包 S,就像在标准的Java?

So - Any ideas? Maybe this could be achieved using ResourceBundles, just like in standard Java?

推荐答案

您可以保存默认语言环境的所有字符串在地图全局类里面,像应用程序启动应用程序时执行:

You can save all the strings of the default locale in a Map inside a global class, like Application that is executed when launching the app:

public class DualLocaleApplication extends Application {

    private static Map<Integer, String> defaultLocaleString;

    public void onCreate() {
        super.onCreate();
        Resources currentResources = getResources();
        AssetManager assets = currentResources.getAssets();
        DisplayMetrics metrics = currentResources.getDisplayMetrics();
        Configuration config = new Configuration(
                currentResources.getConfiguration());
        config.locale = Locale.ENGLISH;
        new Resources(assets, metrics, config);
        defaultLocaleString = new HashMap<Integer, String>();
        Class<?> stringResources = R.string.class;
        for (Field field : stringResources.getFields()) {
            String packageName = getPackageName();
            int resId = getResources().getIdentifier(field.getName(), "string", packageName);
            defaultLocaleString.put(resId, getString(resId));
        }
        // Restore device-specific locale
        new Resources(assets, metrics, currentResources.getConfiguration());
    }

    public static String getStringInDefaultLocale(int resId) {
        return defaultLocaleString.get(resId);
    }

}

这个解决方案不是最优的,但你不会有并发问题。

This solution is not optimal, but you won't have concurrency problems.

这篇关于Android的:如何得到字符串中的特定语言环境不改变当前的语言环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆