-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcreateNewTranslationFiles.js
More file actions
154 lines (141 loc) · 5.28 KB
/
Copy pathcreateNewTranslationFiles.js
File metadata and controls
154 lines (141 loc) · 5.28 KB
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
const fs = require('fs')
const path = require('path')
const { reporter } = require('@dhis2/cli-helpers-engine')
const {
getTemplateMainLanguage,
getTemplateAlternativeLanguage,
} = require('./getTemplates.js')
const {
LENGTH_TO_SPLIT_LINE_AT,
splitTranslation,
} = require('./splitTranslation.js')
/**
* @param {Object} args
* @param {string} args.outDir
* @param {string} args.pootlePath
* @param {Object} args.creationDate
* @param {Object} args.translations
* @param {boolean} args.logMissingKeys
* @param {string} args.primaryLanguage
* @param {string[]} args.languagesToTransform
* @param {boolean} args.overrideExistingFiles
* @param {boolean} args.appendToExistingFiles
* @return {void}
*/
const createNewTranslationFiles = ({
outDir,
pootlePath,
translations,
creationDate,
logMissingKeys,
primaryLanguage,
languagesToTransform,
overrideExistingFiles,
appendToExistingFiles,
}) => {
for (const language in translations) {
if (Object.prototype.hasOwnProperty.call(translations, language)) {
if (
languagesToTransform.length &&
languagesToTransform.indexOf(language) === -1
) {
continue
}
const newLanguageFileName =
language === primaryLanguage
? `${language}.pot`
: `${language}.po`
let newContents = ''
const languageTranslations = translations[language]
const newLanguageFilePath = path.join(outDir, newLanguageFileName)
if (
!overrideExistingFiles &&
fs.existsSync(newLanguageFilePath) &&
!appendToExistingFiles
) {
reporter.print('')
reporter.print(
`Creating translation file for "${language}" :: Skipped`
)
reporter.print(
`Translation file ("${newLanguageFilePath}") already exists.`
)
reporter.print(
`If you want to append the translations, use the "--append-to-existing-files" option.`
)
reporter.print(
`If you want to override the existing files, use the "--override-existing-files" option`
)
continue
} else if (
overrideExistingFiles ||
!fs.existsSync(newLanguageFilePath)
) {
newContents +=
language === primaryLanguage
? getTemplateMainLanguage(creationDate)
: getTemplateAlternativeLanguage(
language,
creationDate,
pootlePath
)
}
for (const key in languageTranslations) {
if (
Object.prototype.hasOwnProperty.call(
languageTranslations,
key
)
) {
if (!translations[primaryLanguage][key]) {
logMissingKeys &&
reporter.info(
`Original translation missing for key "${key}" of language "${language}"`
)
continue
}
const originalTranslation =
translations[primaryLanguage][key]
const translation =
language === primaryLanguage
? ''
: languageTranslations[key]
newContents += '\n\n'
if (originalTranslation.length < LENGTH_TO_SPLIT_LINE_AT) {
newContents += `msgid "${originalTranslation}"\n`
} else {
newContents += `msgid ""\n`
const newLines = splitTranslation(originalTranslation)
newLines.forEach((translationPart) => {
newContents += `"${unescape(translationPart)}"\n`
})
}
if (translation.length < LENGTH_TO_SPLIT_LINE_AT) {
newContents += `msgstr "${translation}"`
} else {
newContents += `msgstr ""\n`
const newLines = splitTranslation(translation)
newLines.forEach((translationPart) => {
newContents += `"${unescape(translationPart)}"\n`
})
}
}
}
const shouldAppendContents =
!overrideExistingFiles &&
fs.existsSync(newLanguageFilePath) &&
appendToExistingFiles
reporter.info(
`${
shouldAppendContents ? 'Appending' : 'Writing'
} to "${newLanguageFilePath}"`
)
fs.writeFileSync(newLanguageFilePath, newContents, {
flag: shouldAppendContents ? 'a' : 'w',
})
}
}
}
module.exports = {
createNewTranslationFiles,
}