@@ -1,16 +1,15 | |||
|
1 | 1 | I18n.default_locale = 'en' |
|
2 | # Adds fallback to default locale for untranslated strings | |
|
3 | I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks) | |
|
2 | I18n.backend = Redmine::I18n::Backend.new | |
|
4 | 3 | |
|
5 | 4 | require 'redmine' |
|
6 | 5 | |
|
7 | 6 | # Load the secret token from the Redmine configuration file |
|
8 | 7 | secret = Redmine::Configuration['secret_token'] |
|
9 | 8 | if secret.present? |
|
10 | 9 | RedmineApp::Application.config.secret_token = secret |
|
11 | 10 | end |
|
12 | 11 | |
|
13 | 12 | Redmine::Plugin.load |
|
14 | 13 | unless Redmine::Configuration['mirror_plugins_assets_on_startup'] == false |
|
15 | 14 | Redmine::Plugin.mirror_assets |
|
16 | 15 | end |
@@ -1,88 +1,157 | |||
|
1 | 1 | module Redmine |
|
2 | 2 | module I18n |
|
3 | 3 | def self.included(base) |
|
4 | 4 | base.extend Redmine::I18n |
|
5 | 5 | end |
|
6 | 6 | |
|
7 | 7 | def l(*args) |
|
8 | 8 | case args.size |
|
9 | 9 | when 1 |
|
10 | 10 | ::I18n.t(*args) |
|
11 | 11 | when 2 |
|
12 | 12 | if args.last.is_a?(Hash) |
|
13 | 13 | ::I18n.t(*args) |
|
14 | 14 | elsif args.last.is_a?(String) |
|
15 | 15 | ::I18n.t(args.first, :value => args.last) |
|
16 | 16 | else |
|
17 | 17 | ::I18n.t(args.first, :count => args.last) |
|
18 | 18 | end |
|
19 | 19 | else |
|
20 | 20 | raise "Translation string with multiple values: #{args.first}" |
|
21 | 21 | end |
|
22 | 22 | end |
|
23 | 23 | |
|
24 | 24 | def l_or_humanize(s, options={}) |
|
25 | 25 | k = "#{options[:prefix]}#{s}".to_sym |
|
26 | 26 | ::I18n.t(k, :default => s.to_s.humanize) |
|
27 | 27 | end |
|
28 | 28 | |
|
29 | 29 | def l_hours(hours) |
|
30 | 30 | hours = hours.to_f |
|
31 | 31 | l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f)) |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def ll(lang, str, value=nil) |
|
35 | 35 | ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }) |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def format_date(date) |
|
39 | 39 | return nil unless date |
|
40 | 40 | options = {} |
|
41 | 41 | options[:format] = Setting.date_format unless Setting.date_format.blank? |
|
42 | 42 | options[:locale] = User.current.language unless User.current.language.blank? |
|
43 | 43 | ::I18n.l(date.to_date, options) |
|
44 | 44 | end |
|
45 | 45 | |
|
46 | 46 | def format_time(time, include_date = true) |
|
47 | 47 | return nil unless time |
|
48 | 48 | options = {} |
|
49 | 49 | options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format) |
|
50 | 50 | options[:locale] = User.current.language unless User.current.language.blank? |
|
51 | 51 | time = time.to_time if time.is_a?(String) |
|
52 | 52 | zone = User.current.time_zone |
|
53 | 53 | local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) |
|
54 | 54 | (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options) |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def day_name(day) |
|
58 | 58 | ::I18n.t('date.day_names')[day % 7] |
|
59 | 59 | end |
|
60 | 60 | |
|
61 | 61 | def day_letter(day) |
|
62 | 62 | ::I18n.t('date.abbr_day_names')[day % 7].first |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def month_name(month) |
|
66 | 66 | ::I18n.t('date.month_names')[month] |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def valid_languages |
|
70 | @@valid_languages ||= Dir.glob(File.join(Rails.root, 'config', 'locales', '*.yml')).collect {|f| File.basename(f).split('.').first}.collect(&:to_sym) | |
|
70 | ::I18n.available_locales | |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def find_language(lang) |
|
74 | 74 | @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } |
|
75 | 75 | @@languages_lookup[lang.to_s.downcase] |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | def set_language_if_valid(lang) |
|
79 | 79 | if l = find_language(lang) |
|
80 | 80 | ::I18n.locale = l |
|
81 | 81 | end |
|
82 | 82 | end |
|
83 | 83 | |
|
84 | 84 | def current_language |
|
85 | 85 | ::I18n.locale |
|
86 | 86 | end |
|
87 | ||
|
88 | # Custom backend based on I18n::Backend::Simple with the following changes: | |
|
89 | # * lazy loading of translation files | |
|
90 | # * available_locales are determined by looking at translation file names | |
|
91 | class Backend | |
|
92 | (class << self; self; end).class_eval { public :include } | |
|
93 | ||
|
94 | module Implementation | |
|
95 | include ::I18n::Backend::Base | |
|
96 | ||
|
97 | # Stores translations for the given locale in memory. | |
|
98 | # This uses a deep merge for the translations hash, so existing | |
|
99 | # translations will be overwritten by new ones only at the deepest | |
|
100 | # level of the hash. | |
|
101 | def store_translations(locale, data, options = {}) | |
|
102 | locale = locale.to_sym | |
|
103 | translations[locale] ||= {} | |
|
104 | data = data.deep_symbolize_keys | |
|
105 | translations[locale].deep_merge!(data) | |
|
106 | end | |
|
107 | ||
|
108 | # Get available locales from the translations filenames | |
|
109 | def available_locales | |
|
110 | @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*').to_sym}.uniq.sort | |
|
111 | end | |
|
112 | ||
|
113 | # Clean up translations | |
|
114 | def reload! | |
|
115 | @translations = nil | |
|
116 | @available_locales = nil | |
|
117 | super | |
|
118 | end | |
|
119 | ||
|
120 | protected | |
|
121 | ||
|
122 | def init_translations(locale) | |
|
123 | locale = locale.to_s | |
|
124 | paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale} | |
|
125 | load_translations(paths) | |
|
126 | translations[locale] ||= {} | |
|
127 | end | |
|
128 | ||
|
129 | def translations | |
|
130 | @translations ||= {} | |
|
131 | end | |
|
132 | ||
|
133 | # Looks up a translation from the translations hash. Returns nil if | |
|
134 | # eiher key is nil, or locale, scope or key do not exist as a key in the | |
|
135 | # nested translations hash. Splits keys or scopes containing dots | |
|
136 | # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as | |
|
137 | # <tt>%w(currency format)</tt>. | |
|
138 | def lookup(locale, key, scope = [], options = {}) | |
|
139 | init_translations(locale) unless translations.key?(locale) | |
|
140 | keys = ::I18n.normalize_keys(locale, key, scope, options[:separator]) | |
|
141 | ||
|
142 | keys.inject(translations) do |result, _key| | |
|
143 | _key = _key.to_sym | |
|
144 | return nil unless result.is_a?(Hash) && result.has_key?(_key) | |
|
145 | result = result[_key] | |
|
146 | result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol) | |
|
147 | result | |
|
148 | end | |
|
149 | end | |
|
150 | end | |
|
151 | ||
|
152 | include Implementation | |
|
153 | # Adds fallback to default locale for untranslated strings | |
|
154 | include ::I18n::Backend::Fallbacks | |
|
155 | end | |
|
87 | 156 | end |
|
88 | 157 | end |
General Comments 0
You need to be logged in to leave comments.
Login now