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)
If you do not have to operate on those strings, the best option is moving to
strings.xml
, but that would make you share allres
folder between flavors. If you generate these strings based on some property onbuild.gradle
, then I think you're out of luck, unfortunately.EDIT: clarifying what I mean by operate above and add some options:
By operating on those strings I mean some sort of concatenation with a build parameter, a reading from command line or environment variable during the build process (e.g., getting the commit SHA1 so that it's easier to trace bugs later). If no operation is necessary,
strings.xml
may be an option. But when you overwrite ares
folder for flavor, all of it is overwritten and that could pose a problem if several flavors share the sameres
except for a limited number of strings.If each APK has its own locale, then it's just a
resValue
orbuildConfigField
in a flavor. You can define variables to for easier reuse of values. Something likeBut if several locales are needed in the same APK and it will be chosen at runtime by Android, then the string must be in the correct
values-<locale>
folder.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
src/flavor1/res/values-hu/strings.xml
src/flavor2/res/values/strings.xml
src/flavor2/res/values-hu/strings.xml`
build.gradle
This means that
flavor1/2
will have an extra unusedapp_name_gpe
string resource, but that'll be taken care of by aapt:You're operating on different levels here,
BuildConfig
is code, and as such not localized, that's why we have Lint warnings for hard-coded strings. Localization in Android is done via<string
resources, there's no way around that if you want the system to choose the language at runtime depending on user settings. There are many ways to have resources though:values
folder,resValue
in build.gradle, and generated resources.You should look into the
buildSrc
project in Gradle, for example I use it to generate SQL Inserts fromsrc/main/values/stuff.xml
. Here's some code to start with.buildSrc/build.gradle
buildSrc/src/main/groovy/Plugin.groovy
buildSrc/src/main/groovy/GenerateTask.groovy
This is just the skeleton you can go wild and do anything from here: e.g. retrieve a CSV through the network and spread the contents into generated
variant*/res/values-*/gen.xml
files.You can run it manually when you need to or run it at the right point in the build lifecycle (in
build.gradle
: