annotate lib/redmine/i18n.rb @ 1452:d6b9fd02bb89 feature_36_js_refactoring

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