Skip to content

Commit

Permalink
Reverted translation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
neilsutcliffe committed Dec 27, 2017
1 parent f26e4f5 commit d9ccfb2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 41 deletions.
73 changes: 34 additions & 39 deletions lib/server/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@
*/

const translation = require('./translation.js');
const chalk = require('chalk');

let language = undefined;

const env = require('./env.js');
let language = 'en';

/* handle escaped characters that get converted into json strings */
function parseEscapeSequences(str) {
Expand All @@ -25,48 +22,46 @@ function parseEscapeSequences(str) {
.replace(new RegExp('\\\\', 'g'), '\\');
}

const setLanguage = lang => {
function setLanguage(lang) {
language = lang;
};

const translate = str => {
const translationFile = language ? language : 'default';
let result;

// Fallback order. Eventually we can get rid of en.
[translationFile, 'default', 'en'].every(language => {
let found =
translation[language] &&
translation[language]['pages-strings'] &&
translation[language]['pages-strings'][str];

let keepLooking = found == undefined;
}

if (keepLooking) return true;
function translate(str) {
if (!language || language === '') {
return str;
}

if (language != translationFile && translationFile != 'en') {
// We only warn when falling back from a non-english language
// Because this module is recreated every page load, it is impossible to cache this without using a global variable. Spam is inevitable
if (
!translation[language] ||
!translation[language]['pages-strings'] ||
!translation[language]['pages-strings'][str]
) {
// if a translated string doesn't exist, but english does then fallback
if (
translation['en'] &&
translation['en']['pages-strings'] &&
translation['en']['pages-strings'][str]
) {
console.error(
chalk.yellow(
`Warning: Translation strings are missing in '${translationFile}' for string ${str} Using '${language}' instead.`
)
"Could not find a string translation in '" +
language +
"' for string '" +
str +
"'. Using English version instead."
);
}

result = parseEscapeSequences(found);
return false;
});

if (result) return result;

throw new Error(
`Text that you've identified for translation '${str}' hasn't been added to the global list in 'default.json'.
To solve this problem run 'yarn write-translations'.`
);
};
return parseEscapeSequences(translation['en']['pages-strings'][str]);
}
throw new Error(
"Text that you've identified for translation ('" +
str +
"') hasn't been added to the global list in 'en.json'. To solve this problem run 'yarn write-translations'."
);
}
return parseEscapeSequences(translation[language]['pages-strings'][str]);
}

module.exports = {
setLanguage: setLanguage,
translate: translate,
};
};
4 changes: 2 additions & 2 deletions lib/write-translations.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function execute() {
currentTranslations['localized-strings']
);
writeFileAndCreateFolder(
CWD + '/i18n/default.json',
CWD + '/i18n/en.json',
JSON.stringify(
Object.assign(
{
Expand All @@ -180,4 +180,4 @@ function execute() {

execute();

module.exports = execute;
module.exports = execute;

0 comments on commit d9ccfb2

Please sign in to comment.