Refactor I18n class

i18n
Jed Fox 7 years ago
parent d9a11c28c7
commit 75b3ab90ab

@ -1,17 +1,30 @@
class @I18n class @I18n
constructor: (@data, lang = 'en') -> default: 'en'
@setLanguage lang constructor: (@langs = [@default]) ->
setLanguage: (lang) -> _: (obj, substitution) =>
return false unless @data[lang] if typeof obj is 'object' or typeof obj is 'function'
@lang = lang @interpolate obj[@get_lang obj], substitution
true else
obj
_: (key, substitution) -> get_lang: (obj) ->
str = @data[@lang][key] for lang in @langs
return unless str? # eg. en-US
return lang if obj[lang]
lang = lang.replace /_\w+$/, ''
# eg. en
return lang if obj[lang]
@default
interpolate: (str, substitution) ->
return '' if not str?
if substitution? if substitution?
for key in Object.keys(substitution) for key in Object.keys(substitution)
str = str.replace("{#{key}}", substitution[key]) str = str.replace("{#{key}}", substitution[key])
str str
@i18n = new I18n navigator.languages
@_ = @i18n._

Loading…
Cancel
Save