Skip to content

Commit c4b7197

Browse files
authored
Add translations guide (#89)
1 parent 2b800f2 commit c4b7197

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
},
66
"cSpell.words": [
77
"codereaper",
8+
"lproj",
89
"Noget",
910
"stretchr",
1011
"swiftlint",

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
`lane` is a task automation helper.
99

10+
## Guides
11+
12+
1. [Translations](docs/guides/using-translations.md) can help with generating translation files from a single source of truth.
13+
1014
## Installation
1115

1216
The preferred method of installation is through [asdf](http://asdf-vm.com/).

docs/guides/using-translations.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
## Using Translations
2+
3+
The purpose of translations in lane is to automate handling of text for Android, iOS and web applications.
4+
5+
Translation in lane provides these functions:
6+
7+
1. An optional helper function for downloading CSV files from Google Sheets.
8+
1. A function that writes out the actual files and support files needed for each application.
9+
10+
### A Practical Example
11+
12+
Suppose you have a Google Sheet document with an ID of `MY-DOCUMENT` which contains translations for two languages, English and Danish. This documents should have contents similar to this:
13+
14+
| Key | Context | EN | DA |
15+
| ---------------- | ----------------------- | -------- | ------ |
16+
| WELCOME_MESSAGE | Message upon login | Hello %1 | Hej %1 |
17+
| BUY | The purchase now button | Acquire | Køb |
18+
19+
Note that the string key is in column `1` and that English is in column `3` and Danish in column `4` and that the header row is only for us humans.
20+
21+
We will assume you already have your credentials in `lane/translations.json` (for help with credentials see [](../../docs/generated/lane_translations_download.md)).
22+
23+
What we will be doing to generate translation files for Android, iOS and web is to use `docker run` to invoke lane. Below are examples of how such calls should be integrated into tooling your project may already be using for easier copy-and-paste, but ...
24+
25+
**Remember**:
26+
1. Update the document id, if used
27+
1. Remove download step if you are using a local CSV file.
28+
1. Adjust the configurations and other options to match your needs.
29+
30+
Alright, go ahead and copy!
31+
32+
#### Android
33+
34+
We will update the `build.gradle` file so we can update all translations by running a gradle task, or simply:
35+
36+
```
37+
./gradle updateStrings
38+
```
39+
40+
The updates to a `build.gradle` file should look like this:
41+
42+
```gradle
43+
static void executeWithErrorHandling(ArrayList<String> command, File dir) {
44+
def process = command.execute(null, dir)
45+
process.waitFor()
46+
if (process.exitValue() != 0) throw new GradleException("Gradle exited with: " + process.exitValue() + "\nDetails: " + process.errorReader().text)
47+
}
48+
49+
static void dockerRun(ArrayList<String> args, File dir) {
50+
executeWithErrorHandling(['docker', 'run', '-it', '--rm', '--quiet-pull', '-v .:/workspace', '-w /workspace', 'ghcr.io/codereaper/lane@1', '--'] + args, dir)
51+
}
52+
53+
tasks.register('updateStrings') {
54+
dockerRun(['lane', 'translations', 'download',
55+
'--credentials', 'lane/translations.json', '--output', 'lane/translations.csv', '--document', 'MY-DOCUMENT'],
56+
projectDir)
57+
58+
dockerRun(['lane', 'translations', 'generate',
59+
'--fill-in', '--input', 'lane/translations.csv', '--type', 'android',
60+
'--index', '1', '--main-index', '3',
61+
'--configuration', '3 src/main/res/values/strings.xml',
62+
'--configuration', '3 src/main/res/values-en-rGB/strings.xml',
63+
'--configuration', '4 src/main/res/values-da/strings.xml'],
64+
projectDir)
65+
}
66+
```
67+
68+
#### iOS
69+
70+
We will add a target to a Makefile so we can update all translations by running a single task:
71+
72+
```
73+
make update-strings
74+
```
75+
76+
The updates to a `Makefile` file should look like this:
77+
78+
```make
79+
LANE_RUN = docker run -it --rm --quiet-pull -v .:/workspace -w /workspace ghcr.io/codereaper/lane@1 -- lane
80+
81+
update-strings:
82+
$(LANE_RUN) translations download --credentials lane/translations.json --output lane/translations.csv \
83+
--document MY-DOCUMENT
84+
85+
$(LANE_RUN) translations generate --fill-in --input lane/translations.csv --type ios \
86+
--output iOSProject/Assets/Translations.swift \
87+
--index 1 --main-index 3 \
88+
--configuration, '3 iOSProject/Assets/Translations/Base.lproj/Localizable.strings' \
89+
--configuration, '3 iOSProject/Assets/Translations/Base.lproj/InfoPlist.strings' \
90+
--configuration, '3 iOSProject/Assets/Translations/en-GB.lproj/Localizable.strings' \
91+
--configuration, '3 iOSProject/Assets/Translations/en-GB.lproj/InfoPlist.strings' \
92+
--configuration, '4 iOSProject/Assets/Translations/da.lproj/Localizable.strings' \
93+
--configuration, '4 iOSProject/Assets/Translations/da.lproj/InfoPlist.strings'
94+
```
95+
96+
#### Web
97+
98+
We will add a target to a Makefile so we can update all translations by running a single task:
99+
100+
```
101+
make update-strings
102+
```
103+
104+
The updates to a `Makefile` file should look like this:
105+
106+
```make
107+
LANE_RUN = docker run -it --rm --quiet-pull -v .:/workspace -w /workspace ghcr.io/codereaper/lane@1 -- lane
108+
109+
update-strings:
110+
$(LANE_RUN) translations download --credentials lane/translations.json --output lane/translations.csv \
111+
--document MY-DOCUMENT
112+
113+
$(LANE_RUN) translations generate --fill-in --input lane/translations.csv --type json \
114+
--index 1 --main-index 3 \
115+
--configuration, '3 src/locale/messages/en.json' \
116+
--configuration, '4 src/locale/messages/da.json'
117+
```

0 commit comments

Comments
 (0)