@vocab/phrase 2.1.1 → 2.1.2-fix-messageformat-import-20250923014407

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,347 +0,0 @@
1
- import { promises } from 'fs';
2
- import path from 'path';
3
- import { getAltLanguages, loadAllTranslations, getUniqueKey, getAltLanguageFilePath } from '@vocab/core';
4
- import pc from 'picocolors';
5
- import debug from 'debug';
6
- import { stringify } from 'csv-stringify/sync';
7
-
8
- const mkdir = promises.mkdir;
9
- const writeFile = promises.writeFile;
10
-
11
- const trace = debug(`vocab:phrase`);
12
- const log = (...params) => {
13
- // eslint-disable-next-line no-console
14
- console.log(pc.yellow('Vocab'), ...params);
15
- };
16
-
17
- function translationsToCsv(translations, devLanguage) {
18
- const languages = Object.keys(translations);
19
- const altLanguages = languages.filter(language => language !== devLanguage);
20
- const devLanguageTranslations = translations[devLanguage];
21
- const csvFilesByLanguage = Object.fromEntries(languages.map(language => [language, []]));
22
- Object.entries(devLanguageTranslations).map(([key, {
23
- message,
24
- description,
25
- tags
26
- }]) => {
27
- const sharedData = [key, description, tags === null || tags === void 0 ? void 0 : tags.join(',')];
28
- const devLanguageRow = [...sharedData, message];
29
- csvFilesByLanguage[devLanguage].push(devLanguageRow);
30
- altLanguages.map(language => {
31
- var _translations$languag;
32
- const altTranslationMessage = (_translations$languag = translations[language]) === null || _translations$languag === void 0 || (_translations$languag = _translations$languag[key]) === null || _translations$languag === void 0 ? void 0 : _translations$languag.message;
33
- if (altTranslationMessage) {
34
- csvFilesByLanguage[language].push([...sharedData, altTranslationMessage]);
35
- }
36
- });
37
- });
38
- const csvFileStrings = Object.fromEntries(Object.entries(csvFilesByLanguage)
39
- // Ensure CSV files are only created if the language has at least 1 translation
40
- .filter(([_, csvFile]) => csvFile.length > 0).map(([language, csvFile]) => {
41
- const csvFileString = stringify(csvFile, {
42
- delimiter: ',',
43
- header: false
44
- });
45
- return [language, csvFileString];
46
- }));
47
-
48
- // Column indices start at 1
49
- const keyIndex = 1;
50
- const commentIndex = keyIndex + 1;
51
- const tagColumn = commentIndex + 1;
52
- const messageIndex = tagColumn + 1;
53
- return {
54
- csvFileStrings,
55
- keyIndex,
56
- messageIndex,
57
- commentIndex,
58
- tagColumn
59
- };
60
- }
61
-
62
- /* eslint-disable no-console */
63
- function _callPhrase(path, options = {}) {
64
- const phraseApiToken = process.env.PHRASE_API_TOKEN;
65
- if (!phraseApiToken) {
66
- throw new Error('Missing PHRASE_API_TOKEN');
67
- }
68
- return fetch(path, {
69
- ...options,
70
- headers: {
71
- Authorization: `token ${phraseApiToken}`,
72
- // Provide identification via User Agent as requested in https://developers.phrase.com/api/#overview--identification-via-user-agent
73
- 'User-Agent': 'Vocab Client (https://github.com/seek-oss/vocab)',
74
- ...options.headers
75
- }
76
- }).then(async response => {
77
- console.log(`${path}: ${response.status} - ${response.statusText}`);
78
- const secondsUntilLimitReset = Math.ceil(Number.parseFloat(response.headers.get('X-Rate-Limit-Reset') || '0') - Date.now() / 1000);
79
- console.log(`Rate Limit: ${response.headers.get('X-Rate-Limit-Remaining')} of ${response.headers.get('X-Rate-Limit-Limit')} remaining. (${secondsUntilLimitReset} seconds remaining)`);
80
- trace('\nLink:', response.headers.get('Link'), '\n');
81
- // Print All Headers:
82
- // console.log(Array.from(r.headers.entries()));
83
-
84
- try {
85
- var _response$headers$get;
86
- const result = await response.json();
87
- trace(`Internal Result (Length: ${result.length})\n`);
88
- if ((!options.method || options.method === 'GET') && (_response$headers$get = response.headers.get('Link')) !== null && _response$headers$get !== void 0 && _response$headers$get.includes('rel=next')) {
89
- var _response$headers$get2, _response$headers$get3;
90
- const [, nextPageUrl] = (_response$headers$get2 = (_response$headers$get3 = response.headers.get('Link')) === null || _response$headers$get3 === void 0 ? void 0 : _response$headers$get3.match(/<([^>]*)>; rel=next/)) !== null && _response$headers$get2 !== void 0 ? _response$headers$get2 : [];
91
- if (!nextPageUrl) {
92
- throw new Error("Can't parse next page URL");
93
- }
94
- console.log('Results received with next page: ', nextPageUrl);
95
- const nextPageResult = await _callPhrase(nextPageUrl, options);
96
- return [...result, ...nextPageResult];
97
- }
98
- return result;
99
- } catch (e) {
100
- console.error('Unable to parse response as JSON', e);
101
- return response.text();
102
- }
103
- });
104
- }
105
- async function callPhrase(relativePath, options = {}) {
106
- const projectId = process.env.PHRASE_PROJECT_ID;
107
- if (!projectId) {
108
- throw new Error('Missing PHRASE_PROJECT_ID');
109
- }
110
- return _callPhrase(`https://api.phrase.com/v2/projects/${projectId}/${relativePath}`, options).then(result => {
111
- if (Array.isArray(result)) {
112
- console.log('Result length:', result.length);
113
- }
114
- return result;
115
- }).catch(error => {
116
- console.error(`Error calling phrase for ${relativePath}:`, error);
117
- throw Error;
118
- });
119
- }
120
- async function pullAllTranslations(branch) {
121
- const phraseResult = await callPhrase(`translations?branch=${branch}&per_page=100`);
122
- const translations = {};
123
- for (const r of phraseResult) {
124
- if (!translations[r.locale.name]) {
125
- translations[r.locale.name] = {};
126
- }
127
- translations[r.locale.name][r.key.name] = {
128
- message: r.content
129
- };
130
- }
131
- return translations;
132
- }
133
- async function pushTranslations(translationsByLanguage, {
134
- devLanguage,
135
- branch
136
- }) {
137
- const {
138
- csvFileStrings,
139
- keyIndex,
140
- commentIndex,
141
- tagColumn,
142
- messageIndex
143
- } = translationsToCsv(translationsByLanguage, devLanguage);
144
- let devLanguageUploadId = '';
145
- for (const [language, csvFileString] of Object.entries(csvFileStrings)) {
146
- const formData = new FormData();
147
- formData.append('file', new Blob([csvFileString], {
148
- type: 'text/csv'
149
- }), `${language}.translations.csv`);
150
- formData.append('file_format', 'csv');
151
- formData.append('branch', branch);
152
- formData.append('update_translations', 'true');
153
- formData.append('update_descriptions', 'true');
154
- formData.append(`locale_mapping[${language}]`, messageIndex.toString());
155
- formData.append('format_options[key_index]', keyIndex.toString());
156
- formData.append('format_options[comment_index]', commentIndex.toString());
157
- formData.append('format_options[tag_column]', tagColumn.toString());
158
- formData.append('format_options[enable_pluralization]', 'false');
159
- log(`Uploading translations for language ${language}`);
160
- const result = await callPhrase(`uploads`, {
161
- method: 'POST',
162
- body: formData
163
- });
164
- trace('Upload result:\n', result);
165
- if (result && 'id' in result) {
166
- log('Upload ID:', result.id, '\n');
167
- log('Successfully Uploaded\n');
168
- } else {
169
- log(`Error uploading: ${result === null || result === void 0 ? void 0 : result.message}\n`);
170
- log('Response:', result);
171
- throw new Error('Error uploading');
172
- }
173
- if (language === devLanguage) {
174
- devLanguageUploadId = result.id;
175
- }
176
- }
177
- return {
178
- devLanguageUploadId
179
- };
180
- }
181
- async function deleteUnusedKeys(uploadId, branch) {
182
- const query = `unmentioned_in_upload:${uploadId}`;
183
- const {
184
- records_affected
185
- } = await callPhrase('keys', {
186
- method: 'DELETE',
187
- headers: {
188
- 'Content-Type': 'application/json'
189
- },
190
- body: JSON.stringify({
191
- branch,
192
- q: query
193
- })
194
- });
195
- log('Successfully deleted', records_affected, 'unused keys from branch', branch);
196
- }
197
- async function ensureBranch(branch) {
198
- await callPhrase(`branches`, {
199
- method: 'POST',
200
- headers: {
201
- 'Content-Type': 'application/json'
202
- },
203
- body: JSON.stringify({
204
- name: branch
205
- })
206
- });
207
- log('Created branch:', branch);
208
- }
209
-
210
- async function pull({
211
- branch = 'local-development',
212
- errorOnNoGlobalKeyTranslation
213
- }, config) {
214
- trace(`Pulling translations from branch ${branch}`);
215
- await ensureBranch(branch);
216
- const alternativeLanguages = getAltLanguages(config);
217
- const allPhraseTranslations = await pullAllTranslations(branch);
218
- trace(`Pulling translations from Phrase for languages ${config.devLanguage} and ${alternativeLanguages.join(', ')}`);
219
- const phraseLanguages = Object.keys(allPhraseTranslations);
220
- trace(`Found Phrase translations for languages ${phraseLanguages.join(', ')}`);
221
- if (!phraseLanguages.includes(config.devLanguage)) {
222
- throw new Error(`Phrase did not return any translations for the configured development language "${config.devLanguage}".\nPlease ensure this language is present in your Phrase project's configuration.`);
223
- }
224
- const allVocabTranslations = await loadAllTranslations({
225
- fallbacks: 'none',
226
- includeNodeModules: false,
227
- withTags: true
228
- }, config);
229
- for (const loadedTranslation of allVocabTranslations) {
230
- const devTranslations = loadedTranslation.languages[config.devLanguage];
231
- if (!devTranslations) {
232
- throw new Error('No dev language translations loaded');
233
- }
234
- const defaultValues = {
235
- ...devTranslations
236
- };
237
- const localKeys = Object.keys(defaultValues);
238
- for (const key of localKeys) {
239
- var _defaultValues$key$gl;
240
- defaultValues[key] = {
241
- ...defaultValues[key],
242
- ...allPhraseTranslations[config.devLanguage][(_defaultValues$key$gl = defaultValues[key].globalKey) !== null && _defaultValues$key$gl !== void 0 ? _defaultValues$key$gl : getUniqueKey(key, loadedTranslation.namespace)]
243
- };
244
- }
245
-
246
- // Only write a `_meta` field if necessary
247
- if (Object.keys(loadedTranslation.metadata).length > 0) {
248
- defaultValues._meta = loadedTranslation.metadata;
249
- }
250
- await writeFile(loadedTranslation.filePath, `${JSON.stringify(defaultValues, null, 2)}\n`);
251
- for (const alternativeLanguage of alternativeLanguages) {
252
- if (alternativeLanguage in allPhraseTranslations) {
253
- const altTranslations = {
254
- ...loadedTranslation.languages[alternativeLanguage]
255
- };
256
- const phraseAltTranslations = allPhraseTranslations[alternativeLanguage];
257
- for (const key of localKeys) {
258
- var _defaultValues$key$gl2, _phraseAltTranslation;
259
- const phraseKey = (_defaultValues$key$gl2 = defaultValues[key].globalKey) !== null && _defaultValues$key$gl2 !== void 0 ? _defaultValues$key$gl2 : getUniqueKey(key, loadedTranslation.namespace);
260
- const phraseTranslationMessage = (_phraseAltTranslation = phraseAltTranslations[phraseKey]) === null || _phraseAltTranslation === void 0 ? void 0 : _phraseAltTranslation.message;
261
- if (!phraseTranslationMessage) {
262
- trace(`Missing translation. No translation for key ${key} in phrase as ${phraseKey} in language ${alternativeLanguage}.`);
263
- if (errorOnNoGlobalKeyTranslation && defaultValues[key].globalKey) {
264
- throw new Error(`Missing translation for global key ${key} in language ${alternativeLanguage}`);
265
- }
266
- continue;
267
- }
268
- altTranslations[key] = {
269
- ...altTranslations[key],
270
- message: phraseTranslationMessage
271
- };
272
- }
273
- const altTranslationFilePath = getAltLanguageFilePath(loadedTranslation.filePath, alternativeLanguage);
274
- await mkdir(path.dirname(altTranslationFilePath), {
275
- recursive: true
276
- });
277
- await writeFile(altTranslationFilePath, `${JSON.stringify(altTranslations, null, 2)}\n`);
278
- }
279
- }
280
- }
281
- }
282
-
283
- /**
284
- * Uploads translations to the Phrase API for each language.
285
- * A unique namespace is appended to each key using the file path the key came from.
286
- */
287
- async function push({
288
- branch,
289
- deleteUnusedKeys: deleteUnusedKeys$1,
290
- ignore
291
- }, config) {
292
- if (ignore) {
293
- trace(`ignoring files on paths: ${ignore.join(', ')}`);
294
- }
295
- const allLanguageTranslations = await loadAllTranslations({
296
- fallbacks: 'none',
297
- includeNodeModules: false,
298
- withTags: true
299
- }, {
300
- ...config,
301
- ignore: [...(config.ignore || []), ...(ignore || [])]
302
- });
303
- trace(`Pushing translations to branch ${branch}`);
304
- const allLanguages = config.languages.map(v => v.name);
305
- await ensureBranch(branch);
306
- trace(`Pushing translations to phrase for languages ${allLanguages.join(', ')}`);
307
- const phraseTranslations = {};
308
- for (const loadedTranslation of allLanguageTranslations) {
309
- for (const language of allLanguages) {
310
- const localTranslations = loadedTranslation.languages[language];
311
- if (!localTranslations) {
312
- continue;
313
- }
314
- if (!phraseTranslations[language]) {
315
- phraseTranslations[language] = {};
316
- }
317
- const {
318
- metadata: {
319
- tags: sharedTags = []
320
- }
321
- } = loadedTranslation;
322
- for (const localKey of Object.keys(localTranslations)) {
323
- const {
324
- tags = [],
325
- ...localTranslation
326
- } = localTranslations[localKey];
327
- if (language === config.devLanguage) {
328
- localTranslation.tags = [...tags, ...sharedTags];
329
- }
330
- const globalKey = loadedTranslation.languages[config.devLanguage][localKey].globalKey;
331
- const phraseKey = globalKey !== null && globalKey !== void 0 ? globalKey : getUniqueKey(localKey, loadedTranslation.namespace);
332
- phraseTranslations[language][phraseKey] = localTranslation;
333
- }
334
- }
335
- }
336
- const {
337
- devLanguageUploadId
338
- } = await pushTranslations(phraseTranslations, {
339
- devLanguage: config.devLanguage,
340
- branch
341
- });
342
- if (deleteUnusedKeys$1) {
343
- await deleteUnusedKeys(devLanguageUploadId, branch);
344
- }
345
- }
346
-
347
- export { pull, push };