1 | #
|
---|
2 | # irb/locale.rb - internationalization module
|
---|
3 | # $Release Version: 0.9.5$
|
---|
4 | # $Revision: 11708 $
|
---|
5 | # $Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
|
---|
6 | # by Keiju ISHITSUKA([email protected])
|
---|
7 | #
|
---|
8 | # --
|
---|
9 | #
|
---|
10 | #
|
---|
11 | #
|
---|
12 |
|
---|
13 | autoload :Kconv, "kconv"
|
---|
14 |
|
---|
15 | module IRB
|
---|
16 | class Locale
|
---|
17 | @RCS_ID='-$Id: locale.rb 11708 2007-02-12 23:01:19Z shyouhei $-'
|
---|
18 |
|
---|
19 | JPDefaultLocale = "ja"
|
---|
20 | LOCALE_DIR = "/lc/"
|
---|
21 |
|
---|
22 | def initialize(locale = nil)
|
---|
23 | @lang = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
|
---|
24 | end
|
---|
25 |
|
---|
26 | attr_reader :lang
|
---|
27 |
|
---|
28 | def lc2kconv(lang)
|
---|
29 | case lang
|
---|
30 | when "ja_JP.ujis", "ja_JP.euc", "ja_JP.eucJP"
|
---|
31 | Kconv::EUC
|
---|
32 | when "ja_JP.sjis", "ja_JP.SJIS"
|
---|
33 | Kconv::SJIS
|
---|
34 | when /ja_JP.utf-?8/i
|
---|
35 | Kconv::UTF8
|
---|
36 | end
|
---|
37 | end
|
---|
38 | private :lc2kconv
|
---|
39 |
|
---|
40 | def String(mes)
|
---|
41 | mes = super(mes)
|
---|
42 | case @lang
|
---|
43 | when /^ja/
|
---|
44 | mes = Kconv::kconv(mes, lc2kconv(@lang))
|
---|
45 | else
|
---|
46 | mes
|
---|
47 | end
|
---|
48 | mes
|
---|
49 | end
|
---|
50 |
|
---|
51 | def format(*opts)
|
---|
52 | String(super(*opts))
|
---|
53 | end
|
---|
54 |
|
---|
55 | def gets(*rs)
|
---|
56 | String(super(*rs))
|
---|
57 | end
|
---|
58 |
|
---|
59 | def readline(*rs)
|
---|
60 | String(super(*rs))
|
---|
61 | end
|
---|
62 |
|
---|
63 | def print(*opts)
|
---|
64 | ary = opts.collect{|opt| String(opt)}
|
---|
65 | super(*ary)
|
---|
66 | end
|
---|
67 |
|
---|
68 | def printf(*opts)
|
---|
69 | s = format(*opts)
|
---|
70 | print s
|
---|
71 | end
|
---|
72 |
|
---|
73 | def puts(*opts)
|
---|
74 | ary = opts.collect{|opt| String(opt)}
|
---|
75 | super(*ary)
|
---|
76 | end
|
---|
77 |
|
---|
78 | def require(file, priv = nil)
|
---|
79 | rex = Regexp.new("lc/#{Regexp.quote(file)}\.(so|o|sl|rb)?")
|
---|
80 | return false if $".find{|f| f =~ rex}
|
---|
81 |
|
---|
82 | case file
|
---|
83 | when /\.rb$/
|
---|
84 | begin
|
---|
85 | load(file, priv)
|
---|
86 | $".push file
|
---|
87 | return true
|
---|
88 | rescue LoadError
|
---|
89 | end
|
---|
90 | when /\.(so|o|sl)$/
|
---|
91 | return super
|
---|
92 | end
|
---|
93 |
|
---|
94 | begin
|
---|
95 | load(f = file + ".rb")
|
---|
96 | $".push f #"
|
---|
97 | return true
|
---|
98 | rescue LoadError
|
---|
99 | return ruby_require(file)
|
---|
100 | end
|
---|
101 | end
|
---|
102 |
|
---|
103 | alias toplevel_load load
|
---|
104 |
|
---|
105 | def load(file, priv=nil)
|
---|
106 | dir = File.dirname(file)
|
---|
107 | dir = "" if dir == "."
|
---|
108 | base = File.basename(file)
|
---|
109 |
|
---|
110 | if /^ja(_JP)?$/ =~ @lang
|
---|
111 | back, @lang = @lang, "C"
|
---|
112 | end
|
---|
113 | begin
|
---|
114 | if dir[0] == ?/ #/
|
---|
115 | lc_path = search_file(dir, base)
|
---|
116 | return real_load(lc_path, priv) if lc_path
|
---|
117 | end
|
---|
118 |
|
---|
119 | for path in $:
|
---|
120 | lc_path = search_file(path + "/" + dir, base)
|
---|
121 | return real_load(lc_path, priv) if lc_path
|
---|
122 | end
|
---|
123 | ensure
|
---|
124 | @lang = back if back
|
---|
125 | end
|
---|
126 | raise LoadError, "No such file to load -- #{file}"
|
---|
127 | end
|
---|
128 |
|
---|
129 | def real_load(path, priv)
|
---|
130 | src = self.String(File.read(path))
|
---|
131 | if priv
|
---|
132 | eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
|
---|
133 | else
|
---|
134 | eval(src, TOPLEVEL_BINDING, path)
|
---|
135 | end
|
---|
136 | end
|
---|
137 | private :real_load
|
---|
138 |
|
---|
139 | def find(file , paths = $:)
|
---|
140 | dir = File.dirname(file)
|
---|
141 | dir = "" if dir == "."
|
---|
142 | base = File.basename(file)
|
---|
143 | if dir[0] == ?/ #/
|
---|
144 | return lc_path = search_file(dir, base)
|
---|
145 | else
|
---|
146 | for path in $:
|
---|
147 | if lc_path = search_file(path + "/" + dir, base)
|
---|
148 | return lc_path
|
---|
149 | end
|
---|
150 | end
|
---|
151 | end
|
---|
152 | nil
|
---|
153 | end
|
---|
154 |
|
---|
155 | def search_file(path, file)
|
---|
156 | if File.exist?(p1 = path + lc_path(file, "C"))
|
---|
157 | if File.exist?(p2 = path + lc_path(file))
|
---|
158 | return p2
|
---|
159 | else
|
---|
160 | end
|
---|
161 | return p1
|
---|
162 | else
|
---|
163 | end
|
---|
164 | nil
|
---|
165 | end
|
---|
166 | private :search_file
|
---|
167 |
|
---|
168 | def lc_path(file = "", lc = @lang)
|
---|
169 | case lc
|
---|
170 | when "C"
|
---|
171 | LOCALE_DIR + file
|
---|
172 | when /^ja/
|
---|
173 | LOCALE_DIR + "ja/" + file
|
---|
174 | else
|
---|
175 | LOCALE_DIR + @lang + "/" + file
|
---|
176 | end
|
---|
177 | end
|
---|
178 | private :lc_path
|
---|
179 | end
|
---|
180 | end
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 |
|
---|