From 412cc133de0d8a7de9eb3b665cb9830210d34a93 Mon Sep 17 00:00:00 2001 From: MTRNord Date: Tue, 2 May 2017 18:52:30 +0200 Subject: [PATCH] update language loading logic --- scripts/copy-res.js | 56 ++++++++++++-- src/i18n/languages.json | 5 -- src/vector/index.js | 158 +++++++++++++++++++++++----------------- 3 files changed, 143 insertions(+), 76 deletions(-) delete mode 100644 src/i18n/languages.json diff --git a/scripts/copy-res.js b/scripts/copy-res.js index 010a098c750..3723f087d7d 100755 --- a/scripts/copy-res.js +++ b/scripts/copy-res.js @@ -12,12 +12,28 @@ const COPY_LIST = [ ["node_modules/emojione/assets/svg/*", "webapp/emojione/svg/"], ["node_modules/emojione/assets/png/*", "webapp/emojione/png/"], ["./config.json", "webapp", {directwatch: 1}], - ["src/i18n/**", "webapp/i18n/"], + ["src/i18n/", "webapp/i18n/", {languages: 1}], + ["node_modules/matrix-react-sdk/src/i18n/strings/", "webapp/i18n/", {languages: 1}], + ["node_modules/matrix-react-sdk/src/i18n/global/", "webapp/i18n/", {languages: 1}], ]; const parseArgs = require('minimist'); const Cpx = require('cpx'); const chokidar = require('chokidar'); +const fs = require('fs'); + +//From http://stackoverflow.com/a/20525865/4929236 +function generateFileArray(dir, files_) { + files_ = files_ || []; + var files = fs.readdirSync(dir); + for (var i in files){ + if (files[i] !== "languages.json") { + var name = files[i]; + files_.push(name); + }; + } + return files_; +} const argv = parseArgs( process.argv.slice(2), {} @@ -44,8 +60,32 @@ function next(i, err) { const source = ent[0]; const dest = ent[1]; const opts = ent[2] || {}; + let cpx = undefined; + + if (opts.languages) { + const sourceFiles = generateFileArray(source); + let Sourcelanguages = {}; + if (!fs.existsSync(dest)){ + fs.mkdirSync(dest); + } + sourceFiles.forEach(file => { + const fileContents = fs.readFileSync(source + file).toString(); + Sourcelanguages[file] = JSON.parse(fileContents); + }); + sourceFiles.forEach(file => { + if (!fs.existsSync(dest + file)) { + let o = Object.assign({}, Sourcelanguages[file]); + fs.writeFileSync(dest + file, JSON.stringify(o, null, 4)); + } else { + const fileContents = fs.readFileSync(dest + file).toString(); + let o = Object.assign(JSON.parse(fileContents), Sourcelanguages[file]); + fs.writeFileSync(dest + file, JSON.stringify(o, null, 4)); + } + }); - const cpx = new Cpx.Cpx(source, dest); + } else { + cpx = new Cpx.Cpx(source, dest); + } if (verbose) { cpx.on("copy", (event) => { @@ -70,13 +110,18 @@ function next(i, err) { .on('change', copy) .on('ready', cb) .on('error', errCheck); + } else if (opts.languages) { + if (verbose) { + console.log('don\'t copy language file'); + } + next(i+1, err); } else { cpx.on('watch-ready', cb); cpx.on("watch-error", cb); cpx.watch(); } - } else { - cpx.copy(cb); + } else if (!opts.languages) { + cpx.copy(cb); } } @@ -85,7 +130,6 @@ next(0); // Generate Language List const testFolder = 'src/i18n/'; -const fs = require('fs'); let languages = {}; fs.readdir(testFolder, (err, files) => { files.forEach(file => { @@ -101,5 +145,5 @@ fs.readdir(testFolder, (err, files) => { languages[file] = file; } }); - fs.writeFile('src/i18n/languages.json', JSON.stringify(languages, null, 4), 'utf8'); + fs.writeFile('webapp/i18n/languages.json', JSON.stringify(languages, null, 4), 'utf8'); }) diff --git a/src/i18n/languages.json b/src/i18n/languages.json deleted file mode 100644 index 47ef973dcaf..00000000000 --- a/src/i18n/languages.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "de": "de_DE.json", - "en": "en_EN.json", - "pt_br": "pt_BR.json" -} \ No newline at end of file diff --git a/src/vector/index.js b/src/vector/index.js index f104b4e8b78..136a6b42d62 100644 --- a/src/vector/index.js +++ b/src/vector/index.js @@ -233,80 +233,108 @@ function onLoadCompleted() { } } -function getLanguage(langPath, langCode, callback) { - let response_return = {}; - request( - { method: "GET", url: langPath }, - (err, response, body) => { - if (err || response.status < 200 || response.status >= 300) { - // Lack of a config isn't an error, we should - // just use the defaults. - // Also treat a blank config as no config, assuming - // the status code is 0, because we don't get 404s - // from file: URIs so this is the only way we can - // not fail if the file doesn't exist when loading - // from a file:// URI. - if (response) { - if (response.status == 404 || (response.status == 0 && body == '')) { - resp_raw = {}; +function onAction(payload) { + switch (payload.action) { + case 'set_language': + const language = payload.value; + const i18nFolder = 'i18n/'; + request(i18nFolder + 'languages.json', function(err, response, body) { + function getLanguage(langPath, langCode, callback) { + let response_return = {}; + let resp_raw = {}; + request( + { method: "GET", url: langPath }, + (err, response, body) => { + if (err || response.status < 200 || response.status >= 300) { + // Lack of a config isn't an error, we should + // just use the defaults. + // Also treat a blank config as no config, assuming + // the status code is 0, because we don't get 404s + // from file: URIs so this is the only way we can + // not fail if the file doesn't exist when loading + // from a file:// URI. + if (response) { + if (response.status == 404 || (response.status == 0 && body == '')) { + resp_raw = {}; + } + } + const resp = {err: err, response: resp_raw}; + err = resp['err']; + response_cb = resp['response']; + callback(err, response_cb, langCode); + return; } - } - const resp = {err: err, response: resp_raw}; - err = JSON.parse(resp)['err']; - response_cb = JSON.parse(resp)['response']; - callback(err, response_cb, langCode); - return; - } - // We parse the JSON ourselves rather than use the JSON - // parameter, since this throws a parse error on empty - // which breaks if there's no config.json and we're - // loading from the filesystem (see above). + // We parse the JSON ourselves rather than use the JSON + // parameter, since this throws a parse error on empty + // which breaks if there's no config.json and we're + // loading from the filesystem (see above). - response_return = JSON.parse(body); - callback(null, response_return, langCode); + response_return = JSON.parse(body); + callback(null, response_return, langCode); + return; + } + ); return; } - ); - return; -} -function callbackLanguage(err, langJson, langCode){ - if (err !== null) { - var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); - Modal.createDialog(ErrorDialog, { - title: "Error changing language", - description: "Riot was unable to find the correct Data for the selected Language.", - }); - return; - } else { - console.log("set"); - console.log(langJson); - counterpart.registerTranslations(langCode, langJson); - } -} + function callbackLanguage(err, langJson, langCode){ + if (err !== null) { + var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); + Modal.createDialog(ErrorDialog, { + title: "Error changing language", + description: "Riot was unable to find the correct Data for the selected Language.", + }); + return; + } else { + counterpart.registerTranslations(langCode, langJson); + } + } -function onAction(payload) { - switch (payload.action) { - case 'set_language': - const i18nFolder = 'i18n/'; - const languages = require('../i18n/languages.json'); - if (languages.hasOwnProperty(payload.value)) { - getLanguage(i18nFolder + languages[payload.value], payload.value, callbackLanguage); - if (payload.value.indexOf("-") > -1) { - counterpart.setLocale(payload.value.split('-')[0]); - } else if (payload.value == 'pt_br') { - counterpart.setLocale('pt_br'); + let languages = {}; + if(err){ + console.error(err); + }else { + languages = JSON.parse(body); + } + + if (!language){ + const language = navigator.languages[0] || navigator.language || navigator.userLanguage; + if (languages.hasOwnProperty(language)) { + getLanguage(i18nFolder + languages[language], language, callbackLanguage); + if (language.indexOf("-") > -1) { + counterpart.setLocale(language.split('-')[0]); + UserSettingsStore.setLocalSetting('language', language.split('-')[0]); + } else if (language == 'pt-br') { + counterpart.setLocale('pt-br'); + UserSettingsStore.setLocalSetting('language', 'pt-br'); + } else { + counterpart.setLocale(language); + UserSettingsStore.setLocalSetting('language', language); + } + } else { + getLanguage(i18nFolder + languages['en'], 'en', callbackLanguage); + counterpart.setFallbackLocale('en'); + } } else { - counterpart.setLocale(payload.value); + if (languages.hasOwnProperty(language)) { + getLanguage(i18nFolder + languages[language], language, callbackLanguage); + if (language.indexOf("-") > -1) { + counterpart.setLocale(language.split('-')[0]); + UserSettingsStore.setLocalSetting('language', language.split('-')[0]); + } else if (language == 'pt-br') { + counterpart.setLocale('pt-br'); + UserSettingsStore.setLocalSetting('language', 'pt-br'); + } else { + counterpart.setLocale(language); + UserSettingsStore.setLocalSetting('language', language); + } + } else { + getLanguage(i18nFolder + languages['en'], 'en', callbackLanguage); + counterpart.setFallbackLocale('en'); + } } - } else { - var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); - Modal.createDialog(ErrorDialog, { - title: "Error changing language", - description: "Riot was unable to find the correct Data for the selected Language.", - }); - } + }) break; } }