feat(translations): basic translations support

This commit is contained in:
Alexey Kasyanchuk
2018-05-17 02:09:43 +03:00
parent 57c1d0d322
commit feff322592
7 changed files with 113 additions and 16 deletions

View File

@ -5,10 +5,13 @@ import PagesPie from './pages-pie.js';
//import registerServiceWorker from './registerServiceWorker';
import injectTapEventPlugin from 'react-tap-event-plugin';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import __, { changeLanguage } from './translation'
import {Header} from './header'
import Footer from './footer'
window.__ = __
if(typeof WEB !== 'undefined')
{
@ -130,6 +133,10 @@ class App extends Component {
window.p2pStatus = status
this.forceUpdate()
})
window.torrentSocket.on('changeLanguage', (lang) => {
changeLanguage(lang, () => this.forceUpdate())
})
}
componentWillUnmount() {
appReady = false;
@ -138,6 +145,7 @@ class App extends Component {
return (
<MuiThemeProvider>
<div>
{ __('welcome') }
{
((window.currentWindow && !window.currentWindow.isModal()) || typeof WEB !== 'undefined')
&&

41
src/app/translation.js Normal file
View File

@ -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
}