Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Add supoort for multi language #158

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ upload-*
named-*

/node_modules/
/.tmp/
4,735 changes: 4,735 additions & 0 deletions languages/en.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"version": "1.0.0",
"description": "Database for 5e-srd-api",
"scripts": {
"build": "node ./scripts/build.js",
"create-lang": "node ./scripts/create-language.js",
"lint": "eslint . --ext .js,.json",
"db:refresh": "./scripts/db-refresh.sh"
},
Expand Down
37 changes: 37 additions & 0 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var fs = require('fs')

const srcFiles = fs.readdirSync('src');

if (!fs.existsSync('.tmp')){
fs.mkdirSync('.tmp');
}

fs.readdir('languages', (err, languageFileNames) => {
if (err) {
return console.log(err);
}

languageFileNames.forEach(languageFileName => {
const languageData = fs.readFileSync(`languages/${languageFileName}`, 'utf8');

if (!languageData) {
return console.log(err);
}

const languageDataJSON = JSON.parse(languageData);

srcFiles.forEach(srcFileName => {
let srcFileData = fs.readFileSync(`src/${srcFileName}`, 'utf8');

Object.entries(languageDataJSON).forEach(entry => {
srcFileData = srcFileData.replace(new RegExp(`"${entry[0]}"`, 'g'), `"${entry[1]}"`);
});

srcFileData = srcFileData.replace(/"url": "\/api\/(.*)"/g, `"url": "/api/${languageFileName.split('.')[0]}/$1"`);

fs.writeFile(`.tmp/${srcFileName.split('.')[0]}_${languageFileName}`, srcFileData, 'utf8', err => {
if (err) return console.log(err);
});
});
});
});
101 changes: 101 additions & 0 deletions scripts/create-language.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
var fs = require('fs')

String.prototype.trimLeft = function(charlist) {
if (charlist === undefined)
charlist = "\s";

return this.replace(new RegExp("^[" + charlist + "]+"), "");
};

String.prototype.trimRight = function(charlist) {
if (charlist === undefined)
charlist = "\s";

return this.replace(new RegExp("[" + charlist + "]+$"), "");
};

fs.readdir('src', (err, fileNames) => {
if (err) {
return console.log(err);
}

const languageKeysArray = [];
let languageKeysJSON = {};

fileNames.forEach(fileName => {
const data = fs.readFileSync(`src/${fileName}`, 'utf8');

if (!data) {
return console.log(err);
}

const fileLanguageKeysJSON = {};

const dataJSON = JSON.parse(data);

const iterateJSON = function(obj) {
if (typeof obj !== 'object') {
return obj;
}

for(var key in obj) {
if (
(key === 'name' || key === 'class' || key === 'full_name' || key === 'desc')
&& typeof obj[key] === 'string'
){
const string = obj[key];

const newKey = 'LANG-KEY-' + string.replace(/\s+/g, '-')
.replace(/[\:\'\"\,\.\(\))]*/g, '')
.toLowerCase().trimLeft('-').trimRight('.');

obj[key] = newKey;

fileLanguageKeysJSON[newKey] = string;
}
else if (key === 'desc' && obj[key] instanceof Array) {
obj[key] = obj[key].map(item => {
const newKey = 'LANG-KEY-' + item.replace(/\s+/g, '-')
.replace(/[\:\'\"\,\.\(\))]*/g, '')
.replace(/-{2,}/g, '-')
.toLowerCase().trimLeft('-').trimRight('.');

fileLanguageKeysJSON[newKey] = item;

return newKey;
});
}

obj[key] = iterateJSON(obj[key]);
}

return obj;
};

iterateJSON(dataJSON);

Object.assign(languageKeysJSON, fileLanguageKeysJSON);

fs.writeFile(`src/${fileName}`, JSON.stringify(dataJSON, null, '\t'), 'utf8', err => {
if (err) return console.log(err);
});
});

Object.entries(languageKeysJSON).forEach(entry => {
languageKeysArray.push([entry[0], entry[1]]);
})

languageKeysJSON = {};

languageKeysArray.sort().forEach(item => {
languageKeysJSON[item[0]] = item[1]
});

if (!fs.existsSync('languages')){
fs.mkdirSync('languages');
}

fs.writeFile('languages/en.json', JSON.stringify(languageKeysJSON, null, '\t'), 'utf8', err => {
if (err) return console.log(err);
});
});
Loading