-
Notifications
You must be signed in to change notification settings - Fork 10
/
companionTranslations.ts
72 lines (58 loc) · 2.14 KB
/
companionTranslations.ts
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
import { basename } from 'path';
import { promisify } from 'util';
import { LanguageTable, MessageTable } from '@fitbit-sdk/companion-gettext';
import { default as _glob } from 'glob';
import humanizeList from 'humanize-list';
import pofile from 'pofile';
import { dataToEsm } from 'rollup-pluginutils';
import BuildError from '../util/BuildError';
import { validateLanguageTag, supportedTags } from '../languageTag';
const glob = promisify(_glob);
const loadPOFile = promisify(pofile.load);
async function loadTranslations(filePath: string) {
const po = await loadPOFile(filePath);
const messages: MessageTable = {};
for (const { msgid, msgstr } of po.items) {
if (msgstr.length > 1) {
throw new BuildError(
`msgid "${msgid}" in file "${filePath}" has multiple msgstr values. This is not supported.`,
);
}
messages[msgid] = msgstr[0];
}
return messages;
}
export default function companionTranslations(
globPattern: string,
defaultLanguage: string,
) {
return async () => {
const languagePaths = new Map<string, string>();
const translations: LanguageTable = {};
for (const filePath of await glob(globPattern)) {
const languageTag = basename(filePath, '.po');
if (!validateLanguageTag(languageTag)) {
throw new BuildError(
`Translation file "${filePath}" has a bad name. Translation files must be named ${humanizeList(
supportedTags.map((tag) => tag + '.po'),
{ conjunction: 'or' },
)}.`,
);
}
const existingTranslations = languagePaths.get(languageTag);
if (existingTranslations) {
throw new BuildError(
`More than one translation file found for language ${languageTag}. Found "${existingTranslations}" and "${filePath}".`,
);
}
languagePaths.set(languageTag, filePath);
translations[languageTag] = await loadTranslations(filePath);
}
if (!translations.hasOwnProperty(defaultLanguage)) {
throw new BuildError(
`No translation file found for default language "${defaultLanguage}"`,
);
}
return dataToEsm(translations, { namedExports: false });
};
}