-
Notifications
You must be signed in to change notification settings - Fork 1
/
processMessages.js
198 lines (173 loc) · 4.31 KB
/
processMessages.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
const fs = require("fs").promises
const yargs = require("yargs")
const { LokaliseApi } = require("@lokalise/node-api")
const extractReactIntlMessages = require("extract-react-intl-messages")
const LOKALISE_API_TOKEN = process.env.LOKALISE_API_TOKEN || false
const LOKALISE_PROJECT_ID = "588298865e9f9f91e6f251.37381743"
if (!LOKALISE_API_TOKEN) {
throw Error("Require lokalise API token")
}
console.log(`Init lokalise with API key ${LOKALISE_API_TOKEN}`)
const client = new LokaliseApi({ apiKey: LOKALISE_API_TOKEN })
process.env.NODE_ENV = "development"
const convertToLokalise = (id, translation) => ({
key_name: id,
// description: message.description || "",
platforms: ["web"],
tags: ["website"],
translations: [
{
language_iso: "en",
translation: translation,
},
],
})
const updateKeys = async () => {
const keys = await client.keys.list({
project_id: LOKALISE_PROJECT_ID,
filter_platforms: "web",
filter_tags: "website",
include_translations: 1,
})
const keyIds = keys.map(k => k.key_name.web)
const keysToAdd = []
const keysToUpdate = []
const messages = require("./src/locale/en.json")
Object.keys(messages).map(async m => {
if (!keyIds.includes(m)) {
console.log(`Lokalise doesn't have key ${m}`)
keysToAdd.push(convertToLokalise(m, messages[m]))
} else {
let currentKey = keys.find(k => k.key_name.web === m)
let currentTranslation = ""
if (currentKey) {
currentTranslation = currentKey.translations.find(
t => t.language_iso === "en",
).translation
}
if (messages[m] !== currentTranslation) {
console.log(`Will update key ${m}`)
let newKey = convertToLokalise(m, messages[m])
newKey["key_id"] = currentKey["key_id"]
keysToUpdate.push(newKey)
}
}
})
if (keysToAdd.length) {
try {
await client.keys.create(keysToAdd, { project_id: LOKALISE_PROJECT_ID })
} catch (error) {
console.error(`Add error`)
console.error(error)
}
} else {
console.log(`No new keys to add`)
}
if (keysToUpdate.length) {
try {
await client.keys.bulk_update(keysToUpdate, {
project_id: LOKALISE_PROJECT_ID,
})
} catch (error) {
console.error(`Update error`)
console.error(error)
}
} else {
console.log(`No message updates so no keys to update`)
}
}
const extractMessages = async () => {
const l = await extractReactIntlMessages(
["en"],
"src/!(lib)/**/!(*.test).js",
"src/locale/",
)
console.log(l)
}
const downloadTranslations = async () => {
const translations = {}
const locales = []
let keys
try {
keys = await client.keys.list({
project_id: LOKALISE_PROJECT_ID,
filter_platforms: "web",
filter_tags: "website",
include_translations: 1,
})
} catch (error) {
console.error(error)
return false
}
keys.forEach(key => {
key.translations.forEach(translation => {
// if (translation.is_reviewed) {
let iso = translation.language_iso
if (!(iso in translations)) {
console.log(`Found locale ${iso}`)
translations[iso] = {}
locales.push(iso)
}
if (translation.translation && translation.words) {
translations[iso][key.key_name.web] = translation.translation
}
})
})
try {
await fs.writeFile(
`src/locale/messages.json`,
JSON.stringify(translations),
{
flag: "w",
},
)
} catch (error) {
console.error(error)
}
try {
await fs.writeFile(`src/locale/locales.json`, JSON.stringify(locales), {
flag: "w",
})
} catch (error) {
console.error(error)
}
}
yargs
.command(
"extract",
"extract messages",
() => {},
argv => {
console.log(`Extracing messages ..`)
extractMessages()
},
)
.command(
"update",
"update messages to lokalise",
() => {},
argv => {
console.log(`Updating keys ..`)
updateKeys()
},
)
.command(
"download",
"download messages from lokalise",
() => {},
argv => {
console.log(`Downloading translations ..`)
downloadTranslations()
},
)
.command(
"all",
"run all",
() => {},
args => {
extractMessages()
updateKeys()
downloadTranslations()
},
)
.help().argv