Bike!Bike! Website!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

86 lines
2.6 KiB

module I18nHelper
def url_for_locale(locale, url = nil)
return url unless locale.present?
url ||= current_path(true)
return url if Rails.env.development? || Rails.env.test?
return "https://preview-#{locale.to_s}." + ENV['DEFAULT_URL'] + "#{url}" if Rails.env.preview?
"https://#{locale.to_s}." + ENV['DEFAULT_URL'] + "#{url}"
end
def current_path(relative = false)
new_params = params.merge({action: (params[:_original_action] || params[:action])})
new_params.delete(:_original_action)
if Rails.env.development? || Rails.env.test?
return url_for(new_params.merge({lang: locale.to_s}))
end
# return now if this is a relative path, don't add the host
return url_for(new_params) if relative
subdomain = Rails.env.preview? ? "preview-#{locale.to_s}" : locale.to_s
url_for(new_params.merge(host: "#{subdomain}." + ENV['DEFAULT_URL']))
end
def canonical_url
url = current_path
return url if Rails.env.development? || Rails.env.test?
return ENV['DEFAULT_URL'] + "#{url}" if Rails.env.preview?
"https://bikebike.org#{url}"
end
def date(date, format = :long)
return '' unless date.present?
I18n.l(date.is_a?(String) ? Date.parse(date) : date, format: format)
end
def time(time, format = :short)
if time.is_a?(String)
time = Date.parse(time)
elsif time.is_a?(Float) || time.is_a?(Integer)
time = DateTime.now.midnight + time.hours
end
I18n.l(time, format: format)
end
def date_span(date1, date2)
key = 'same_month'
if date1.year != date2.year
key = 'different_year'
elsif date1.month != date2.month
key = 'same_year'
end
d1 = I18n.l(date1.to_date, format: "span_#{key}_date_1".to_sym)
d2 = I18n.l(date2.to_date, format: "span_#{key}_date_2".to_sym)
_('date.date_span', vars: { date_1: d1, date_2: d2 })
end
def time_length(length)
hours = length.to_i
minutes = ((length - hours) * 60).to_i
hours = hours > 0 ? (I18n.t 'datetime.distance_in_words.x_hours', count: hours) : nil
minutes = minutes > 0 ? (I18n.t 'datetime.distance_in_words.x_minutes', count: minutes) : nil
return hours.present? ? (minutes.present? ? (I18n.t 'datetime.distance_in_words.x_and_y', x: hours, y: minutes) : hours) : minutes
end
def hour_span(time1, time2)
(time2 - time1) / 3600
end
def hours(time1, time2)
time_length hour_span(time1, time2)
end
def money(amount)
return _!('$0.00') if amount == 0
_!((amount * 100).to_i.to_s.gsub(/^(.*)(\d\d)$/, '$\1.\2'))
end
def percent(p)
return _!('0.00%') if p == 0
_!((p * 10000).to_i.to_s.gsub(/^(.*)(\d\d)$/, '\1.\2%'))
end
end