Created
June 19, 2020 11:20
-
-
Save eegeeZA/26263f226b2fa46ab1e48fe06203eb9a to your computer and use it in GitHub Desktop.
Pseudo-Locales for XLIFF (*.xlf) files using pure JavaScript on Node/NPM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Pseudo-Locales for XLIFF (*.xlf) files using pure JavaScript on Node/NPM | |
* | |
* Inspired by https://docs.microsoft.com/en-us/windows/win32/intl/pseudo-locales for use in Angular i18n. | |
* The output file will be $SOURCE_FILE_NAME.qps-Ploc.$SOURCE_FILE_EXT compatible with Angular's format. | |
* | |
* Usage: | |
* npm i -D pseudoloc | |
* node $LOCATION/pseudoloc-xliff.js $SOURCE_FILE | |
* | |
* e.g inside package.json :: | |
* "i18n:psedoLoc": "node ./src/locales/pseudoloc-xliff.js ./src/locales/messages.xlf" | |
*/ | |
const fs = require('fs').promises; | |
const path = require('path'); | |
const pseudoloc = require('pseudoloc'); | |
(async () => { | |
let arguments = process.argv.slice(2); | |
if (arguments.length !== 1) { | |
console.log('Only one argument allowed. Arguments received:', arguments); | |
return process.exit(1); | |
} | |
let inputFile = arguments[0]; | |
let fileExtension = path.extname(inputFile); | |
let fileName = path.basename(inputFile, fileExtension); | |
let outputFile = path.resolve(path.dirname(inputFile), `${fileName}.qps-Ploc${fileExtension}`); | |
let fileContents = await fs.readFile(inputFile, 'utf8'); | |
let qpsPloc = fileContents.replace( | |
/(.*)<source>(.*)<\/source>/gi, | |
(sourceNode, padding, sourceText) => `${sourceNode}\n${padding}<target>${pseudoloc.str(sourceText)}</target>` | |
); | |
await fs.writeFile(outputFile, qpsPloc); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment