-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#29) Implementing i18n-js2properties CLI for migrate translations
- Loading branch information
Showing
5 changed files
with
237 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#!/usr/bin/env node | ||
//for the source definitions in style ES6 | ||
require("babel-register")({ | ||
presets: ["es2015", "stage-0"], | ||
plugins: ["istanbul"] | ||
}); | ||
const properties = require("properties"); | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const argv = require('yargs') | ||
.usage("Usage: i18n-js2properties --source src/client/components/i18n --target ../plugin/grails-app/i18n/boilerplateEditor") | ||
.demandOption(["source", "target"]) | ||
.argv; | ||
|
||
const {source, target} = argv; | ||
const sourceFilePath = path.resolve(process.cwd(), source); | ||
console.log(`Reading source import [${sourceFilePath}]`); | ||
let sourceObject = require(sourceFilePath); | ||
//in case if export module with es6 | ||
if (sourceObject.default) { | ||
sourceObject = sourceObject.default; | ||
} | ||
|
||
const defaultLanguage = "en"; | ||
|
||
if (!sourceObject[defaultLanguage]) { | ||
console.error(`Default language [${defaultLanguage}] should be defined in translations [${source}]`); | ||
} else { | ||
const targetPath = path.resolve(process.cwd(), path.dirname(target)); | ||
|
||
if (!fs.existsSync(targetPath)) { | ||
console.error(`Target path [${targetPath}] doesn't exists`); | ||
} else { | ||
const bundleName = path.basename(target); | ||
|
||
const languages = Object.keys(sourceObject); | ||
|
||
function flattenTranslationTexts(object, parentKey) { | ||
let result = {}; | ||
const keys = Object.keys(object); | ||
|
||
for (let i = 0, l = keys.length; i < l; i++) { | ||
const key = keys[i]; | ||
const propertiesKey = parentKey ? `${parentKey}.${key}` : key; | ||
const value = object[key]; | ||
|
||
if (typeof value === "object") { | ||
result = { | ||
...result, | ||
...flattenTranslationTexts(value, propertiesKey) | ||
}; | ||
} else { | ||
result[propertiesKey] = value.toString(); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
for (let i = 0; i < languages.length; i++) { | ||
const language = languages[i]; | ||
const objectTranslationTexts = sourceObject[language]; | ||
const translationTexts = flattenTranslationTexts(objectTranslationTexts); | ||
const propertiesText = properties.stringify(translationTexts, { | ||
// unicode: true, | ||
}); | ||
const filePath = path.join(targetPath, `${bundleName}_${language}.properties`); | ||
|
||
console.log(`Writing target file [${filePath}]`); | ||
fs.writeFileSync(filePath, propertiesText); | ||
} | ||
|
||
const fromFilePath = path.join(targetPath, `${bundleName}_en.properties`); | ||
const toFilePath = path.join(targetPath, `${bundleName}.properties`); | ||
console.log(`Copy file [${fromFilePath}] to [${toFilePath}]`); | ||
//save english properties as default | ||
fs.createReadStream(fromFilePath).pipe( | ||
fs.createWriteStream(toFilePath) | ||
); | ||
} | ||
} | ||
|
||
|
||
|
||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const fs = require("fs"); | ||
const rimraf = require("rimraf"); | ||
const { expect } = require("chai"); | ||
const { spawn } = require("child_process"); | ||
const concat = require("concat-stream"); | ||
const properties = require("properties"); | ||
|
||
function createProcess(processPath, args = [], env = {}) { | ||
return spawn("node", [processPath].concat(args), { | ||
env: { | ||
...env, | ||
...{ NODE_ENV: "test" } | ||
} | ||
}); | ||
} | ||
|
||
function execute(processPath, args = [], opts = {}) { | ||
const { env = null } = opts; | ||
const childProcess = createProcess(processPath, args, env); | ||
childProcess.stdin.setEncoding("utf-8"); | ||
return new Promise((resolve, reject) => { | ||
childProcess.stderr.once("data", err => { | ||
reject(err.toString()); | ||
}); | ||
childProcess.on('error', reject); | ||
childProcess.stdout.pipe( | ||
concat(result => { | ||
resolve(result.toString()); | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
|
||
describe("i18n-js to properties CLI", () => { | ||
beforeEach(() => { | ||
rimraf.sync("tmp"); | ||
fs.mkdirSync("tmp"); | ||
fs.mkdirSync("tmp/i18n"); | ||
fs.mkdirSync("tmp/grails-app"); | ||
fs.mkdirSync("tmp/grails-app/i18n"); | ||
fs.writeFileSync("tmp/i18n/index.js", ` | ||
import en from './en'; | ||
import de from './de'; | ||
export default { | ||
en, | ||
de | ||
}; | ||
`); | ||
fs.writeFileSync("tmp/i18n/en.js", ` | ||
const header = { | ||
title: "test title" | ||
}; | ||
const form = { | ||
"a.b": "test a b", | ||
"a.b.c": "test a b c" | ||
}; | ||
export default { messages: {header, form} } | ||
`); | ||
fs.writeFileSync("tmp/i18n/de.js", ` | ||
const header = { | ||
title: "de test title" | ||
}; | ||
const form = { | ||
"a.b": "de test a b", | ||
"a.b.c": "de test a b c" | ||
}; | ||
export default { messages: {header, form} } | ||
`); | ||
}); | ||
|
||
afterEach(() => { | ||
rimraf.sync("tmp"); | ||
}); | ||
|
||
it("should generate properties", async () => { | ||
await execute("bin/i18n-js2properties.js", ["--source", "tmp/i18n", "--target", "tmp/grails-app/i18n/messages"]); | ||
|
||
expect( | ||
fs.existsSync("tmp/grails-app/i18n/messages.properties") | ||
).to.be.equal(true); | ||
|
||
expect( | ||
fs.existsSync("tmp/grails-app/i18n/messages_en.properties") | ||
).to.be.equal(true); | ||
|
||
expect( | ||
fs.existsSync("tmp/grails-app/i18n/messages_de.properties") | ||
).to.be.equal(true); | ||
|
||
const englishProperties = properties.parse( | ||
fs.readFileSync("tmp/grails-app/i18n/messages_en.properties").toString() | ||
); | ||
|
||
expect(englishProperties).to.deep.include( | ||
{ "messages.header.title": "test title", "messages.form.a.b": "test a b", "messages.form.a.b.c": "test a b c" } | ||
); | ||
|
||
const germanProperties = properties.parse(fs.readFileSync("tmp/grails-app/i18n/messages_de.properties").toString()); | ||
|
||
expect(germanProperties).to.deep.include( | ||
{ | ||
"messages.header.title": "de test title", | ||
"messages.form.a.b": "de test a b", | ||
"messages.form.a.b.c": "de test a b c" | ||
} | ||
); | ||
|
||
const defaultProperties = properties.parse(fs.readFileSync("tmp/grails-app/i18n/messages.properties").toString()); | ||
expect(defaultProperties).to.deep.include(englishProperties); | ||
}); | ||
}); |