annotate .svn/pristine/0a/0a01ef88ba34bfc6a3c1cef8891538f7504e9b0c.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1464 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 module Redmine
Chris@1464 19 module I18n
Chris@1464 20 def self.included(base)
Chris@1464 21 base.extend Redmine::I18n
Chris@1464 22 end
Chris@1464 23
Chris@1464 24 def l(*args)
Chris@1464 25 case args.size
Chris@1464 26 when 1
Chris@1464 27 ::I18n.t(*args)
Chris@1464 28 when 2
Chris@1464 29 if args.last.is_a?(Hash)
Chris@1464 30 ::I18n.t(*args)
Chris@1464 31 elsif args.last.is_a?(String)
Chris@1464 32 ::I18n.t(args.first, :value => args.last)
Chris@1464 33 else
Chris@1464 34 ::I18n.t(args.first, :count => args.last)
Chris@1464 35 end
Chris@1464 36 else
Chris@1464 37 raise "Translation string with multiple values: #{args.first}"
Chris@1464 38 end
Chris@1464 39 end
Chris@1464 40
Chris@1464 41 def l_or_humanize(s, options={})
Chris@1464 42 k = "#{options[:prefix]}#{s}".to_sym
Chris@1464 43 ::I18n.t(k, :default => s.to_s.humanize)
Chris@1464 44 end
Chris@1464 45
Chris@1464 46 def l_hours(hours)
Chris@1464 47 hours = hours.to_f
Chris@1464 48 l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f))
Chris@1464 49 end
Chris@1464 50
Chris@1464 51 def ll(lang, str, value=nil)
Chris@1464 52 ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" })
Chris@1464 53 end
Chris@1464 54
Chris@1464 55 def format_date(date)
Chris@1464 56 return nil unless date
Chris@1464 57 options = {}
Chris@1464 58 options[:format] = Setting.date_format unless Setting.date_format.blank?
Chris@1464 59 options[:locale] = User.current.language unless User.current.language.blank?
Chris@1464 60 ::I18n.l(date.to_date, options)
Chris@1464 61 end
Chris@1464 62
Chris@1464 63 def format_time(time, include_date = true)
Chris@1464 64 return nil unless time
Chris@1464 65 options = {}
Chris@1464 66 options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format)
Chris@1464 67 options[:locale] = User.current.language unless User.current.language.blank?
Chris@1464 68 time = time.to_time if time.is_a?(String)
Chris@1464 69 zone = User.current.time_zone
Chris@1464 70 local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time)
Chris@1464 71 (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options)
Chris@1464 72 end
Chris@1464 73
Chris@1464 74 def day_name(day)
Chris@1464 75 ::I18n.t('date.day_names')[day % 7]
Chris@1464 76 end
Chris@1464 77
Chris@1464 78 def day_letter(day)
Chris@1464 79 ::I18n.t('date.abbr_day_names')[day % 7].first
Chris@1464 80 end
Chris@1464 81
Chris@1464 82 def month_name(month)
Chris@1464 83 ::I18n.t('date.month_names')[month]
Chris@1464 84 end
Chris@1464 85
Chris@1464 86 def valid_languages
Chris@1464 87 ::I18n.available_locales
Chris@1464 88 end
Chris@1464 89
Chris@1464 90 # Returns an array of languages names and code sorted by names, example:
Chris@1464 91 # [["Deutsch", "de"], ["English", "en"] ...]
Chris@1464 92 #
Chris@1464 93 # The result is cached to prevent from loading all translations files.
Chris@1464 94 def languages_options
Chris@1464 95 ActionController::Base.cache_store.fetch "i18n/languages_options" do
Chris@1464 96 valid_languages.map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.sort {|x,y| x.first <=> y.first }
Chris@1464 97 end
Chris@1464 98 end
Chris@1464 99
Chris@1464 100 def find_language(lang)
Chris@1464 101 @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k }
Chris@1464 102 @@languages_lookup[lang.to_s.downcase]
Chris@1464 103 end
Chris@1464 104
Chris@1464 105 def set_language_if_valid(lang)
Chris@1464 106 if l = find_language(lang)
Chris@1464 107 ::I18n.locale = l
Chris@1464 108 end
Chris@1464 109 end
Chris@1464 110
Chris@1464 111 def current_language
Chris@1464 112 ::I18n.locale
Chris@1464 113 end
Chris@1464 114
Chris@1464 115 # Custom backend based on I18n::Backend::Simple with the following changes:
Chris@1464 116 # * lazy loading of translation files
Chris@1464 117 # * available_locales are determined by looking at translation file names
Chris@1464 118 class Backend
Chris@1464 119 (class << self; self; end).class_eval { public :include }
Chris@1464 120
Chris@1464 121 module Implementation
Chris@1464 122 include ::I18n::Backend::Base
Chris@1464 123
Chris@1464 124 # Stores translations for the given locale in memory.
Chris@1464 125 # This uses a deep merge for the translations hash, so existing
Chris@1464 126 # translations will be overwritten by new ones only at the deepest
Chris@1464 127 # level of the hash.
Chris@1464 128 def store_translations(locale, data, options = {})
Chris@1464 129 locale = locale.to_sym
Chris@1464 130 translations[locale] ||= {}
Chris@1464 131 data = data.deep_symbolize_keys
Chris@1464 132 translations[locale].deep_merge!(data)
Chris@1464 133 end
Chris@1464 134
Chris@1464 135 # Get available locales from the translations filenames
Chris@1464 136 def available_locales
Chris@1464 137 @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym)
Chris@1464 138 end
Chris@1464 139
Chris@1464 140 # Clean up translations
Chris@1464 141 def reload!
Chris@1464 142 @translations = nil
Chris@1464 143 @available_locales = nil
Chris@1464 144 super
Chris@1464 145 end
Chris@1464 146
Chris@1464 147 protected
Chris@1464 148
Chris@1464 149 def init_translations(locale)
Chris@1464 150 locale = locale.to_s
Chris@1464 151 paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale}
Chris@1464 152 load_translations(paths)
Chris@1464 153 translations[locale] ||= {}
Chris@1464 154 end
Chris@1464 155
Chris@1464 156 def translations
Chris@1464 157 @translations ||= {}
Chris@1464 158 end
Chris@1464 159
Chris@1464 160 # Looks up a translation from the translations hash. Returns nil if
Chris@1464 161 # eiher key is nil, or locale, scope or key do not exist as a key in the
Chris@1464 162 # nested translations hash. Splits keys or scopes containing dots
Chris@1464 163 # into multiple keys, i.e. <tt>currency.format</tt> is regarded the same as
Chris@1464 164 # <tt>%w(currency format)</tt>.
Chris@1464 165 def lookup(locale, key, scope = [], options = {})
Chris@1464 166 init_translations(locale) unless translations.key?(locale)
Chris@1464 167 keys = ::I18n.normalize_keys(locale, key, scope, options[:separator])
Chris@1464 168
Chris@1464 169 keys.inject(translations) do |result, _key|
Chris@1464 170 _key = _key.to_sym
Chris@1464 171 return nil unless result.is_a?(Hash) && result.has_key?(_key)
Chris@1464 172 result = result[_key]
Chris@1464 173 result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol)
Chris@1464 174 result
Chris@1464 175 end
Chris@1464 176 end
Chris@1464 177 end
Chris@1464 178
Chris@1464 179 include Implementation
Chris@1464 180 # Adds fallback to default locale for untranslated strings
Chris@1464 181 include ::I18n::Backend::Fallbacks
Chris@1464 182 end
Chris@1464 183 end
Chris@1464 184 end