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