---
applications:
- name: i18nCsvConverterApp
host: i18nCsvConverter
path: i18nCsvConverterApp
memory: 128M
cd i18nCsvConverterApp
npm init
{
"name": "i18ncsvconverterapp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
{
"name": "i18ncsvconverterapp",
"version": "1.0.0",
"description": "",
"main": "createCsv.js",
"scripts": {
"createCsv": "node createCsv.js",
"createI18n": "node createI18n.js"
},
"author": "",
"license": "ISC"
}
// The purpose of "use strict" is to indicate that the code should be executed in "strict mode". With strict mode, you cannot, for example, use undeclared variables.
'use strict';
// The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.
const fs = require('fs');
// NPM Module to recursive read directory async (non-blocking).
const rra = require('recursive-readdir-async');
// Convert .properties files to JSON (using JavaScript).
// The function propertiesToJSON takes a string and returns a JavaScript object.
const propertiesToJSON = require("properties-to-json");
// This module makes easy to convert JSON to CSV and its very customizable.
const jsonexport = require('jsonexport');
// Convert all the backslashes in the path to your project to forward slashes
const filePath = process.argv[2].replace(/\\/g, "/");
console.log("Filepath to search for i18n files: " + filePath);
npm i recursive-readdir-async
npm i properties-to-json
npm i jsonexport
// Recursive reader configuration options
const options = {
mode: rra.LIST,
recursive: true,
stats: false,
ignoreFolders: true,
extensions: false,
deep: false,
realPath: true,
normalizePath: true,
include: [".properties"],
exclude: [],
readContent: false,
encoding: 'base64'
}
rra.list(filePath, options).then(function (list) {
});
const i18nFiles = list.filter(file => file.path.substring(file.path.length - 5) === "/i18n");
let aPromises = i18nFiles.map(file => {
let appPath = file.fullname.split(filePath)[1].replace(/(_[a-zA-Z]{2}){0,2}.properties/, "");
let language = file.fullname.match(/(_[a-zA-Z]{2}){0,2}.properties/)[0].replace(".properties", "").substring(1) || "default";
let promise = new Promise(function (resolve, reject) {
fs.readFile(file.fullname, 'utf-8', function (err, data) {
resolve({ appPath: appPath, language: language, data: propertiesToJSON(data) });
});
});
return promise;
});
Promise.all(aPromises).then(function (result) {
let aTranslationEntries = [];
result.forEach(function (file) {
Object.keys(file.data).forEach(function (key) {
let foundTranslation = aTranslationEntries.filter(entry => entry.key === key && entry.appName === file.appPath)[0];
if (foundTranslation) {
foundTranslation[[file.language]] = file.data[key];
}
else {
aTranslationEntries.push({
appName: file.appPath,
key: key,
[[file.language]]: file.data[key]
});
}
});
});
});
jsonexport(aTranslationEntries, function (err, csv) {
const sPath = filePath.match(/^(.*[\\\/])[^\\\/]*$/)[1] + "translationFileI18n.csv";
// console.log(csv);
// IMPORTANT to pass \ufeff to tell csv that it is UTF-8
fs.writeFile(sPath, "\ufeff" + csv, (err) => {
if (err) {
console.log(err); // Do something to handle the error or just throw it
throw new Error(err);
}
console.log('Success!');
});
});
'use strict';
const fs = require('fs');
const rra = require('recursive-readdir-async');
const propertiesToJSON = require("properties-to-json");
const jsonexport = require('jsonexport');
const filePath = process.argv[2].replace(/\\/g, "/");
console.log("Filepath to search for i18n files: " + filePath);
const options = {
mode: rra.LIST,
recursive: true,
stats: false,
ignoreFolders: true,
extensions: false,
deep: false,
realPath: true,
normalizePath: true,
include: [".properties"],
exclude: [],
readContent: false,
encoding: 'base64'
}
rra.list(filePath, options).then(function (list) {
const i18nFiles = list.filter(file => file.path.substring(file.path.length - 5) === "/i18n");
let aPromises = i18nFiles.map(file => {
let appPath = file.fullname.split(filePath)[1].replace(/(_[a-zA-Z]{2}){0,2}.properties/, "");
let language = file.fullname.match(/(_[a-zA-Z]{2}){0,2}.properties/)[0].replace(".properties", "").substring(1) || "default";
let promise = new Promise(function (resolve, reject) {
fs.readFile(file.fullname, 'utf-8', function (err, data) {
resolve({ appPath: appPath, language: language, data: propertiesToJSON(data) });
});
});
return promise;
});
Promise.all(aPromises).then(function (result) {
let aTranslationEntries = [];
result.forEach(function (file) {
Object.keys(file.data).forEach(function (key) {
let foundTranslation = aTranslationEntries.filter(entry => entry.key === key && entry.appName === file.appPath)[0];
if (foundTranslation) {
foundTranslation[[file.language]] = file.data[key];
}
else {
aTranslationEntries.push({
appName: file.appPath,
key: key,
[[file.language]]: file.data[key]
});
}
});
});
jsonexport(aTranslationEntries, function (err, csv) {
const sPath = filePath.match(/^(.*[\\\/])[^\\\/]*$/)[1] + "translationFileI18n.csv";
// console.log(csv);
// IMPORTANT to pass \ufeff to tell csv that it is UTF-8
fs.writeFile(sPath, "\ufeff" + csv, (err) => {
if (err) {
console.log(err); // Do something to handle the error or just throw it
throw new Error(err);
}
// console.log('Success!');
});
});
});
});
npm run createCsv "the-full-path-to-your-project-including-the-project-itself"
'use strict';
// csvtojson module is a comprehensive nodejs csv parser to convert csv to json or column arrays.
const csvToJson = require('csvtojson');
// Parse the json objects to properties for a .properties file
const jsonToProperties = require("properties-file");
// / The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.
const fs = require('fs');
// Replace all the backslashes to forward slashes
const filePath = process.argv[2].replace(/\\/g, "/");
const filePathOutput = process.argv[3].replace(/\\/g, "/");
console.log("Filepath to CSV File: " + filePath);
npm i csvtojson
npm i properties-file
csvToJson().fromFile(filePath).then((jsonObj) => {
});
const result = jsonObj.reduce(function (accum, element) {
}, []);
const result = jsonObj.reduce(function (accum, element) {
const o = null;
let appName = accum.find(obj => {
return obj.appName === element.appName;
});
return accum;
}, []);
if (!appName) {
// create object and add to right langauge
let app = {
appName: element.appName,
}
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName' && l !== 'key' });
aLanguages.forEach(function (lang) {
let key = element.key;
app[lang] = {};
app[lang][key] = element[lang];
});
accum.push(app);
}
const result = jsonObj.reduce(function (accum, element) {
const o = null;
let appName = accum.find(obj => {
return obj.appName === element.appName;
});
if (!appName) {
// create object and add to right langauge
let app = {
appName: element.appName,
}
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName' && l !== 'key' });
aLanguages.forEach(function (lang) {
let key = element.key;
app[lang] = {};
app[lang][key] = element[lang];
});
accum.push(app);
}
else {
// add to right langauge array
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName' && l !== 'key' });
aLanguages.forEach(function (lang) {
let key = element.key;
appName[lang][key] = element[lang];
});
}
return accum;
}, []);
result.forEach(function (appFile) {
let aLanguages = Object.keys(appFile);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName'});
let appName = appFile.appName;
let fullFinalOutputPath = null;
aLanguages.forEach(function (lang) {
let fullFinalOutputPath = filePathOutput + appName ;
if(lang === "default") {
fullFinalOutputPath = fullFinalOutputPath + ".properties";
}
else {
fullFinalOutputPath = fullFinalOutputPath + "_" + lang +".properties";
}
fs.writeFile(fullFinalOutputPath, jsonToProperties.stringify(appFile[lang]), (err) => {
if (err) {
console.log(err);
throw new Error(err);
}
console.log('Success!');
});
});
});
'use strict';
const csvToJson = require('csvtojson');
const jsonToProperties = require("properties-file");
const fs = require('fs');
const filePath = process.argv[2].replace(/\\/g, "/");
const filePathOutput = process.argv[3].replace(/\\/g, "/");
console.log("Filepath to CSV File: " + filePath);
csvToJson().fromFile(filePath).then((jsonObj) => {
const result = jsonObj.reduce(function (accum, element) {
const o = null;
let appName = accum.find(obj => {
return obj.appName === element.appName;
});
if (!appName) {
// create object and add to right langauge
let app = {
appName: element.appName,
}
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName' && l !== 'key' });
aLanguages.forEach(function (lang) {
let key = element.key;
app[lang] = {};
app[lang][key] = element[lang];
});
accum.push(app);
}
else {
// add to right langauge array
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName' && l !== 'key' });
aLanguages.forEach(function (lang) {
let key = element.key;
appName[lang][key] = element[lang];
});
}
return accum;
}, []);
result.forEach(function (appFile) {
let aLanguages = Object.keys(appFile);
aLanguages = aLanguages.filter(function (l) { return l !== 'appName'});
let appName = appFile.appName;
let fullFinalOutputPath = null;
aLanguages.forEach(function (lang) {
let fullFinalOutputPath = filePathOutput + appName ;
if(lang === "default") {
fullFinalOutputPath = fullFinalOutputPath + ".properties";
}
else {
fullFinalOutputPath = fullFinalOutputPath + "_" + lang +".properties";
}
fs.writeFile(fullFinalOutputPath, jsonToProperties.stringify(appFile[lang]), (err) => {
if (err) {
console.log(err);
throw new Error(err);
}
console.log('Success!');
});
});
});
});
npm run createI18n "the-full-path-to-your-csv-translation-file-including-the-translation-csv-file" "the-full-path-to-your-project-including-the-project-itself"
'b�b�'
function padWithLeadingZeros(string) {
return new Array(5 - string.length).join("0") + string;
}
function unicodeCharEscape(charCode) {
return "\\u" + padWithLeadingZeros(charCode.toString(16));
}
function unicodeEscape(string) {
return string.split("")
.map(function (char) {
var charCode = char.charCodeAt(0);
return charCode > 127 ? unicodeCharEscape(charCode) : char;
})
.join("");
}
var specialStr = 'ipsum áá éé lore';
var encodedStr = unicodeEscape(specialStr);
ipsum \\u00e1\\u00e1 \\u00e9\\u00e9 lore
npm install unicode-escape
const unicodeToJsEscape = require('unicode-escape');
unicodeToJsEscape('pasta');
\u0070\u0061\u0073\u0074\u0061
const unicodeToJsEscape = require('unicode-escape');
else {
// add to right langauge array
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) {
return l !== 'appName' && l !== 'key'
});
aLanguages.forEach(function (lang) {
let key = element.key;
appName[lang][key] = unicodeEscape(element[lang]);
});
}
key1=value
=
key2=value
else {
// add to right langauge array
let aLanguages = Object.keys(element);
aLanguages = aLanguages.filter(function (l) {
return l !== 'appName' && l !== 'key'
});
aLanguages.forEach(function (lang) {
let key = element.key;
if (key) {
appName[lang][key] = unicodeEscape(element[lang]);
}
});
}
function unicodeEscape(string) {
return string.split("")
.map(function (char) {
var charCode = char.charCodeAt(0);
return charCode > 127 || char === "'" ? unicodeCharEscape(charCode) : char;
})
.join("");
}
file.data[key].replace(/(\\[a-zA-Z0-9]{5}):*/g, function (string) {
return JSON.parse('"' + string + '"')
})
let aTranslationEntries = [];
result.forEach(function (file) {
Object.keys(file.data).forEach(function (key) {
let foundTranslation = aTranslationEntries.filter(entry => entry.key === key && entry.appName === file.appPath)[0];
if (foundTranslation) {
foundTranslation[[file.language]] = file.data[key].replace(/(\\[a-zA-Z0-9]{5}):*/g, function (string) {
return JSON.parse('"' + string + '"')
});
} else {
aTranslationEntries.push({
appName: file.appPath,
key: key,
[
[file.language]
]: file.data[key].replace(/(\\[a-zA-Z0-9]{5}):*/g, function (string) {
return JSON.parse('"' + string + '"')
})
});
}
});
});
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
6 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 |