Skip to content

Commit

Permalink
(EPROC-20779) Remove support of converting language specific translat…
Browse files Browse the repository at this point in the history
…ion bundles from json into properties file (#34)
  • Loading branch information
dsanko-sc committed Oct 4, 2021
1 parent 46ee15e commit 0ff4f44
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 91 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@

[Release 1.2.15-EPROC-20779-SNAPSHOT](https://github.com/OpusCapita/i18n/releases/tag/v1.2.15-EPROC-20779-SNAPSHOT) Thu Sep 30 2021 22:55:13 GMT+0300 (MSK)
=======================================================

- Remove support of converting language specific translation bundles from json into properties file (Dmitriy Sanko dmitriy.sanko@opuscapita.com, 2021-09-30 22:44:53 +0300)
- (EPROC-20383) Used jdk zulu (GitHub ashestak-sc@users.noreply.github.com, 2021-04-06 16:50:55 +0300)
- (EPROC-20252) Added circleci builds jira integration (GitHub ashestak-sc@users.noreply.github.com, 2021-03-10 16:08:02 +0300)
- Update CI image to opuscapita/minsk-core-ci:grails-2.4.4-jdk-8u192-nodejs-8.17.0-maven-3.3.9 [ci skip] (Egor Stambakio egor.stambakio@opuscapita.com, 2020-05-07 16:13:08 +0300)

[Release 1.2.14](https://github.com/OpusCapita/i18n/releases/tag/v1.2.14) Thu Apr 09 2020 09:58:33 GMT+0300 (MSK)
=======================================================

Expand Down
82 changes: 31 additions & 51 deletions bin/i18n-js2properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ if (sourceObject.default) {
sourceObject = sourceObject.default;
}

const defaultLanguage = "en";

function padWithLeadingZeros(string) {
return new Array(5 - string.length).join("0") + string;
}
Expand All @@ -40,59 +38,41 @@ function unicodeEscape(string) {
.join("");
}

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;
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();
}
}

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);
const filePath = path.join(targetPath, `${bundleName}_${language}.properties`);
return result;
}

console.log(`Writing target file [${filePath}]`);
fs.writeFileSync(filePath, unicodeEscape(propertiesText));
}
const targetPath = path.resolve(process.cwd(), path.dirname(target));

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)
);
}
if (!fs.existsSync(targetPath)) {
console.error(`Target path [${targetPath}] doesn't exists`);
} else {
const bundleName = path.basename(target);

const translationTexts = flattenTranslationTexts(sourceObject);
const propertiesText = properties.stringify(translationTexts);
const filePath = path.join(targetPath, `${bundleName}.properties`);

console.log(`Writing target file [${filePath}]`);
fs.writeFileSync(filePath, unicodeEscape(propertiesText));
}


Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@opuscapita/i18n",
"version": "1.2.15-SNAPSHOT",
"version": "1.2.16-EPROC-20779-2-SNAPSHOT",
"description": "OpusCapita simple i18n mechanism",
"main": "lib/index.js",
"files": [
Expand Down
44 changes: 5 additions & 39 deletions src/cli/i18n-js2properties.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ describe("i18n-js to properties CLI", () => {
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"
};
Expand All @@ -58,18 +49,6 @@ const form = {
"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} }
`);
});
Expand All @@ -87,31 +66,18 @@ export default { messages: {header, form} }

expect(
fs.existsSync("tmp/grails-app/i18n/messages_en.properties")
).to.be.equal(true);
).to.be.equal(false);

expect(
fs.existsSync("tmp/grails-app/i18n/messages_de.properties")
).to.be.equal(true);
).to.be.equal(false);

const englishProperties = properties.parse(
fs.readFileSync("tmp/grails-app/i18n/messages_en.properties").toString()
const defaultProperties = properties.parse(
fs.readFileSync("tmp/grails-app/i18n/messages.properties").toString()
);

expect(englishProperties).to.deep.include(
expect(defaultProperties).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 \u00fc"
}
);

const defaultProperties = properties.parse(fs.readFileSync("tmp/grails-app/i18n/messages.properties").toString());
expect(defaultProperties).to.deep.include(englishProperties);
});
});

0 comments on commit 0ff4f44

Please sign in to comment.