(file, relativePath, language, isFragment)
| 307 | }; |
| 308 | |
| 309 | const generateLocalizedHtmlFilesImpl = (file, relativePath, language, isFragment) => { |
| 310 | let stringTable = {}; |
| 311 | // Try to open i18n file for this file |
| 312 | let locFile = path.join("./i18n", language.folderName, relativePath + ".i18n.json"); |
| 313 | if (fs.existsSync(locFile)) { |
| 314 | stringTable = jsonc.parse(fs.readFileSync(locFile).toString()); |
| 315 | } |
| 316 | // Entire file is scanned and modified, then serialized for that language. |
| 317 | // Even if no translations are available, we still write new files to dist/html/... |
| 318 | |
| 319 | // Rewrite child nodes to fill in {0}, {1}, etc., in localized string. |
| 320 | let nodeCallback = (locId, locHint, node) => { |
| 321 | let locString = stringTable[locId]; |
| 322 | if (locString) { |
| 323 | let nonTextChildNodes = node.childNodes.filter(childNode => childNode.nodeName != "#text"); |
| 324 | let textParts = locString.split(/\{[0-9]+\}/); |
| 325 | let matchParts = locString.match(/\{[0-9]+\}/g); |
| 326 | let newChildNodes = []; |
| 327 | let i = 0; |
| 328 | for (; i < textParts.length - 1; i++) { |
| 329 | if (textParts[i] != "") { |
| 330 | newChildNodes.push({ nodeName: "#text", value: textParts[i] }); |
| 331 | } |
| 332 | let childIndex = matchParts[i].match(/[0-9]+/); |
| 333 | newChildNodes.push(nonTextChildNodes[childIndex]); |
| 334 | } |
| 335 | if (textParts[i] != "") { |
| 336 | newChildNodes.push({ nodeName: "#text", value: textParts[i] }); |
| 337 | } |
| 338 | node.childNodes = newChildNodes; |
| 339 | } |
| 340 | }; |
| 341 | let attributeCallback = (locId, locHint, attribute) => { |
| 342 | let value = stringTable[locId]; |
| 343 | if (value) { |
| 344 | attribute.value = value; |
| 345 | } |
| 346 | }; |
| 347 | let htmlTree = traverseHtml(file.contents.toString(), nodeCallback, attributeCallback, isFragment); |
| 348 | return parse5.serialize(htmlTree); |
| 349 | }; |
| 350 | |
| 351 | const generateLocalizedHtmlFiles = () => { |
| 352 | return es.through(function (file) { |
no test coverage detected