Custom plugin with the purpose to import shared strings to the app from the PoEditor project.
Convention plugin provides completely custom AndroidPoEditorPlugin which can import shared string
resources
from PoEditor, converts them accordingly and also copy appropriates strings
to appropriates modules based on cz.kotox.android.poeditor.ResourceModuleMapping
file.
- shared:
strings_*_shared.xml
- those strings are shared with iOs platform. Those files are generated and must not be updated manually. Any update has to be done via PoEditor - local:
strings_*.xml
- those strings should be avoided since all strings should be in PoEditor. Use such string resources temporarily just to speed up development only!
Following conventions are given by the specific situation where all strings in PoEditor are imported from iOs app, so they have an iOs key format. Also there is not more projects separation to Android/iOs, but current AndroidPoEditorPlugin can convert iOs format to Android.
If there is Android specific string, the resource key in the PoEditor should have _android
suffix
so it will not
be distributed to the iOs resources.
Similiar situation is for iOs specific string, the resource key in the PoEditor should have _ios
suffix so it will
not be distributed to the Android resources.
Mobile platform specific keys (_android
,_ios
) are removed by the plugin during import so they
are not part of the final Android key in resources.
In order to add module as a recipient of shared string resources do following steps:
- Ensure
build.gradle.kts
of that module definesalias(libs.plugins.cz.kotox.android.poeditor)
gradle plugin. - Ensure proper mapping of specific resource key expression to the target module
in the
cz.kotox.android.poeditor.ResourceModuleMapping.kt
With above mentioned two steps fulfilled there will be proper strings_*_shared.xml
generated in
the module resources with ./gradlew importPoEditorStrings
- Ensure you have API_TOKEN and PROJECTID set in the environment variables.
- On MacOs it might be in
~/.zshenv
- Add line: export
POEDITOR_API_TOKEN=
here_write_your_api_token - Add line: export
POEDITOR_PROJECT_ID=
here_write_your_project_id - Ensure those variables are loaded in to environment (e.g. restart your computer)
- On MacOs it might be in
- Execute import via gradle command:
./gradlew importPoEditorStrings
in the root of the project - Check for new files, there should be imported string resources after successful run of the gradle command
Let's take a closer look under the plugin hood. Everything starts in PoEditorImportController)
PoEditorPlugin functional code is based in convention plugin in it's own
package: cz.kotox.android.poeditor.*
.
PoEditorPlugin definition is then available in: cz.kotox.android.AndroidPoEditorPlugin.kt
.
Custom PoEditorPlugin works in two phases:
- Download source file from https://poeditor.com/docs/api#openapi
- Post-process source file and distribute final strings.xml files to modules.
After android resource file type is downloaded from openapi, there needs to be done some post-processing since there is a lot of iOs specific which would not work on Android side.
Post procesing of source file downloaded from openapi stands for several points:
- Avoid processing of iOs specific resources (by
_ios
suffix) - Determine targetResource module/file based on resource key. The mapping is specified
in
cz.kotox.android.poeditor.ResourceModuleMapping
. - Fix resource key. Transform original iOs form (separated by dots) to Android form (separated by underscore).
- Remove eventual Android specific resource key suffix
_android
- Fix resource parameter placeholders. Transform original iOs placeholders to Android placeholders
- Copy processed nodes to determined targets.
Slides from Android Talks in Prague 2023/5.