力的区域设置为Android风味resConfig [英] Force locale for Android flavor with resConfig

查看:3117
本文介绍了力的区域设置为Android风味resConfig的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用resConfig和resConfigs从Android编译系统。

I am trying to use the resConfig and resConfigs from the Android Build system.

  • Android的工作室版本1.2.2
  • 在摇篮内部版本1.2.3
  • 在OSX 10.10.3

我有问题,这两个选项与我的项目,所以我开始与Android工作室的一个新的空白项目。我重视我的build.gradle,我只加了resConfigsEN,FR在

I was having problem with these 2 options with my project so I started a new blank project with android studio. I attached my build.gradle where I only added resConfigs "en", "fr" under

android { 
   defaultConfig {
        ...
        resConfigs "en", "fr" 
        ...
   }
}

和定义2基本形式

productFlavors {
        fr {
            resConfig "fr"
        }

        en {
            resConfig "en"
        }
}

然后,我创建了一个2的strings.xml文件和翻译参考hello world默认标签

I then created a 2 strings.xml files and translated the hello_world default label

/src/main/res/values​​/strings.xml(默认)
  /src/main/res/values​​-en/strings.xml
  /src/main/res/values​​-fr/strings.xml

/src/main/res/values/strings.xml (default)
/src/main/res/values-en/strings.xml
/src/main/res/values-fr/strings.xml

有了这个,我希望看到在所有MyApplication /应用/建立/只3个值的文件夹中间体/ RES /恩,因为我在resConfigs定义为只使用连接和FR和过滤别的/调试

With this, I would expect to see only 3 values folders in MyApplication/app/builds/intermediates/res/en/debug since I defined in the resConfigs to only use "en" and "fr" and filter anything else

/价值/
  /值恩/
  /值-FR /

/values/
/values-en/
/values-fr/

虽然,所有的语言值的文件夹仍然存在这样resConfigs没有任何过滤明显。

Although, all languages values folder are still in there so the resConfigs is not filtering anything apparently.

我也希望看到从标签参考hello world值恩运行enDebug味道变的时候,因为我指定的味道恩会用resConfig恩虽然当我运行测试软件,我看到/标签值-FR /strings.xml而不是/values​​-en/strings.xml因为在平板电脑上的语言设置为法语加拿大

I would also expect to see the label hello_world from values-en when running enDebug flavor variant since I specified that the flavor en would use resConfig "en" although when I run the test app I see the label from /values-fr/strings.xml instead of /values-en/strings.xml since the language on the tablet is configured as French Canada

我误解背后resConfig的目的是什么?如果是这样,我怎么强制的味道只在唯一的语言运行,不依赖于操作系统区域设置?因为我用他们在真正的应用程序,我想配置这个解决方案必须与flavorDimensions兼容了。

Am I misunderstanding the purpose behind resConfig? If so, how am I supposed to force a flavor to only run in only language and not be dependant on the OS locale setting? This solution must be compatible with flavorDimensions too since I use them in the real app I'm trying to configure.

注意:我也提出一个错误,因为我觉得真的有这个resConfig行为的 HTTPS://$c$c.google.com/p/android/issues/detail ID = 180694

NOTE: I also filed a bug since I think there really is a problem with this resConfig behavior https://code.google.com/p/android/issues/detail?id=180694

感谢

推荐答案

好了,几个小时后,这里的解决我的问题,对于那些结束了同样的问题。

Well, after many hours, here's the solution to my problem for those ending up with the same problem.

原来,resConfig不是我所虽然。正确的,只有这样,我发现给力的语言环境特定的味道是这样的:

Turns out that resConfig is not for what I though. The proper and only way I found to force the locale for a specific flavor is like this:

首先,确保你所有的活动,扩展了其将负责强制语言环境的共同活动。 (我要创建2,因为我有一些活动延长FragmentActivity以及一些延伸的活动,我也真的不喜欢在活动构造这样做,但因为我有打电话给applyOverrideConfiguration之前的任何呼叫在getResources做,我需要活动的ONSTART 的),然后调用它。

First, make sure all your activities extends a common activity which will be responsible to force the locale. (I had to create 2 since I have some activities extending FragmentActivity and some extending Activity. I also don't really like doing so in the Activities constructor but since I have to call applyOverrideConfiguration before any call is done on getResources, I need to call it before the activity's onStart).

public class LocaleMainActivity extends Activity {

    public LocaleMainActivity() {
        ActivityUtils.forceLocale(this);
    }
}

public class LocaleMainFragmentActivity extends FragmentActivity {

    public LocaleMainFragmentActivity() {
        ActivityUtils.forceLocale(this);
    }
}

//Method from ActivityUtils
public static void forceLocale(ContextThemeWrapper contextThemeWrapper) {
    Configuration configuration = new Configuration();
    Locale defaultLocale = new Locale(BuildConfig.LOCALE);
    configuration.locale = defaultLocale;
    contextThemeWrapper.applyOverrideConfiguration(configuration);      
}

接下来,你需要确定你的build.gradle口味的区域

Next, you need to define the locale in your build.gradle flavors

productFlavors {

    myFrenchFlavor {
     buildConfigField "String", "LOCALE", "\"fr\""
    }
    myEnglishFlavor {
      buildConfigField "String", "LOCALE", "\"en\""
    }
}

我测试的API 19+的解决方案。当您更改语言,而在应用程序中也起作用。

I tested the solution on API 19+. Works when you change language while in the app too.



替代的解决方案不会对Android的M +合作<一href="https://$c$c.google.com/p/android-developer-$p$pview/issues/detail?can=2&start=0&num=100&q=&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars&groupby=&sort=&id=2070"相对=nofollow>看到Android的bug报告 的) 该解决方案是在StackOverflow的广泛小号preaded但我面对一堵墙,而执行这项在Android中号所以请记住这一点。



Alternate solution (won't work on Android M+ see android bug report) This solution is wide spreaded on stackOverflow but I faced a wall while implementing this on Android M so keep this in mind.

在OnCreate()你AndroidApplication,你可以调用updateConfiguration与区域集。

In the onCreate() of your AndroidApplication you can call updateConfiguration with the locale set.

Locale myLocale = new Locale(BuildConfig.LOCALE);
Resources res = getResources();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf, res.getDisplayMetrics());

您还需要重置,如果你定义onConfigurationChanged()

You will also need to reset it if you defined onConfigurationChanged()

@Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);            
        Configuration config = new Configuration(newConfig);
        config.locale = new Locale(BuildConfig.LOCALE);
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    }

编辑:请注意,这applyOverrideConfiguration目前有一个副作用,这是在这里解释<一个href="http://stackoverflow.com/questions/32050784/chromium-webview-does-not-seems-to-work-with-android-applyoverrideconfiguration">Chromium web视图似乎并不与Android applyOverrideConfiguration 的工作。铬团队目前正在努力就可以了!

Be aware that this applyOverrideConfiguration currently has a side effect which is explained here Chromium webview does not seems to work with Android applyOverrideConfiguration. The Chromium team are currently working on it!

感谢。

这篇关于力的区域设置为Android风味resConfig的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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