-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#58 use custom dictionary for testing interpolation
- Loading branch information
Showing
7 changed files
with
123 additions
and
58 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 |
---|---|---|
@@ -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,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'); | ||
}); |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"node": true | ||
}, | ||
"globals": { | ||
"jest": false, | ||
"test": false, | ||
"expect": false, | ||
"beforeEach": false, | ||
|