Module: Redmine::I18n

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
# File 'lib/redmine/i18n.rb', line 17

def self.included(base)
  base.extend Redmine::I18n
end

Instance Method Details

#current_languageObject



89
90
91
# File 'lib/redmine/i18n.rb', line 89

def current_language
  ::I18n.locale
end

#day_name(day) ⇒ Object



66
67
68
# File 'lib/redmine/i18n.rb', line 66

def day_name(day)
  ::I18n.t('date.day_names')[day % 7]
end

#find_language(lang) ⇒ Object



78
79
80
81
# File 'lib/redmine/i18n.rb', line 78

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

#format_date(date) ⇒ Object



52
53
54
55
# File 'lib/redmine/i18n.rb', line 52

def format_date(date)
  return nil unless date
  Setting.date_format.blank? ? ::I18n.l(date.to_date) : date.strftime(Setting.date_format)
end

#format_time(time, include_date = true) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/redmine/i18n.rb', line 57

def format_time(time, include_date = true)
  return nil unless time
  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)
  (include_date ? "#{format_date(local)} " : "") +
    (Setting.time_format.blank? ? ::I18n.l(local, :format => :time) : local.strftime(Setting.time_format))
end

#l(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/redmine/i18n.rb', line 21

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

#l_hours(hours) ⇒ Object



43
44
45
46
# File 'lib/redmine/i18n.rb', line 43

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

#l_or_humanize(s, options = {}) ⇒ Object



38
39
40
41
# File 'lib/redmine/i18n.rb', line 38

def l_or_humanize(s, options={})
  k = "#{options[:prefix]}#{s}".to_sym
  ::I18n.t(k, :default => s.to_s.humanize)
end

#ll(lang, str, value = nil) ⇒ Object



48
49
50
# File 'lib/redmine/i18n.rb', line 48

def ll(lang, str, value=nil)
  ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
end

#month_name(month) ⇒ Object



70
71
72
# File 'lib/redmine/i18n.rb', line 70

def month_name(month)
  ::I18n.t('date.month_names')[month]
end

#set_language_if_valid(lang) ⇒ Object



83
84
85
86
87
# File 'lib/redmine/i18n.rb', line 83

def set_language_if_valid(lang)
  if l = find_language(lang)
    ::I18n.locale = l
  end
end

#valid_languagesObject



74
75
76
# File 'lib/redmine/i18n.rb', line 74

def valid_languages
  @@valid_languages ||= Dir.glob(File.join(RAILS_ROOT, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym)
end