-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.ts
60 lines (55 loc) · 1.78 KB
/
adapter.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
import { Extraction } from './extract';
import {
TranslationContext,
TranslationEntry, TranslationObject,
Translations,
} from 'gettext-parser';
import { blacklist } from './blacklist';
import { collate } from './collate';
export interface HeaderInfo {
project?: string;
projectVersion?: string;
bugsEmailAddress?: string;
}
export function translationEntryFromExtraction(extraction: Extraction): TranslationEntry {
return {
msgid: extraction.text,
msgstr: [''],
};
}
export function translationsFromExtractions(extractions: Extraction[]): Translations {
// For now, only support no/the default context:
const defaultCtxt = extractions.reduce((ctx: TranslationContext, extraction: Extraction) => {
const entry = translationEntryFromExtraction(extraction);
ctx[entry.msgid] = entry;
return ctx;
}, {});
return {
'': defaultCtxt,
};
}
export function translationObjectFromExtractions(
extractions: Extraction[],
headerInfo: HeaderInfo = {},
blacklistRegexes: RegExp[] = []): TranslationObject {
const filtered = blacklist(extractions, blacklistRegexes);
const collated = collate(filtered);
const project = headerInfo.project || 'untitled';
const projectVersion = headerInfo.projectVersion || '1.0';
const headers: TranslationObject['headers'] = {
'project-id-version': `${project} ${projectVersion}`,
'language': '',
'mime-version': '1.0',
'content-type': 'text/plain; charset=UTF-8',
'content-transfer-encoding': '8bit',
'x-generator': 'asciidoctor-gettext',
};
if (headerInfo.bugsEmailAddress && headerInfo.bugsEmailAddress.length > 0) {
headers['report-msgid-bugs-to'] = headerInfo.bugsEmailAddress;
}
return {
charset: 'utf-8',
headers,
translations: translationsFromExtractions(collated),
};
}