feat(translations): generate words on translation

This commit is contained in:
Alexey Kasyanchuk 2018-05-18 00:40:33 +03:00
parent 127192b14d
commit 0d39bbd9c3
5 changed files with 40 additions and 21 deletions

View File

@ -29,6 +29,7 @@
"from": "resources/icons/19x19.png",
"to": "icons/19x19.png"
},
"translations",
"CHANGELOG.md"
],
"directories": {

View File

@ -1,16 +1,24 @@
const fs = require('fs')
const env = typeof WEB !== 'undefined' ? false : require('env')
let dictionary = {}
const translationsDir = () => {
if(env.name == 'production')
return process.resourcesPath + '/translations'
else
return 'translations'
}
function loadJSON(file, callback) {
if(fs)
if(fs && env)
{
callback(JSON.parse(fs.readFileSync(file, 'utf8')))
callback(JSON.parse(fs.readFileSync(`${translationsDir()}/${file}`, 'utf8')))
}
else
{
const xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open('GET', file, true);
xobj.open('GET', `translations/${file}`, true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == 200) {
// .open will NOT return a value but simply returns undefined in async mode so use a callback
@ -22,19 +30,29 @@ function loadJSON(file, callback) {
}
const changeLanguage = (lang, callback) => {
loadJSON(`translations/${lang}.json`, (data) => {
loadJSON(`${lang}.json`, (data) => {
dictionary = data.translations
if(callback)
callback()
})
}
export { changeLanguage }
export { changeLanguage, translationsDir }
export default (word) => {
const translation = dictionary[word]
if(translation === undefined)
{
if(fs && env && env.name === "development")
{
dictionary[word] = word
fs.readdirSync('translations').forEach(translation => {
console.log('update translation in file', translation)
const translationJson = JSON.parse(fs.readFileSync(`translations/${translation}`, 'utf8'))
translationJson.translations[word] = word
fs.writeFileSync(`translations/${translation}`, JSON.stringify(translationJson, null, 4), 'utf8');
})
}
return word
}
return translation

View File

@ -1,7 +1,7 @@
import { app, BrowserWindow } from "electron";
import fs from 'fs'
import path from 'path'
import __ from '../../app/translation'
import __, { translationsDir } from '../../app/translation'
export const settingsMenuTemplateFunc = (config, onLanguageChange) => ({
label: "Settings",
@ -24,8 +24,9 @@ export const settingsMenuTemplateFunc = (config, onLanguageChange) => ({
label: __("Language"),
submenu: (() => {
const translations = []
fs.readdirSync('translations').forEach(translation => {
const translationJson = JSON.parse(fs.readFileSync(`translations/${translation}`, 'utf8'))
const translationsDirectory = translationsDir()
fs.readdirSync(translationsDirectory).forEach(translation => {
const translationJson = JSON.parse(fs.readFileSync(`${translationsDirectory}/${translation}`, 'utf8'))
const lang = path.basename(translation, '.json')
translations.push({
label: translationJson.nameOriginal,

View File

@ -1,8 +1,8 @@
{
"name": "English",
"nameOriginal": "English",
"translations":
{
"translations": {
"Language": "Language",
"welcome": "welcome"
}
}

View File

@ -1,9 +1,8 @@
{
"name": "Russian",
"nameOriginal": "Русский",
"translations":
{
"welcome": "приветствие",
"Language": "Язык"
"translations": {
"Language": "Язык",
"welcome": "Добро пожаловать"
}
}