2 # irb/locale.rb - internationalization module
3 # $Release Version: 0.9.5$
5 # by Keiju ISHITSUKA(keiju@ruby-lang.org)
13 @RCS_ID='-$Id: locale.rb 20889 2008-12-20 02:02:48Z yugui $-'
16 (?<language>[[:alpha:]]{2})
18 (?<territory>[[:alpha:]]{2,3})
29 @@legacy_encoding_alias_map = {}.freeze
31 def initialize(locale = nil)
32 @lang = @territory = @encoding_name = @modifier = nil
33 @locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
34 if m = LOCALE_NAME_RE.match(@locale)
35 @lang, @territory, @encoding_name, @modifier = m[:language], m[:territory], m[:codeset], m[:modifier]
38 begin load 'irb/encoding_aliases.rb'; rescue LoadError; end
39 if @encoding = @@legacy_encoding_alias_map[@encoding_name]
40 warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"]
42 @encoding = Encoding.find(@encoding_name) rescue nil
45 @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
48 attr_reader :lang, :territory, :encoding, :modifieer
72 ary = opts.collect{|opt| String(opt)}
82 ary = opts.collect{|opt| String(opt)}
86 def require(file, priv = nil)
87 rex = Regexp.new("lc/#{Regexp.quote(file)}\.(so|o|sl|rb)?")
88 return false if $".find{|f| f =~ rex}
103 load(f = file + ".rb")
107 return ruby_require(file)
111 alias toplevel_load load
113 def load(file, priv=nil)
114 dir = File.dirname(file)
115 dir = "" if dir == "."
116 base = File.basename(file)
119 lc_path = search_file(dir, base)
120 return real_load(lc_path, priv) if lc_path
124 lc_path = search_file(path + "/" + dir, base)
125 return real_load(lc_path, priv) if lc_path
127 raise LoadError, "No such file to load -- #{file}"
130 def real_load(path, priv)
131 src = MagicFile.open(path){|f| f.read}
133 eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
135 eval(src, TOPLEVEL_BINDING, path)
140 def find(file , paths = $:)
141 dir = File.dirname(file)
142 dir = "" if dir == "."
143 base = File.basename(file)
145 return lc_path = search_file(dir, base)
148 if lc_path = search_file(path + "/" + dir, base)
156 def search_file(path, file)
157 each_sublocale do |lc|
158 full_path = path + lc_path(file, lc)
159 return full_path if File.exist?(full_path)
165 def lc_path(file = "", lc = @locale)
169 LOCALE_DIR + @lang + "/" + file
178 yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier
179 yield "#{@lang}_#{@territory}.#{@encoding_name}"
181 yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier
182 yield "#{@lang}_#{@territory}"
184 yield "#{@lang}@#{@modifier}" if @modifier
189 private :each_sublocale