()
| 126 | // Traverses the HTML document looking for node and attributes containing data-loc-id, to localize |
| 127 | // Outputs *.nls.json files containing strings to localize. |
| 128 | const processHtmlFiles = () => { |
| 129 | return es.through(function (file) { |
| 130 | let localizationJsonContents = {}; |
| 131 | let localizationMetadataContents = { |
| 132 | messages: [], |
| 133 | keys: [], |
| 134 | filePath: removePathPrefix(file.path, file.cwd) |
| 135 | }; |
| 136 | let nodeCallback = (locId, locHint, node) => { |
| 137 | let subNodeCount = 0; |
| 138 | let text = ""; |
| 139 | node.childNodes.forEach((childNode) => { |
| 140 | if (childNode.nodeName == "#text") { |
| 141 | text += childNode.value; |
| 142 | } else { |
| 143 | text += `{${subNodeCount++}}`; |
| 144 | } |
| 145 | }); |
| 146 | localizationJsonContents[locId] = text; |
| 147 | localizationMetadataContents.keys.push(locHint ? { key: locId, comment: [locHint] } : locId); |
| 148 | localizationMetadataContents.messages.push(text); |
| 149 | }; |
| 150 | let attributeCallback = (locId, locHint, attribute) => { |
| 151 | localizationJsonContents[locId] = attribute.value; |
| 152 | localizationMetadataContents.keys.push(locHint ? { key: locId, comment: [locHint] } : locId); |
| 153 | localizationMetadataContents.messages.push(attribute.value); |
| 154 | }; |
| 155 | traverseHtml(file.contents.toString(), nodeCallback, attributeCallback, false); |
| 156 | this.queue(new vinyl({ |
| 157 | path: path.join(file.path + '.nls.json'), |
| 158 | contents: Buffer.from(JSON.stringify(localizationJsonContents, null, '\t'), 'utf8') |
| 159 | })); |
| 160 | this.queue(new vinyl({ |
| 161 | path: path.join(file.path + '.nls.metadata.json'), |
| 162 | contents: Buffer.from(JSON.stringify(localizationMetadataContents, null, '\t'), 'utf8') |
| 163 | })); |
| 164 | }); |
| 165 | }; |
| 166 | |
| 167 | // descriptionCallback(path, value, parent) is invoked for attributes |
| 168 | const traverseJson = (jsonTree, descriptionCallback, prefixPath) => { |
no test coverage detected