1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
=begin
textdomainmanager.rb - Manage TextDomains.
Copyright (C) 2006 Masao Mutoh
You may redistribute it and/or modify it under the same
license terms as Ruby.
$Id: textdomainmanager.rb,v 1.2 2006/06/11 15:36:20 mutoh Exp $
=end
require 'gettext/locale'
require 'gettext/textdomain'
module GetText
# Manage TextDomain (Internal use only)
# A class/module is able to have plural textdomains.
class TextDomainManager
include Enumerable
attr_reader :target
@@output_charset = ENV["OUTPUT_CHARSET"]
@@textdomain_all = {}
# Sets the current output_charset.
# * charset: output_charset.
# * Returns: output_charset.
def self.output_charset=(charset)
@@output_charset = charset
end
# Gets the current output_charset.
# * Returns: output_charset.
def self.output_charset
@@output_charset
end
def self.each_all
@@textdomain_all.each do |k, textdomain|
yield textdomain
end
end
def self.textdomain(domainname)
@@textdomain_all[domainname]
end
# Initialize a TextDomainManager
# * target: a target class/module to bind this TextDomainManager.
# * locale: a Locale::Object.
def initialize(target, locale)
@target = target
@locale = locale
@textdomains = {}
end
# Add a textdomain
# * options: If they aren't set or invalid, default values are used.
# * :path - the path to the mo-files. If not set, it will search default paths such as
# /usr/share/locale, /usr/local/share/locale)
def add_textdomain(domainname, options = {})
path = options[:path]
if $DEBUG
$stderr.print "Bind the domain '#{domainname}' to '#{@target}'. "
$stderr.print "Current locale is #{@locale.inspect}\n"
end
textdomain = @@textdomain_all[domainname]
if textdomain
textdomain.set_locale(@locale)
else
textdomain = TextDomain.new(domainname, path, @locale)
@@textdomain_all[domainname] = textdomain
end
@textdomains[domainname] = textdomain
textdomain
end
# Iterate textdomains.
def each
@textdomains.each do |k, textdomain|
yield textdomain
end
self
end
# Sets locale such as "de", "fr", "it", "ko", "ja_JP.eucJP", "zh_CN.EUC" ...
#
# Notice that you shouldn't use this for your own Libraries.
# * locale: a locale string or Locale::Object.
# * force: Change locale forcely.
# * Returns: self
def set_locale(locale, force = false)
if locale != @locale or force
each do |textdomain|
textdomain.set_locale(locale)
end
@locale = locale
end
self
end
end
end
|