Skip to content

Commit

Permalink
update language loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MTRNord committed May 2, 2017
1 parent e30379c commit 412cc13
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 76 deletions.
56 changes: 50 additions & 6 deletions scripts/copy-res.js
Original file line number Diff line number Diff line change
Expand Up @@ -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), {}
Expand All @@ -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) => {
Expand All @@ -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);
}
}

Expand All @@ -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 => {
Expand All @@ -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');
})
5 changes: 0 additions & 5 deletions src/i18n/languages.json

This file was deleted.

158 changes: 93 additions & 65 deletions src/vector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 412cc13

Please sign in to comment.