Skip to content

Commit

Permalink
Check English translation if language doesn't exist (#361)
Browse files Browse the repository at this point in the history
Need this because of #316
  • Loading branch information
JoelMarcey authored Dec 27, 2017
1 parent 5805149 commit 5a77f18
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/server/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@ function setLanguage(lang) {
language = lang;
}

function doesTranslationExist(str, lang) {
return (
translation[lang] &&
translation[lang]['pages-strings'] &&
translation[lang]['pages-strings'][str]
);
}

function translate(str) {
if (!language || language === '') {
// Check English, just in case; otherwise, just return the raw string back
if (doesTranslationExist(str, 'en')) {
return parseEscapeSequences(translation['en']['pages-strings'][str]);
}
return str;
}

if (
!translation[language] ||
!translation[language]['pages-strings'] ||
!translation[language]['pages-strings'][str]
) {
if (!doesTranslationExist(str, language)) {
// if a translated string doesn't exist, but english does then fallback
if (
translation['en'] &&
translation['en']['pages-strings'] &&
translation['en']['pages-strings'][str]
) {
if (doesTranslationExist(str, 'en')) {
console.error(
"Could not find a string translation in '" +
language +
Expand Down

2 comments on commit 5a77f18

@neilsutcliffe
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. I've made a similar change in https://github.com/neilsutcliffe/Docusaurus/blob/5838e66ae6ea37c941bd8a58bd4c12b6fe0878a8/lib/server/translate.js

It has the concept of 'default.json' being a valid translation, but it falls back to english if default isn't there.

@JoelMarcey
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@neilsutcliffe Hi. Yours, on the surface, may be a bit cleaner and tidier :)

I wanted to get something correct-ish so I could get 1.0.4 which fixed some bugs.

Maybe 1.0.5 can be a refactor-focus.

Please sign in to comment.