Finished perfecting i18n

This commit is contained in:
Ruben Vereecken 2014-03-13 00:47:08 +01:00
parent 280b986898
commit 3856ac597f
2 changed files with 16 additions and 17 deletions

View file

@ -53,17 +53,17 @@ module.exports.i18n = (say, target, language=me.lang(), fallback='en') ->
matches = (/\w+/gi).exec(language) matches = (/\w+/gi).exec(language)
generalName = matches[0] if matches generalName = matches[0] if matches
for locale in say?.i18n for localeName, locale of say.i18n
if target of say[locale] if target of locale
result = say[locale][target] result = locale[target]
else continue else continue
return result if locale == language return result if localeName == language
generalResult = result if locale == generalName generalResult = result if localeName == generalName
fallbackResult = result if locale == fallback fallbackResult = result if localeName == fallback
fallforwardResult = result if locale.indexOf language != -1 and not fallforwardResult? fallforwardResult = result if localeName.indexOf(language) == 0 and not fallforwardResult?
return generalResult if generalResult? return generalResult if generalResult?
return fallbackResult if fallbackResult?
return fallforwardResult if fallforwardResult? return fallforwardResult if fallforwardResult?
return fallbackResult if fallbackResult?
return say.text if 'text' of say return say.text if 'text' of say
null null

View file

@ -15,18 +15,17 @@ describe 'utils library', ->
"text": "S'lut, Magicien! Venu pratiquer? Ok, bien débutons..." "text": "S'lut, Magicien! Venu pratiquer? Ok, bien débutons..."
"pt-BR": "pt-BR":
"text": "Bom dia, feiticeiro! Veio praticar? Então vamos começar..." "text": "Bom dia, feiticeiro! Veio praticar? Então vamos começar..."
"de":
"text": "'N Tach auch, Zauberer! Kommst Du zum Üben? Dann lass uns anfangen..."
"tr":
"text": "İyi günler, Büyücü! Antremana mı geldin? Güzel, hadi başlayalım..."
"sv":
"text": "Godagens, trollkarl! Kommit för att öva? Nå, låt oss börja..."
"en": "en":
"text": "Ohai Magician!" "text": "Ohai Magician!"
"de":
"text": "'N Tach auch, Zauberer! Kommst Du zum Üben? Dann lass uns anfangen..."
"sv":
"text": "Godagens, trollkarl! Kommit för att öva? Nå, låt oss börja..."
it 'i18n should find a valid target string', -> it 'i18n should find a valid target string', ->
expect(util.i18n(this.fixture1, 'text', 'sv')).toEqual(this.fixture1.i18n['sv'].text) expect(util.i18n(this.fixture1, 'text', 'sv')).toEqual(this.fixture1.i18n['sv'].text)
expect(util.i18n(this.fixture1, 'test', 'es-ES')).toEqual(this.fixture1.i18n['es-ES'].text) expect(util.i18n(this.fixture1, 'text', 'es-ES')).toEqual(this.fixture1.i18n['es-ES'].text)
it 'i18n picks the correct fallback for a specific language', -> it 'i18n picks the correct fallback for a specific language', ->
expect(util.i18n(this.fixture1, 'text', 'fr-be')).toEqual(this.fixture1.i18n['fr'].text) expect(util.i18n(this.fixture1, 'text', 'fr-be')).toEqual(this.fixture1.i18n['fr'].text)
@ -35,7 +34,7 @@ describe 'utils library', ->
expect(util.i18n(this.fixture1, 'text', 'nl')).toEqual(this.fixture1.i18n['en'].text) expect(util.i18n(this.fixture1, 'text', 'nl')).toEqual(this.fixture1.i18n['en'].text)
expect(util.i18n(this.fixture1, 'text', 'nl', 'de')).toEqual(this.fixture1.i18n['de'].text) expect(util.i18n(this.fixture1, 'text', 'nl', 'de')).toEqual(this.fixture1.i18n['de'].text)
it 'i18n falls back to the default text, even for other targets (like burb)', -> it 'i18n falls back to the default text, even for other targets (like blurb)', ->
delete this.fixture1.i18n['en'] delete this.fixture1.i18n['en']
expect(util.i18n(this.fixture1, 'text', 'en')).toEqual(this.fixture1.text) expect(util.i18n(this.fixture1, 'text', 'en')).toEqual(this.fixture1.text)
expect(util.i18n(this.fixture1, 'blurb', 'en')).toEqual(this.fixture1.text) expect(util.i18n(this.fixture1, 'blurb', 'en')).toEqual(this.fixture1.text)