|
#--
|
|
# CyborgHood, a distributed system management software.
|
|
# Copyright (c) 2009-2011 Marc Dequènes (Duck) <Duck@DuckCorp.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
#++
|
|
|
|
require 'singleton'
|
|
require 'gettext'
|
|
require 'http_headers'
|
|
|
|
|
|
module CyborgHood
|
|
class I18nController
|
|
include Singleton
|
|
include GetText
|
|
|
|
DEFAULT_LOCALE = "en"
|
|
|
|
def initialize
|
|
@config = Config.instance
|
|
|
|
@available_locales = nil
|
|
@prefs_to_locale_cache = {}
|
|
@translate_mutex = Mutex.new
|
|
end
|
|
|
|
def available_locales
|
|
return @available_locales if @available_locales
|
|
|
|
list = ['en'] + Dir.new(Config::L10N_DIR).select{|d| File.directory?(File.join(Config::L10N_DIR, d)) and d[0..0] != "." }
|
|
# local admin can restrict available locales
|
|
# (may be useful if l10n is partial due to third party plugins)
|
|
list = list & @config.i18n.restricted_locale_set if @config.i18n.restricted_locale_set
|
|
list = ['en'] if list.empty?
|
|
@available_locales = list
|
|
end
|
|
|
|
def set_locale(locale)
|
|
if locale
|
|
set_locale_all(locale)
|
|
else
|
|
set_default_locale
|
|
end
|
|
end
|
|
|
|
def set_default_locale
|
|
set_locale(DEFAULT_LOCALE)
|
|
end
|
|
|
|
def locale_from_prefs(preferred_locales)
|
|
return DEFAULT_LOCALE if preferred_locales.nil?
|
|
|
|
locale = @prefs_to_locale_cache[preferred_locales]
|
|
return locale if locale
|
|
|
|
lang_chooser = HTTPHeaders::AcceptLanguage.parse(preferred_locales)
|
|
if lang_chooser
|
|
ordered_list = lang_chooser.reduce(available_locales)
|
|
locale = ordered_list.empty? ? DEFAULT_LOCALE : ordered_list.first.range
|
|
@prefs_to_locale_cache[preferred_locales] = locale
|
|
else
|
|
# notify the preferred_locales string is not a valid RFC2616 Accept-Language value
|
|
nil
|
|
end
|
|
end
|
|
|
|
def translate(message, preferred_locales)
|
|
locale = locale_from_prefs(preferred_locales)
|
|
|
|
@translate_mutex.synchronize do
|
|
set_locale(locale)
|
|
message.to_s
|
|
end
|
|
end
|
|
end
|
|
|
|
module I18nTranslation
|
|
def self.included(base)
|
|
base.class_eval("include GetText")
|
|
|
|
# translation methods needs to be available at class level too
|
|
base.extend(self)
|
|
end
|
|
include GetText
|
|
|
|
alias :_orig :_
|
|
def _(message, parts = {})
|
|
create_tm(message, parts, :_orig)
|
|
end
|
|
|
|
alias :n_orig :n_
|
|
def n_(message, parts = {})
|
|
create_tm(message, parts, :n_orig)
|
|
end
|
|
|
|
alias :s_orig :s_
|
|
def s_(message, parts = {})
|
|
create_tm(message, parts, :s_orig)
|
|
end
|
|
|
|
alias :ns_orig :ns_
|
|
def ns_(message, parts = {})
|
|
create_tm(message, parts, :ns_orig)
|
|
end
|
|
|
|
alias :np_orig :np_
|
|
def np_(message, parts = {})
|
|
create_tm(message, parts, :np_orig)
|
|
end
|
|
|
|
def tm_(message, parts = {})
|
|
create_tm(message, parts)
|
|
end
|
|
|
|
def create_tm(message, parts, method = nil)
|
|
I18nMessage.new(message, parts, self, method)
|
|
end
|
|
|
|
module_function :_, :n_, :s_, :ns_, :np_
|
|
end
|
|
|
|
class I18nMessage
|
|
include I18nTranslation
|
|
|
|
attr_reader :untranslated, :parts
|
|
|
|
def initialize(untranslated, parts, context, method)
|
|
@untranslated = untranslated % parts
|
|
@parts = parts
|
|
@context = context
|
|
@method = method
|
|
end
|
|
|
|
def to_s
|
|
ch_i18n_msg_method, ch_i18n_msg_untranslated, ch_i18n_msg_parts = @method, @untranslated, @parts
|
|
@context.instance_eval do
|
|
send(ch_i18n_msg_method, ch_i18n_msg_untranslated) % ch_i18n_msg_parts
|
|
end
|
|
end
|
|
|
|
def translate(preferred_locales)
|
|
I18nController.instance.translate(self, preferred_locales)
|
|
end
|
|
end
|
|
end
|