Project

General

Profile

Download (3.17 KB) Statistics
| Branch: | Tag: | Revision:
#--
# CyborgHood, a distributed system management software.
# Copyright (c) 2009-2010 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'


module CyborgHood
class I18nController
include Singleton
include GetText

def initialize
@config = Config.instance
end

def available_languages
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 languages
# (may be useful if l10n is partial due to third party plugins)
list = list & @config.i18n.restricted_language_set if @config.i18n and @config.i18n.restricted_language_set
list = ['en'] if list.empty?
list
end

def set_language(lang)
set_locale_all(lang)
end

def set_default_language
set_language('en')
end

def set_language_for_user(user)
if user.nil?
set_default_language
else
logger.debug "User preference for language: " + user.preferredLanguage

lang = user.prefered_language(self.available_languages)
if lang.nil?
logger.debug "No available language fits the user preference, using english"
lang = 'en'
else
logger.debug "Language better fitting user preference: " + lang
end

set_language(lang)
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 to_s
@translated
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(:_orig, message, parts)
end

alias :n_orig :n_
def n_(message, parts = {})
create_tm(:n_orig, message, parts)
end

alias :s_orig :s_
def s_(message, parts = {})
create_tm(:s_orig, message, parts)
end

alias :ns_orig :ns_
def ns_(message, parts = {})
create_tm(:ns_orig, message, parts)
end

alias :np_orig :np_
def np_(message, parts = {})
create_tm(:np_orig, message, parts)
end

def create_tm(method, message, parts)
I18nMessage.new(self.send(method, message), message, parts)
end

module_function :_, :n_, :s_, :ns_, :np_
end
end
(5-5/6)