OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / lib / irb / locale.rb
1 #
2 #   irb/locale.rb - internationalization module
3 #       $Release Version: 0.9.5$
4 #       $Revision: 20889 $
5 #       by Keiju ISHITSUKA(keiju@ruby-lang.org)
6 #
7 # --
8 #
9 #   
10 #
11 module IRB
12   class Locale
13     @RCS_ID='-$Id: locale.rb 20889 2008-12-20 02:02:48Z yugui $-'
14
15     LOCALE_NAME_RE = %r[
16       (?<language>[[:alpha:]]{2})
17       (?:_
18        (?<territory>[[:alpha:]]{2,3})
19        (?:\.
20         (?<codeset>[^@]+)
21        )?
22       )?
23       (?:@
24        (?<modifier>.*)
25       )?
26     ]x
27     LOCALE_DIR = "/lc/"
28
29     @@legacy_encoding_alias_map = {}.freeze
30
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]
36
37         if @encoding_name
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}"]
41           end
42           @encoding = Encoding.find(@encoding_name) rescue nil
43         end
44       end
45       @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
46     end
47
48     attr_reader :lang, :territory, :encoding, :modifieer
49
50     def String(mes)
51       mes = super(mes)
52       if @encoding
53         mes.encode(@encoding) 
54       else
55         mes
56       end
57     end
58
59     def format(*opts)
60       String(super(*opts))
61     end
62
63     def gets(*rs)
64       String(super(*rs))
65     end
66
67     def readline(*rs)
68       String(super(*rs))
69     end
70
71     def print(*opts)
72       ary = opts.collect{|opt| String(opt)}
73       super(*ary)
74     end
75
76     def printf(*opts)
77       s = format(*opts)
78       print s
79     end
80
81     def puts(*opts)
82       ary = opts.collect{|opt| String(opt)}
83       super(*ary)
84     end
85
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}
89
90       case file
91       when /\.rb$/
92         begin
93           load(file, priv)
94           $".push file
95           return true
96         rescue LoadError
97         end
98       when /\.(so|o|sl)$/
99         return super
100       end
101
102       begin
103         load(f = file + ".rb")
104         $".push f  #"
105         return true
106       rescue LoadError
107         return ruby_require(file)
108       end
109     end
110
111     alias toplevel_load load
112     
113     def load(file, priv=nil)
114       dir = File.dirname(file)
115       dir = "" if dir == "."
116       base = File.basename(file)
117
118       if dir[0] == ?/ #/
119         lc_path = search_file(dir, base)
120         return real_load(lc_path, priv) if lc_path
121       end
122
123       for path in $:
124         lc_path = search_file(path + "/" + dir, base)
125         return real_load(lc_path, priv) if lc_path
126       end
127       raise LoadError, "No such file to load -- #{file}"
128     end 
129
130     def real_load(path, priv)
131       src = MagicFile.open(path){|f| f.read}
132       if priv
133         eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
134       else
135         eval(src, TOPLEVEL_BINDING, path)
136       end
137     end
138     private :real_load
139
140     def find(file , paths = $:)
141       dir = File.dirname(file)
142       dir = "" if dir == "."
143       base = File.basename(file)
144       if dir[0] == ?/ #/
145           return lc_path = search_file(dir, base)
146       else
147         for path in $:
148           if lc_path = search_file(path + "/" + dir, base)
149             return lc_path
150           end
151         end
152       end
153       nil
154     end
155
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)
160       end
161       nil
162     end
163     private :search_file
164
165     def lc_path(file = "", lc = @locale)
166       if lc.nil?
167         LOCALE_DIR + file
168       else
169         LOCALE_DIR + @lang + "/" + file
170       end
171     end
172     private :lc_path
173
174     def each_sublocale
175       if @lang
176         if @territory
177           if @encoding_name
178             yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier
179             yield "#{@lang}_#{@territory}.#{@encoding_name}"
180           end
181           yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier
182           yield "#{@lang}_#{@territory}"
183         end
184         yield "#{@lang}@#{@modifier}" if @modifier
185         yield "#{@lang}"
186       end
187       yield nil
188     end
189     private :each_sublocale
190   end
191 end
192
193
194
195