Skip to content

Commit

Permalink
Merge pull request #62 from Zuehlke/58-maintenance
Browse files Browse the repository at this point in the history
#58 use polyglot for translations
  • Loading branch information
xeronimus authored Jun 13, 2020
2 parents f2d61d2 + 471e645 commit 085980e
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 57 deletions.
4 changes: 2 additions & 2 deletions client/app/services/clientActionReducer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import clientSettingsStore from '../store/clientSettingsStore';
import translator from './translator';
import {
TOGGLE_BACKLOG,
TOGGLE_USER_MENU,
Expand Down Expand Up @@ -86,7 +85,8 @@ export default function clientActionReducer(state, action) {
case SET_LANGUAGE: {
const language = action.language;
clientSettingsStore.setPresetLanguage(language);
return {...state, language, translator: (key) => translator(key, language)};
state.setLanguage(language);
return {...state, language};
}

case LOCATION_CHANGED: {
Expand Down
36 changes: 9 additions & 27 deletions client/app/services/translator.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import log from 'loglevel';
import Polyglot from 'node-polyglot';

import translationsEN from '../assets/i18n/en.json';
import translationsDE from '../assets/i18n/de.json';
export default function translatorFactory(dictionary, language) {
const polyglot = new Polyglot({phrases: dictionary[language]});

const dictionary = {
en: translationsEN,
de: translationsDE
};
return {
t: translatorFunction,
setLanguage: (lang) => polyglot.replace(dictionary[lang])
};

/**
* Performs a lookup of a translation with the given key for the given language.
*
* @param translationKey
* @param language
* @returns {string}
*/
export default function lookup(translationKey, language) {
const translations = dictionary[language];

if (!translations) {
log.error(`Unknown language '${language}'`);
return `!!!${translationKey}!!!`;
}

const translation = translations[translationKey];
if (!translation) {
return `!!!${translationKey}!!!`;
function translatorFunction(translationKey, data) {
return polyglot.t(translationKey, {...data, _: `!!!${translationKey}!!!`});
}

return translation;
}
16 changes: 14 additions & 2 deletions client/app/store/initialState.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import clientSettingsStore from './clientSettingsStore';
import translator from '../services/translator';
import translatorFactory from '../services/translator';

import translationsEN from '../../app/assets/i18n/en.json';
import translationsDE from '../../app/assets/i18n/de.json';

const DEFAULT_LANGUAGE = 'en';
const userLanguage = clientSettingsStore.getPresetLanguage();

const {t, setLanguage} = translatorFactory(
{
en: translationsEN,
de: translationsDE
},
userLanguage || DEFAULT_LANGUAGE
);

/**
* The initial state that is loaded into the redux store on (client) application load.
*/
Expand All @@ -30,7 +41,8 @@ const INITIAL_STATE = {
actionLog: [], // will contain human readable "log messages" of actions that did take place in the current room
pendingCommands: {}, // will contain pending commands (commands for which no event is received yet)
language: userLanguage || DEFAULT_LANGUAGE,
translator: (key) => translator(key, userLanguage || DEFAULT_LANGUAGE)
translator: t,
setLanguage
};

export default INITIAL_STATE;
70 changes: 44 additions & 26 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"dependencies": {},
"devDependencies": {
"node-polyglot": "^2.4.0",
"@babel/core": "^7.10.1",
"@babel/preset-env": "^7.10.1",
"@babel/preset-react": "7.10.1",
Expand Down
50 changes: 50 additions & 0 deletions client/test/unit/translatorTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import translatorFactory from '../../app/services/translator';

import translationsEN from '../../app/assets/i18n/en.json';
import translationsDE from '../../app/assets/i18n/de.json';

const dictionary = {
en: translationsEN,
de: translationsDE
};

test('simple', () => {
const {t, setLanguage} = translatorFactory(dictionary, 'de');

let translated = t('username');
expect(translated).toBe('Benutzername');

setLanguage('en');

translated = t('username');
expect(translated).toBe('Username');
});

test('unknown key', () => {
const {t} = translatorFactory(dictionary, 'de');

let translated = t('notKnown');
expect(translated).toBe('!!!notKnown!!!');
});

test('interpolation', () => {
const {t, setLanguage} = translatorFactory(
{
de: {
joinRoomname: '%{roomName} beitreten'
},
en: {
joinRoomname: 'Join %{roomName}'
}
},
'de'
);

let translated = t('joinRoomname', {roomName: 'Custom-Room'});
expect(translated).toBe('Custom-Room beitreten');

setLanguage('en');

translated = t('joinRoomname', {roomName: 'Custom-Room'});
expect(translated).toBe('Join Custom-Room');
});

0 comments on commit 085980e

Please sign in to comment.