feat(translations): generate words on translation
This commit is contained in:
parent
127192b14d
commit
0d39bbd9c3
@ -29,6 +29,7 @@
|
|||||||
"from": "resources/icons/19x19.png",
|
"from": "resources/icons/19x19.png",
|
||||||
"to": "icons/19x19.png"
|
"to": "icons/19x19.png"
|
||||||
},
|
},
|
||||||
|
"translations",
|
||||||
"CHANGELOG.md"
|
"CHANGELOG.md"
|
||||||
],
|
],
|
||||||
"directories": {
|
"directories": {
|
||||||
|
@ -1,16 +1,24 @@
|
|||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
|
const env = typeof WEB !== 'undefined' ? false : require('env')
|
||||||
let dictionary = {}
|
let dictionary = {}
|
||||||
|
|
||||||
|
const translationsDir = () => {
|
||||||
|
if(env.name == 'production')
|
||||||
|
return process.resourcesPath + '/translations'
|
||||||
|
else
|
||||||
|
return 'translations'
|
||||||
|
}
|
||||||
|
|
||||||
function loadJSON(file, callback) {
|
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
|
else
|
||||||
{
|
{
|
||||||
const xobj = new XMLHttpRequest();
|
const xobj = new XMLHttpRequest();
|
||||||
xobj.overrideMimeType("application/json");
|
xobj.overrideMimeType("application/json");
|
||||||
xobj.open('GET', file, true);
|
xobj.open('GET', `translations/${file}`, true);
|
||||||
xobj.onreadystatechange = function() {
|
xobj.onreadystatechange = function() {
|
||||||
if (xobj.readyState == 4 && xobj.status == 200) {
|
if (xobj.readyState == 4 && xobj.status == 200) {
|
||||||
// .open will NOT return a value but simply returns undefined in async mode so use a callback
|
// .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) => {
|
const changeLanguage = (lang, callback) => {
|
||||||
loadJSON(`translations/${lang}.json`, (data) => {
|
loadJSON(`${lang}.json`, (data) => {
|
||||||
dictionary = data.translations
|
dictionary = data.translations
|
||||||
if(callback)
|
if(callback)
|
||||||
callback()
|
callback()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export { changeLanguage }
|
export { changeLanguage, translationsDir }
|
||||||
|
|
||||||
export default (word) => {
|
export default (word) => {
|
||||||
const translation = dictionary[word]
|
const translation = dictionary[word]
|
||||||
if(translation === undefined)
|
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 word
|
||||||
}
|
}
|
||||||
return translation
|
return translation
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { app, BrowserWindow } from "electron";
|
import { app, BrowserWindow } from "electron";
|
||||||
import fs from 'fs'
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import __ from '../../app/translation'
|
import __, { translationsDir } from '../../app/translation'
|
||||||
|
|
||||||
export const settingsMenuTemplateFunc = (config, onLanguageChange) => ({
|
export const settingsMenuTemplateFunc = (config, onLanguageChange) => ({
|
||||||
label: "Settings",
|
label: "Settings",
|
||||||
@ -24,8 +24,9 @@ export const settingsMenuTemplateFunc = (config, onLanguageChange) => ({
|
|||||||
label: __("Language"),
|
label: __("Language"),
|
||||||
submenu: (() => {
|
submenu: (() => {
|
||||||
const translations = []
|
const translations = []
|
||||||
fs.readdirSync('translations').forEach(translation => {
|
const translationsDirectory = translationsDir()
|
||||||
const translationJson = JSON.parse(fs.readFileSync(`translations/${translation}`, 'utf8'))
|
fs.readdirSync(translationsDirectory).forEach(translation => {
|
||||||
|
const translationJson = JSON.parse(fs.readFileSync(`${translationsDirectory}/${translation}`, 'utf8'))
|
||||||
const lang = path.basename(translation, '.json')
|
const lang = path.basename(translation, '.json')
|
||||||
translations.push({
|
translations.push({
|
||||||
label: translationJson.nameOriginal,
|
label: translationJson.nameOriginal,
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "English",
|
"name": "English",
|
||||||
"nameOriginal": "English",
|
"nameOriginal": "English",
|
||||||
"translations":
|
"translations": {
|
||||||
{
|
"Language": "Language",
|
||||||
"welcome": "welcome"
|
"welcome": "welcome"
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,9 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "Russian",
|
"name": "Russian",
|
||||||
"nameOriginal": "Русский",
|
"nameOriginal": "Русский",
|
||||||
"translations":
|
"translations": {
|
||||||
{
|
"Language": "Язык",
|
||||||
"welcome": "приветствие",
|
"welcome": "Добро пожаловать"
|
||||||
"Language": "Язык"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user