resValue"使用&QUOT通过本地化增加的build.gradle字符串资源; [英] Localizing string resources added via build.gradle using "resValue"
问题描述
这是在延续答案这有助于我在这个帖子
我们可以从的build.gradle
如下添加字符串资源:
productFlavors {
主要{
resValue串,APP_NAME,InTouch中信使
} GOOGLEPLAY {
resValue串,APP_NAME,InTouch中信使:GPE版
}
}
块引用>它的工作原理就像一个魅力,并提供具有每风味不同的应用名称的目的。 (与删除原始的
APP_NAME
字符串资源的strings.xml
文件。但是,我们如何添加本地化字符串此字符串从
添加的资源的build.gradle
?有没有我们可以通过指定区域设置一个额外的参数?
或
可以使用gradle这个
任务做呢?注意:我无法做到这一点使用
的strings.xml
(不可行的,因为的几种方法我的项目的结构)解决方案你虽然使用的情况下我的有关生成资源的其他答案可能是矫枉过正。基本我目前了解你的项目,我觉得这个人是一个更合适:
(不,你仍然可以用产生的资源结合本)的src / flavor1 / RES /价值/ strings.xml中
<字符串名称=app_name_base>的InTouch信使< /串>
<字符串名称=app_name_gpe>的InTouch使者:GPE版< /串>的src / flavor1 / RES /值虎/ strings.xml中
<字符串名称=app_name_base>的InTouchÜzenetküldő< /串>
<字符串名称=app_name_gpe>的InTouchÜzenetküldő:GPEVáltozat< /串>的src / flavor2 / RES /价值/ strings.xml中
<字符串名称=app_name_base>无论信使< /串>
<字符串名称=app_name_gpe>不管使者:GPE版< /串>的src / flavor2 / RES /值虎/ strings.xml`
<字符串名称=app_name_base>无论Üzenetküldő< /串>
<字符串名称=app_name_gpe>无论Üzenetküldő:GPEVáltozat< /串>的build.gradle
安卓{
sourceSets {
[flavor1,flavor3]。每个{
it.res.srcDirs = ['SRC / flavor1 / RES']
}
[flavor2,flavor4]。每个{
it.res.srcDirs = ['SRC / flavor2 / RES']
}
}
productFlavors {//注意到比sourceSets不同的号码
[flavor1,flavor2]。每个{
it.resValue串,APP_NAME,@字符串/ app_name_base
}
[flavor3,flavor4]。每个{
it.resValue串,APP_NAME,@字符串/ app_name_gpe
}
}
}这意味着
flavor1 / 2
将有一个额外的未使用的app_name_gpe
字符串资源,但会采取通过AAPT呵护:安卓{
buildTypes {
发布 {
shrinkResources真// http://tools.android.com/tech-docs/new-build-system/resource-shrinking
}This is in continuation to an answer which helped me on this post
We can add the string resource as follows from
build.gradle
:productFlavors { main{ resValue "string", "app_name", "InTouch Messenger" } googlePlay{ resValue "string", "app_name", "InTouch Messenger: GPE Edition" } }
It works like a charm and serves the purpose of having different app names per flavor. (with the original
app_name
string resource deleted fromstrings.xml
file.But, how do we add localized strings for this string resource added from
build.gradle
?Is there an additional parameter we can pass specifying the locale? OR Possible to do it using a
gradle
task?Note: I cannot do this using
strings.xml
(not feasible because of several ways in which my project is structured)解决方案My other answer about the generated resources may be an overkill for you use case though. Base what I currently know about your project I think this one is a better fit: (not that you can still combine this with generated resources)
src/flavor1/res/values/strings.xml
<string name="app_name_base">InTouch Messenger"</string> <string name="app_name_gpe">InTouch Messenger: GPE Edition"</string>
src/flavor1/res/values-hu/strings.xml
<string name="app_name_base">InTouch Üzenetküldő"</string> <string name="app_name_gpe">InTouch Üzenetküldő: GPE Változat"</string>
src/flavor2/res/values/strings.xml
<string name="app_name_base">Whatever Messenger"</string> <string name="app_name_gpe">Whatever Messenger: GPE Edition"</string>
src/flavor2/res/values-hu/strings.xml`
<string name="app_name_base">Whatever Üzenetküldő"</string> <string name="app_name_gpe">Whatever Üzenetküldő: GPE Változat"</string>
build.gradle
android { sourceSets { [flavor1, flavor3].each { it.res.srcDirs = ['src/flavor1/res'] } [flavor2, flavor4].each { it.res.srcDirs = ['src/flavor2/res'] } } productFlavors { // notice the different numbers than sourceSets [flavor1, flavor2].each { it.resValue "string", "app_name", "@string/app_name_base" } [flavor3, flavor4].each { it.resValue "string", "app_name", "@string/app_name_gpe" } } }
This means that
flavor1/2
will have an extra unusedapp_name_gpe
string resource, but that'll be taken care of by aapt:android { buildTypes { release { shrinkResources true // http://tools.android.com/tech-docs/new-build-system/resource-shrinking }
这篇关于resValue&QUOT;使用&QUOT通过本地化增加的build.gradle字符串资源;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!