Created i18n utility function

This commit is contained in:
Ruben Vereecken 2014-03-12 20:30:43 +01:00
parent da762fbc4f
commit 76110423c0
2 changed files with 23 additions and 1 deletions

View file

@ -44,4 +44,26 @@ module.exports.hslToHex = (hsl) ->
toHex = (n) ->
h = Math.floor(n).toString(16)
h = '0'+h if h.length is 1
h
h
i18n = (say, target, language=me.lang(), fallback='en') ->
generalResult = null
fallbackResult = null
fallforwardResult = null # If a general language isn't available, the first specific one will do
matches = (/\w+/gi).exec(language)
generalName = matches[0] if matches
for locale in say?.i18n
if target of say[locale]
result = say[locale][target]
else continue
return result if locale == language
generalResult = result if locale == generalName
fallbackResult = result if locale == fallback
fallforwardResult = result if locale.indexOf language != -1 and not fallforwardResult?
return generalResult if generalResult?
return fallbackResult if fallbackResult?
return fallforwardResult if fallforwardResult?
return say.text if 'text' of say
null

View file