Chris@1295: # Redmine - project management software Chris@1295: # Copyright (C) 2006-2013 Jean-Philippe Lang Chris@1295: # Chris@1295: # This program is free software; you can redistribute it and/or Chris@1295: # modify it under the terms of the GNU General Public License Chris@1295: # as published by the Free Software Foundation; either version 2 Chris@1295: # of the License, or (at your option) any later version. Chris@1295: # Chris@1295: # This program is distributed in the hope that it will be useful, Chris@1295: # but WITHOUT ANY WARRANTY; without even the implied warranty of Chris@1295: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Chris@1295: # GNU General Public License for more details. Chris@1295: # Chris@1295: # You should have received a copy of the GNU General Public License Chris@1295: # along with this program; if not, write to the Free Software Chris@1295: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Chris@1295: Chris@1295: module Redmine Chris@1295: module I18n Chris@1295: def self.included(base) Chris@1295: base.extend Redmine::I18n Chris@1295: end Chris@1295: Chris@1295: def l(*args) Chris@1295: case args.size Chris@1295: when 1 Chris@1295: ::I18n.t(*args) Chris@1295: when 2 Chris@1295: if args.last.is_a?(Hash) Chris@1295: ::I18n.t(*args) Chris@1295: elsif args.last.is_a?(String) Chris@1295: ::I18n.t(args.first, :value => args.last) Chris@1295: else Chris@1295: ::I18n.t(args.first, :count => args.last) Chris@1295: end Chris@1295: else Chris@1295: raise "Translation string with multiple values: #{args.first}" Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def l_or_humanize(s, options={}) Chris@1295: k = "#{options[:prefix]}#{s}".to_sym Chris@1295: ::I18n.t(k, :default => s.to_s.humanize) Chris@1295: end Chris@1295: Chris@1295: def l_hours(hours) Chris@1295: hours = hours.to_f Chris@1295: l((hours < 2.0 ? :label_f_hour : :label_f_hour_plural), :value => ("%.2f" % hours.to_f)) Chris@1295: end Chris@1295: Chris@1295: def ll(lang, str, value=nil) Chris@1295: ::I18n.t(str.to_s, :value => value, :locale => lang.to_s.gsub(%r{(.+)\-(.+)$}) { "#{$1}-#{$2.upcase}" }) Chris@1295: end Chris@1295: Chris@1295: def format_date(date) Chris@1295: return nil unless date Chris@1295: options = {} Chris@1295: options[:format] = Setting.date_format unless Setting.date_format.blank? Chris@1295: options[:locale] = User.current.language unless User.current.language.blank? Chris@1295: ::I18n.l(date.to_date, options) Chris@1295: end Chris@1295: Chris@1295: def format_time(time, include_date = true) Chris@1295: return nil unless time Chris@1295: options = {} Chris@1295: options[:format] = (Setting.time_format.blank? ? :time : Setting.time_format) Chris@1295: options[:locale] = User.current.language unless User.current.language.blank? Chris@1295: time = time.to_time if time.is_a?(String) Chris@1295: zone = User.current.time_zone Chris@1295: local = zone ? time.in_time_zone(zone) : (time.utc? ? time.localtime : time) Chris@1295: (include_date ? "#{format_date(local)} " : "") + ::I18n.l(local, options) Chris@1295: end Chris@1295: Chris@1295: def day_name(day) Chris@1295: ::I18n.t('date.day_names')[day % 7] Chris@1295: end Chris@1295: Chris@1295: def day_letter(day) Chris@1295: ::I18n.t('date.abbr_day_names')[day % 7].first Chris@1295: end Chris@1295: Chris@1295: def month_name(month) Chris@1295: ::I18n.t('date.month_names')[month] Chris@1295: end Chris@1295: Chris@1295: def valid_languages Chris@1295: ::I18n.available_locales Chris@1295: end Chris@1295: Chris@1295: # Returns an array of languages names and code sorted by names, example: Chris@1295: # [["Deutsch", "de"], ["English", "en"] ...] Chris@1295: # Chris@1295: # The result is cached to prevent from loading all translations files. Chris@1295: def languages_options Chris@1295: ActionController::Base.cache_store.fetch "i18n/languages_options" do Chris@1295: valid_languages.map {|lang| [ll(lang.to_s, :general_lang_name), lang.to_s]}.sort {|x,y| x.first <=> y.first } Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def find_language(lang) Chris@1295: @@languages_lookup = valid_languages.inject({}) {|k, v| k[v.to_s.downcase] = v; k } Chris@1295: @@languages_lookup[lang.to_s.downcase] Chris@1295: end Chris@1295: Chris@1295: def set_language_if_valid(lang) Chris@1295: if l = find_language(lang) Chris@1295: ::I18n.locale = l Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: def current_language Chris@1295: ::I18n.locale Chris@1295: end Chris@1295: Chris@1295: # Custom backend based on I18n::Backend::Simple with the following changes: Chris@1295: # * lazy loading of translation files Chris@1295: # * available_locales are determined by looking at translation file names Chris@1295: class Backend Chris@1295: (class << self; self; end).class_eval { public :include } Chris@1295: Chris@1295: module Implementation Chris@1295: include ::I18n::Backend::Base Chris@1295: Chris@1295: # Stores translations for the given locale in memory. Chris@1295: # This uses a deep merge for the translations hash, so existing Chris@1295: # translations will be overwritten by new ones only at the deepest Chris@1295: # level of the hash. Chris@1295: def store_translations(locale, data, options = {}) Chris@1295: locale = locale.to_sym Chris@1295: translations[locale] ||= {} Chris@1295: data = data.deep_symbolize_keys Chris@1295: translations[locale].deep_merge!(data) Chris@1295: end Chris@1295: Chris@1295: # Get available locales from the translations filenames Chris@1295: def available_locales Chris@1295: @available_locales ||= ::I18n.load_path.map {|path| File.basename(path, '.*')}.uniq.sort.map(&:to_sym) Chris@1295: end Chris@1295: Chris@1295: # Clean up translations Chris@1295: def reload! Chris@1295: @translations = nil Chris@1295: @available_locales = nil Chris@1295: super Chris@1295: end Chris@1295: Chris@1295: protected Chris@1295: Chris@1295: def init_translations(locale) Chris@1295: locale = locale.to_s Chris@1295: paths = ::I18n.load_path.select {|path| File.basename(path, '.*') == locale} Chris@1295: load_translations(paths) Chris@1295: translations[locale] ||= {} Chris@1295: end Chris@1295: Chris@1295: def translations Chris@1295: @translations ||= {} Chris@1295: end Chris@1295: Chris@1295: # Looks up a translation from the translations hash. Returns nil if Chris@1295: # eiher key is nil, or locale, scope or key do not exist as a key in the Chris@1295: # nested translations hash. Splits keys or scopes containing dots Chris@1295: # into multiple keys, i.e. currency.format is regarded the same as Chris@1295: # %w(currency format). Chris@1295: def lookup(locale, key, scope = [], options = {}) Chris@1295: init_translations(locale) unless translations.key?(locale) Chris@1295: keys = ::I18n.normalize_keys(locale, key, scope, options[:separator]) Chris@1295: Chris@1295: keys.inject(translations) do |result, _key| Chris@1295: _key = _key.to_sym Chris@1295: return nil unless result.is_a?(Hash) && result.has_key?(_key) Chris@1295: result = result[_key] Chris@1295: result = resolve(locale, _key, result, options.merge(:scope => nil)) if result.is_a?(Symbol) Chris@1295: result Chris@1295: end Chris@1295: end Chris@1295: end Chris@1295: Chris@1295: include Implementation Chris@1295: # Adds fallback to default locale for untranslated strings Chris@1295: include ::I18n::Backend::Fallbacks Chris@1295: end Chris@1295: end Chris@1295: end