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