annotate .svn/pristine/da/daec043929c9800ec1c86a096d250b8584050bd6.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

Merge from branch "live"
author Chris Cannam
date Tue, 09 Sep 2014 09:34:53 +0100
parents cbb26bc654de
children
rev   line source
Chris@909 1 module Redmine
Chris@909 2 module I18n
Chris@909 3 def self.included(base)
Chris@909 4 base.extend Redmine::I18n
Chris@909 5 end
Chris@909 6
Chris@909 7 def l(*args)
Chris@909 8 case args.size
Chris@909 9 when 1
Chris@909 10 ::I18n.t(*args)
Chris@909 11 when 2
Chris@909 12 if args.last.is_a?(Hash)
Chris@909 13 ::I18n.t(*args)
Chris@909 14 elsif args.last.is_a?(String)
Chris@909 15 ::I18n.t(args.first, :value => args.last)
Chris@909 16 else
Chris@909 17 ::I18n.t(args.first, :count => args.last)
Chris@909 18 end
Chris@909 19 else
Chris@909 20 raise "Translation string with multiple values: #{args.first}"
Chris@909 21 end
Chris@909 22 end
Chris@909 23
Chris@909 24 def l_or_humanize(s, options={})
Chris@909 25 k = "#{options[:prefix]}#{s}".to_sym
Chris@909 26 ::I18n.t(k, :default => s.to_s.humanize)
Chris@909 27 end
Chris@909 28
Chris@909 29 def l_hours(hours)
Chris@909 30 hours = hours.to_f
Chris@909 31 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
Chris@909 32 end
Chris@909 33
Chris@909 34 def ll(lang, str, value=nil)
Chris@909 35 ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
Chris@909 36 end
Chris@909 37
Chris@909 38 def format_date(date)
Chris@909 39 return nil unless date
Chris@909 40 Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
Chris@909 41 end
Chris@909 42
Chris@909 43 def format_time(time, include_date = true)
Chris@909 44 return nil unless time
Chris@909 45 time = time.to_time if time.is_a?(String)
Chris@909 46 zone = User.current.time_zone
Chris@909 47 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
Chris@909 48 (include_date ? "#{format_date(local)} " : "") +
Chris@909 49 (Setting.time_format.blank? ? ::I18n.l(local, :format => :time) : local.strftime(Setting.time_format))
Chris@909 50 end
Chris@909 51
Chris@909 52 def day_name(day)
Chris@909 53 ::I18n.t('date.day_names')[day % 7]
Chris@909 54 end
Chris@909 55
Chris@909 56 def month_name(month)
Chris@909 57 ::I18n.t('date.month_names')[month]
Chris@909 58 end
Chris@909 59
Chris@909 60 def valid_languages
Chris@909 61 @@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym)
Chris@909 62 end
Chris@909 63
Chris@909 64 def find_language(lang)
Chris@909 65 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
Chris@909 66 @@languages_lookup[lang.to_s.downcase]
Chris@909 67 end
Chris@909 68
Chris@909 69 def set_language_if_valid(lang)
Chris@909 70 if l = find_language(lang)
Chris@909 71 ::I18n.locale = l
Chris@909 72 end
Chris@909 73 end
Chris@909 74
Chris@909 75 def current_language
Chris@909 76 ::I18n.locale
Chris@909 77 end
Chris@909 78 end
Chris@909 79 end