-
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(translations): basic translations support
- Loading branch information
Showing
7 changed files
with
113 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
const fs = require('fs') | ||
let dictionary = {} | ||
|
||
function loadJSON(file, callback) { | ||
if(fs) | ||
{ | ||
callback(JSON.parse(fs.readFileSync(file, 'utf8'))) | ||
} | ||
else | ||
{ | ||
const xobj = new XMLHttpRequest(); | ||
xobj.overrideMimeType("application/json"); | ||
xobj.open('GET', file, true); | ||
xobj.onreadystatechange = function() { | ||
if (xobj.readyState == 4 && xobj.status == 200) { | ||
// .open will NOT return a value but simply returns undefined in async mode so use a callback | ||
callback(JSON.parse(xobj.responseText)); | ||
} | ||
} | ||
xobj.send(null); | ||
} | ||
} | ||
|
||
const changeLanguage = (lang, callback) => { | ||
loadJSON(`translations/${lang}.json`, (data) => { | ||
dictionary = data.translations | ||
if(callback) | ||
callback() | ||
}) | ||
} | ||
|
||
export { changeLanguage } | ||
|
||
export default (word) => { | ||
const translation = dictionary[word] | ||
if(translation === undefined) | ||
{ | ||
return word | ||
} | ||
return translation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "English", | ||
"nameOriginal": "English", | ||
"translations": | ||
{ | ||
"welcome": "welcome" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "Russian", | ||
"nameOriginal": "Русский", | ||
"translations": | ||
{ | ||
"welcome": "приветствие", | ||
"Language": "Язык" | ||
} | ||
} |