DEFINITIONS
This source file includes following functions.
1 #
2 # irb/locale.rb - internationalization module
3 # $Release Version: 0.9$
4 # $Revision: 1.4 $
5 # $Date: 2002/07/09 11:17:16 $
6 # by Keiju ISHITSUKA(keiju@ishitsuka.com)
7 #
8 # --
9 #
10 #
11 #
12
13 autoload :Tempfile, "tempfile"
14 autoload :Kconv, "kconv"
15
16 module IRB
17 class Locale
18 @RCS_ID='-$Id: locale.rb,v 1.4 2002/07/09 11:17:16 keiju Exp $-'
19
20 JPDefaultLocale = "ja"
21 LOCALE_DIR = "/lc/"
22
23 def initialize(locale = nil)
24 @lang = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"]
25 @lang = "C" unless @lang
26 end
27
28 attr_reader :lang
29
30 def String(mes)
31 mes = super(mes)
32 case @lang
33 when /^ja/
34 @@LC2KCONV = {
35 # "ja" => Kconv::JIS,
36 # "ja_JP" => Kconv::JIS,
37 "ja_JP.ujis" => Kconv::EUC,
38 "ja_JP.euc" => Kconv::EUC,
39 "ja_JP.eucJP" => Kconv::EUC,
40 "ja_JP.sjis" => Kconv::SJIS,
41 "ja_JP.SJIS" => Kconv::SJIS,
42 } unless defined? @@LC2KCONV
43
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(opts)}
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 tmp_base = path.tr("./:", "___")
131 lc_file = Tempfile.new(tmp_base)
132 File.foreach(path) do |line|
133 line = self.String(line)
134 lc_file.print(line)
135 end
136 lc_file.close
137 toplevel_load lc_file.path, priv
138 lc_file.close(true)
139 end
140 private :real_load
141
142 def find(file , paths = $:)
143 dir = File.dirname(file)
144 dir = "" if dir == "."
145 base = File.basename(file)
146 if dir[0] == ?/ #/
147 return lc_path = search_file(dir, base)
148 else
149 for path in $:
150 if lc_path = search_file(path + "/" + dir, base)
151 return lc_path
152 end
153 end
154 end
155 nil
156 end
157
158 def search_file(path, file)
159 if File.exists?(p1 = path + lc_path(file, "C"))
160 if File.exists?(p2 = path + lc_path(file))
161 return p2
162 else
163 end
164 return p1
165 else
166 end
167 nil
168 end
169 private :search_file
170
171 def lc_path(file = "", lc = @lang)
172 case lc
173 when "C"
174 LOCALE_DIR + file
175 when /^ja/
176 LOCALE_DIR + "ja/" + file
177 else
178 LOCALE_DIR + @lang + "/" + file
179 end
180 end
181 private :lc_path
182 end
183 end
184
185
186
187