-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2d61d2
commit 761c03a
Showing
8 changed files
with
112 additions
and
57 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
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 |
---|---|---|
@@ -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; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,40 @@ | ||
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(dictionary, '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'); | ||
}); |