Skip to content

Commit

Permalink
feat(translations): generate words on translation
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed May 17, 2018
1 parent 520a359 commit d24c793
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"from": "resources/icons/19x19.png",
"to": "icons/19x19.png"
},
"translations",
"CHANGELOG.md"
],
"directories": {
Expand Down
28 changes: 23 additions & 5 deletions src/app/translation.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
7 changes: 4 additions & 3 deletions src/background/menu/config_menu_template.js
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions translations/en.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "English",
"nameOriginal": "English",
"translations":
{
"welcome": "welcome"
}
"name": "English",
"nameOriginal": "English",
"translations": {
"Language": "Language",
"welcome": "welcome"
}
}
13 changes: 6 additions & 7 deletions translations/ru.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "Russian",
"nameOriginal": "Русский",
"translations":
{
"welcome": "приветствие",
"Language": "Язык"
}
"name": "Russian",
"nameOriginal": "Русский",
"translations": {
"Language": "Язык",
"welcome": "Добро пожаловать"
}
}

0 comments on commit d24c793

Please sign in to comment.