Revision 51546fe5
Added by Marc Dequènes over 13 years ago
- ID 51546fe57a7c69cdece4127636d9bb2eb3d7528d
bin/librarian | ||
---|---|---|
module CyborgHood
|
||
module LibrarianLand
|
||
include I18nTranslation
|
||
bindtextdomain("cyborghood_librarian", {:path => Config::L10N_DIR, :charset => "UTF-8"})
|
||
bindtextdomain("cyborghood_librarian", {:path => Config::L10N_DIR, :output_charset => "UTF-8"})
|
||
|
||
class Librarian < Cyborg
|
||
# load config before modules are included
|
bin/mapmaker | ||
---|---|---|
|
||
module MapMakerLand
|
||
include I18nTranslation
|
||
bindtextdomain("cyborghood_mapmaker", {:path => Config::L10N_DIR, :charset => "UTF-8"})
|
||
bindtextdomain("cyborghood_mapmaker", {:path => Config::L10N_DIR, :output_charset => "UTF-8"})
|
||
|
||
class MapMaker < Cyborg
|
||
# load config before modules are included
|
bin/postman | ||
---|---|---|
module CyborgHood
|
||
module PostmanLand
|
||
include I18nTranslation
|
||
bindtextdomain("cyborghood_postman", {:path => Config::L10N_DIR, :charset => "UTF-8"})
|
||
bindtextdomain("cyborghood_postman", {:path => Config::L10N_DIR, :output_charset => "UTF-8"})
|
||
|
||
class Postman < Cyborg
|
||
# load config before modules are included
|
bin/test_client | ||
---|---|---|
module CyborgHood
|
||
module TestClientHome
|
||
include I18nTranslation
|
||
bindtextdomain("cyborghood_mapmaker", {:path => Config::L10N_DIR, :charset => "UTF-8"})
|
||
bindtextdomain("cyborghood_mapmaker", {:path => Config::L10N_DIR, :output_charset => "UTF-8"})
|
||
|
||
class TestClientClient < Cyborg
|
||
# load config before modules are included
|
||
... | ... | |
#know? "MapMaker", :k1, "/Zones"
|
||
#know? "MapMaker", :k2, "/prout"
|
||
set_user "guihome"
|
||
set_preferred_locales "fr"
|
||
ask "Clerk", :dns_info, "/Commands/DNS/INFO"
|
||
on_error do
|
||
puts "PLOUF"
|
lib/cyborghood.rb | ||
---|---|---|
module CyborgHood
|
||
include I18nTranslation
|
||
ENV['LC_ALL'] = "C"
|
||
bindtextdomain("cyborghood", {:path => Config::L10N_DIR, :charset => "UTF-8"})
|
||
bindtextdomain("cyborghood", {:path => Config::L10N_DIR, :output_charset => "UTF-8"})
|
||
|
||
def register_services(container)
|
||
container.define do |b|
|
lib/cyborghood/base/language.rb | ||
---|---|---|
|
||
require 'singleton'
|
||
require 'gettext'
|
||
require 'http_headers'
|
||
|
||
|
||
module CyborgHood
|
||
... | ... | |
|
||
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?
|
||
list
|
||
@available_locales = list
|
||
end
|
||
|
||
def set_locale(locale)
|
||
set_locale_all(locale)
|
||
if locale
|
||
set_locale_all(locale)
|
||
else
|
||
set_default_locale
|
||
end
|
||
end
|
||
|
||
def set_default_locale
|
||
set_locale(DEFAULT_LOCALE)
|
||
end
|
||
|
||
def set_locale_for_user(user)
|
||
if user.nil?
|
||
set_default_locale
|
||
elsif user.preferredLanguage.nil?
|
||
logger.debug "User has no language preference, using default"
|
||
set_default_locale
|
||
def locale_from_prefs(preferred_locales)
|
||
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
|
||
logger.debug "User preference for language: " + user.preferredLanguage
|
||
|
||
locale = user.prefered_language(self.available_locales)
|
||
if locale.nil?
|
||
logger.debug "No available language fits the user preference, using english"
|
||
set_default_locale
|
||
else
|
||
logger.debug "Language better fitting user preference: " + locale
|
||
set_locale(locale)
|
||
end
|
||
# notify the preferred_locales string is not a valid RFC2616 Accept-Language value
|
||
nil
|
||
end
|
||
end
|
||
end
|
||
|
||
class I18nMessage
|
||
attr_reader :translated, :untranslated, :parts
|
||
|
||
def initialize(translated, untranslated, parts)
|
||
@translated = translated % parts
|
||
@untranslated = untranslated % parts
|
||
@parts = parts
|
||
end
|
||
def translate(message, preferred_locales)
|
||
locale = locale_from_prefs(preferred_locales)
|
||
|
||
def to_s
|
||
@translated
|
||
@translate_mutex.synchronize do
|
||
set_locale(locale)
|
||
message.to_s
|
||
end
|
||
end
|
||
end
|
||
|
||
... | ... | |
|
||
def create_tm(message, parts, method = nil)
|
||
translated_message = (method.nil? ? message : self.send(method, message))
|
||
I18nMessage.new(translated_message, message, parts)
|
||
I18nMessage.new(message, parts, method)
|
||
end
|
||
|
||
module_function :_, :n_, :s_, :ns_, :np_
|
||
end
|
||
|
||
class I18nMessage
|
||
include I18nTranslation
|
||
|
||
attr_reader :translated, :untranslated, :parts
|
||
|
||
def initialize(untranslated, parts, method)
|
||
@untranslated = untranslated % parts
|
||
@parts = parts
|
||
@method = method
|
||
|
||
@translated = nil
|
||
end
|
||
|
||
def to_s
|
||
self.send(@method, @untranslated) % parts
|
||
end
|
||
|
||
def translate(preferred_locales)
|
||
I18nController.instance.translate(self, preferred_locales)
|
||
end
|
||
end
|
||
end
|
lib/cyborghood/cyborg/dsl.rb | ||
---|---|---|
#++
|
||
|
||
require 'active_support/basic_object'
|
||
require 'http_headers'
|
||
require 'set'
|
||
|
||
|
||
... | ... | |
|
||
def set_preferred_locales(prefs)
|
||
_add_subtask("setting/preferred_locales") do |subtask|
|
||
logger.debug "Task '#{@name}': setting preferred locales"
|
||
lang_chooser = HTTPHeaders::AcceptLanguage.parse(prefs)
|
||
if lang_chooser
|
||
@preferred_locales = prefs
|
||
|
||
ordered_list = lang_chooser.reduce(I18nController.instance.available_locales)
|
||
@locale = ordered_list.empty? ? I18nController::DEFAULT_LOCALE : ordered_list.first.range
|
||
logger.debug "Task '#{@name}': locale chosen: #{@locale}"
|
||
else
|
||
@errors << CyberError.new(:unrecoverable, "botnet/client/dsl", _("unparsable preferred locales"))
|
||
end
|
||
logger.debug "Task '#{@name}': setting preferred locales to: #{prefs}"
|
||
@preferred_locales = prefs
|
||
|
||
subtask.finish
|
||
end
|
||
end
|
Also available in: Unified diff
[evol] adapted current I18n system for late translation