使用“resValue"本地化通过 build.gradle 添加的字符串资源 [英] Localizing string resources added via build.gradle using "resValue"

查看:116
本文介绍了使用“resValue"本地化通过 build.gradle 添加的字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在这篇文章

上帮助我的答案的延续

我们可以在build.gradle中添加如下字符串资源:

<块引用>

productFlavors {主要的{resValue "string", "app_name", "InTouch Messenger"}谷歌播放{resValue "string", "app_name", "InTouch Messenger: GPE Edition"}}

它的作用就像一个魅力,用于为每种口味提供不同的应用程序名称.(从 strings.xml 文件中删除了原始的 app_name 字符串资源.

但是,我们如何为从 build.gradle 添加的这个字符串资源添加本地化的字符串?

我们可以传递一个额外的参数来指定语言环境吗?可以使用 gradle 任务来完成吗?

注意:我不能使用 strings.xml 做到这一点(不可行,因为我的项目结构有多种方式)

解决方案

不过,我关于生成的资源的其他答案可能对您的用例来说有点矫枉过正.根据我目前对您的项目的了解,我认为这个更合适:(并不是说你仍然可以将它与生成的资源结合起来)

src/flavor1/res/values/strings.xml

src/flavor1/res/values-hu/strings.xml

src/flavor2/res/values/strings.xml

src/flavor2/res/values-hu/strings.xml`

<string name="app_name_base">无论 Üzenetküldő"</string><string name="app_name_gpe">无论 Üzenetküldő:GPE Változat"</string>

build.gradle

android {源集{[风味1,风味3].每个{it.res.srcDirs = ['src/flavor1/res']}[风味2,风味4].每个{it.res.srcDirs = ['src/flavor2/res']}}productFlavors {//注意与 sourceSets 不同的数字[风味1,风味2].每个{it.resValue "string", "app_name", "@string/app_name_base"}[flavor3,flavor4].each {it.resValue "string", "app_name", "@string/app_name_gpe"}}}

这意味着 flavor1/2 将有一个额外未使用的 app_name_gpe 字符串资源,但这将由 aapt 处理:

android {构建类型{释放 {收缩资源真//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 from strings.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 unused app_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"本地化通过 build.gradle 添加的字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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