2009-02-21 14:04:50 +03:00
|
|
|
module Redmine
|
|
|
|
module I18n
|
|
|
|
def self.included(base)
|
|
|
|
base.extend Redmine::I18n
|
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def l(*args)
|
|
|
|
case args.size
|
|
|
|
when 1
|
|
|
|
::I18n.t(*args)
|
|
|
|
when 2
|
|
|
|
if args.last.is_a?(Hash)
|
|
|
|
::I18n.t(*args)
|
|
|
|
elsif args.last.is_a?(String)
|
|
|
|
::I18n.t(args.first, :value => args.last)
|
|
|
|
else
|
|
|
|
::I18n.t(args.first, :count => args.last)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "Translation string with multiple values: #{args.first}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def l_or_humanize(s, options={})
|
|
|
|
k = "#{options[:prefix]}#{s}".to_sym
|
|
|
|
::I18n.t(k, :default => s.to_s.humanize)
|
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def l_hours(hours)
|
|
|
|
hours = hours.to_f
|
|
|
|
l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
|
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def ll(lang, str, value=nil)
|
|
|
|
::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_date(date)
|
|
|
|
return nil unless date
|
2012-06-17 01:45:47 +04:00
|
|
|
options = {}
|
|
|
|
options[:format] = Setting.date_format unless Setting.date_format.blank?
|
|
|
|
options[:locale] = User.current.language unless User.current.language.blank?
|
|
|
|
::I18n.l(date.to_date, options)
|
2009-02-21 14:04:50 +03:00
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def format_time(time, include_date = true)
|
|
|
|
return nil unless time
|
2012-06-17 01:45:47 +04:00
|
|
|
options = {}
|
|
|
|
options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
|
|
|
|
options[:locale] = User.current.language unless User.current.language.blank?
|
2009-02-21 14:04:50 +03:00
|
|
|
time = time.to_time if time.is_a?(String)
|
|
|
|
zone = User.current.time_zone
|
|
|
|
local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
|
2012-06-17 01:45:47 +04:00
|
|
|
(include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
|
2009-02-21 14:04:50 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def day_name(day)
|
|
|
|
::I18n.t('date.day_names')[day % 7]
|
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def month_name(month)
|
|
|
|
::I18n.t('date.month_names')[month]
|
|
|
|
end
|
2011-06-17 04:56:48 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def valid_languages
|
2011-06-17 04:56:48 +04:00
|
|
|
@@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym)
|
2009-02-21 14:04:50 +03:00
|
|
|
end
|
2011-06-17 04:56:48 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def find_language(lang)
|
|
|
|
@@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
|
|
|
|
@@languages_lookup[lang.to_s.downcase]
|
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def set_language_if_valid(lang)
|
|
|
|
if l = find_language(lang)
|
|
|
|
::I18n.locale = l
|
|
|
|
end
|
|
|
|
end
|
2011-09-01 04:45:09 +04:00
|
|
|
|
2009-02-21 14:04:50 +03:00
|
|
|
def current_language
|
|
|
|
::I18n.locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|