Files
mozo-backend/app/assets/javascripts/translations.js.coffee.erb
T
2014-03-30 15:55:22 +02:00

82 lines
2.8 KiB
Plaintext

@$translations =
en:
models: <%= I18n.t('activemodel.models', locale: :en).to_json %>
attributes: <%= I18n.t('activemodel.attributes', locale: :en).to_json %>
helpers: <%= I18n.t('helpers', locale: :en).to_json %>
pagination: <%= I18n.t('views.pagination', locale: :en).to_json %>
nl:
models: <%= I18n.t('activemodel.models', locale: :nl).to_json %>
attributes: <%= I18n.t('activemodel.attributes', locale: :nl).to_json %>
helpers: <%= I18n.t('helpers', locale: :nl).to_json %>
pagination: <%= I18n.t('views.pagination', locale: :nl).to_json %>
@t = (path, vars={}) ->
#result = undefined
#m = undefined
#translatable = undefined
#isafety = undefined
#replacable = undefined
parts = path.split(".")
#accessor = "$translations.#{$locale}[\"#{parts.join("\"][\"")}\"]"
result = $translations[$locale]
try
result = result[part] for part in parts
catch err
result = parts[parts.length - 1].capitalize()
return "" if result is ""
return parts[parts.length - 1].capitalize() unless result
# allow t('test', {var: 'Benjamin'}) statements
# with $translations.nl.test = "Hello %{var}"
for variable, value of vars
result = result.replace("%{#{variable}}", value)
isafety = 0
while result.indexOf("${") > -1
m = result.match(/\${([\w\.]+(\|\w+)?)}/)
if m[2]
translatable = m[1].replace(m[2], "")
operation = $transformation_mappings[m[2].substr(1) or m[2].substr(1)]
else
translatable = m[1]
operation = null
replacable = t(translatable)
replacable = replacable[operation]() if operation
result = result.replace(m[0], replacable)
break if isafety > 10 # referencing other translations may cause infinite loops
isafety += 1
result
@setLocale = (locale, options={}) ->
locale ||= Qstorage.getItem('locale') || options.default || 'en'
Qstorage.setItem "locale", locale
@$locale = locale
setTranslations()
@setTranslations = (selector) ->
#list = $("#top-navigation-list")
selector = $( selector || document)
selector.find(".locale-select").show()
selector.find(".locale-select-" + $locale).hide()
moment.lang $locale
if selector
selector.find("[data-t]").each ->
$(this).html t($(this).data("t"), $(this).data("tAttributes"))
selector.find("*[data-time]").each ->
$(this).text moment($(this).data("time")).format($(this).data("timeFormat") or "dd D MMM HH:MM")
else
$("[data-t]").each ->
$(this).html t($(this).data("t"), $(this).data("tAttributes"))
$("*[data-time]").each ->
$(this).text moment($(this).data("time")).format($(this).data("timeFormat") or "dd D MMM HH:MM")
$(".datepicker").datepicker "option", $.datepicker.regional[$locale]
$transformation_mappings =
downcase: "toLowerCase"
upcase: "toUpperCase"