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