Android的:如何得到字符串中的特定语言环境不改变当前的语言环境语言、环境、字符串、不改变

2023-09-06 01:58:53 作者:名字不重要了

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

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:

how让来自不同区域设置字符串中的Andr​​oid? Can我从不同的语言环境的Andr​​oid获得资源? 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.