@$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 %>
errors: <%= I18n.t('errors', locale: :en).to_json %>
date: <%= {day_name: Hash[Date::DAYNAMES.map(&:downcase).zip(I18n.t('date.day_names', 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 %>
errors: <%= I18n.t('errors', locale: :nl).to_json %>
date: <%= {day_name: Hash[Date::DAYNAMES.map(&:downcase).zip(I18n.t('date.day_names', locale: :nl))]}.to_json %>
@day_minutes_to_time = (minutes)->
return "" unless minutes
[("0" + Math.floor(minutes/60)).substr(-2,2), ("0" + Math.floor(minutes%60)).substr(-2,2)].join(":")
@ttry = (path, vars={})->
@t(path, $.extend(vars, emptyWhenNotFound: true))
# return translation in the form
# Tafel
@tspan = (path, vars={}) -> "#{t(path, vars)}"
@t = (path, vars={}) ->
#result = undefined
#m = undefined
#translatable = undefined
#isafety = undefined
#replacable = undefined
locale = Qstorage.getItem('locale') || 'en'
parts = path.split(".")
#accessor = "$translations.#{$locale}[\"#{parts.join("\"][\"")}\"]"
result = $translations[locale]
try
result = result[part] for part in parts
catch err
console.log "[TRANSLATION] Cannot find translation: #{path}"
result = parts[parts.length - 1].capitalize()
result = if vars.emptyWhenNotFound then "" else parts[parts.length - 1].capitalize()
unless result?
console.log "[TRANSLATION] Cannot find translation: #{path}"
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
try
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
catch err
console.log "translation #{result} cannot be interpolated"
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.locale 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()
# use like
# error_message
# attribute: t('attributes.section.title')
# message: 'blank'
@error_message = (options = {})->
locale = options.locale || Qstorage.getItem('locale') || 'en'
#message = $translations[locale].errors.messages[options.message]
message = t "errors.messages.#{options.message}", options
t 'errors.format',
attribute: options.attribute
message: message
@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 = $('').text(' '+display_date) # add space between the icon and the date
else
display_date = $('').data('t', 'datepicker.no_date').text(t('datepicker.no_date'))
display_tag = $('').addClass('pickadate-display').append('').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"