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