Files
mozo-backend/app/assets/javascripts/translations.js.coffee.erb
T
2014-04-18 20:38:13 +02:00

118 lines
4.5 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")
locale = Qstorage.getItem('locale') || 'en'
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")
# jQuery UI datepicker support
$(".datepicker").datepicker "option", $.datepicker.regional[locale] if $.fn.datepicker
# pickadate support
if $.fn.pickadate
if selector.hasClass('datepicker')
datepicker_object = selector
selector.siblings('.pickadate-display').remove()
else
datepicker_object = selector.find('.datepicker')
selector.find('.pickadate-display').remove()
datepicker_object.pickadate('stop') if datepicker_object.data('pickadate')
$.extend( $.fn.pickadate.defaults, $pickadate_translations[locale] )
window.pickadate_options ||= {}
datepicker_object.pickadate(window.pickadate_options)
datepicker_object.change()
@setupTranslations = (options = {})->
locale = options.locale || Qstorage.getItem('locale') || 'en'
if $.fn.pickadate
$(document).on 'change', '.datepicker', ->
input = $(@)
input.next().remove() if input.next().hasClass('pickadate-display')
if input.data('pickadate')
if input.val()
display_format = $pickadate_translations[$locale].displayFormat
display_date = input.data('pickadate').get('select', display_format)
display_date = $('<span></span>').text(' '+display_date) # add space between the icon and the date
else
display_date = $('<span></span>').data('t', 'datepicker.no_date').text(t('datepicker.no_date'))
display_tag = $('<span></span>').addClass('pickadate-display').append('<span class="fa fa-calendar fa-lg"></span>').append(display_date)
#display_tag.click (e)->(e.preventDefault();input.click().focus();false )
display_tag.click (e)->(e.preventDefault();input.pickadate('open');false )
$(@).after(display_tag)
setLocale(locale)
$('.datepicker').change().hide()
$transformation_mappings =
downcase: "toLowerCase"
upcase: "toUpperCase"