Project

General

Profile

Download (3.45 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
elsif user.preferredLanguage.nil?
logger.debug "User has no language preference, using default"
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(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)
translated_message = (method.nil? ? message : self.send(method, message))
I18nMessage.new(translated_message, message, parts)
end

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