1# 2# irb/locale.rb - internationalization module 3# $Release Version: 0.9.6$ 4# $Revision: 38358 $ 5# by Keiju ISHITSUKA(keiju@ruby-lang.org) 6# 7# -- 8# 9# 10# 11module IRB # :nodoc: 12 class Locale 13 @RCS_ID='-$Id: locale.rb 38358 2012-12-13 05:22:30Z zzak $-' 14 15 LOCALE_NAME_RE = %r[ 16 (?<language>[[:alpha:]]{2,3}) 17 (?:_ (?<territory>[[:alpha:]]{2,3}) )? 18 (?:\. (?<codeset>[^@]+) )? 19 (?:@ (?<modifier>.*) )? 20 ]x 21 LOCALE_DIR = "/lc/" 22 23 @@legacy_encoding_alias_map = {}.freeze 24 25 def initialize(locale = nil) 26 @lang = @territory = @encoding_name = @modifier = nil 27 @locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C" 28 if m = LOCALE_NAME_RE.match(@locale) 29 @lang, @territory, @encoding_name, @modifier = m[:language], m[:territory], m[:codeset], m[:modifier] 30 31 if @encoding_name 32 begin load 'irb/encoding_aliases.rb'; rescue LoadError; end 33 if @encoding = @@legacy_encoding_alias_map[@encoding_name] 34 warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"] 35 end 36 @encoding = Encoding.find(@encoding_name) rescue nil 37 end 38 end 39 @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT) 40 end 41 42 attr_reader :lang, :territory, :encoding, :modifieer 43 44 def String(mes) 45 mes = super(mes) 46 if @encoding 47 mes.encode(@encoding, undef: :replace) 48 else 49 mes 50 end 51 end 52 53 def format(*opts) 54 String(super(*opts)) 55 end 56 57 def gets(*rs) 58 String(super(*rs)) 59 end 60 61 def readline(*rs) 62 String(super(*rs)) 63 end 64 65 def print(*opts) 66 ary = opts.collect{|opt| String(opt)} 67 super(*ary) 68 end 69 70 def printf(*opts) 71 s = format(*opts) 72 print s 73 end 74 75 def puts(*opts) 76 ary = opts.collect{|opt| String(opt)} 77 super(*ary) 78 end 79 80 def require(file, priv = nil) 81 rex = Regexp.new("lc/#{Regexp.quote(file)}\.(so|o|sl|rb)?") 82 return false if $".find{|f| f =~ rex} 83 84 case file 85 when /\.rb$/ 86 begin 87 load(file, priv) 88 $".push file 89 return true 90 rescue LoadError 91 end 92 when /\.(so|o|sl)$/ 93 return super 94 end 95 96 begin 97 load(f = file + ".rb") 98 $".push f #" 99 return true 100 rescue LoadError 101 return ruby_require(file) 102 end 103 end 104 105 alias toplevel_load load 106 107 def load(file, priv=nil) 108 found = find(file) 109 if found 110 return real_load(found, priv) 111 else 112 raise LoadError, "No such file to load -- #{file}" 113 end 114 end 115 116 def find(file , paths = $:) 117 dir = File.dirname(file) 118 dir = "" if dir == "." 119 base = File.basename(file) 120 121 if dir.start_with?('/') 122 return each_localized_path(dir, base).find{|full_path| File.readable? full_path} 123 else 124 return search_file(paths, dir, base) 125 end 126 end 127 128 private 129 def real_load(path, priv) 130 src = MagicFile.open(path){|f| f.read} 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 138 # @param paths load paths in which IRB find a localized file. 139 # @param dir directory 140 # @param file basename to be localized 141 # 142 # typically, for the parameters and a <path> in paths, it searches 143 # <path>/<dir>/<locale>/<file> 144 def search_file(lib_paths, dir, file) 145 each_localized_path(dir, file) do |lc_path| 146 lib_paths.each do |libpath| 147 full_path = File.join(libpath, lc_path) 148 return full_path if File.readable?(full_path) 149 end 150 redo if defined?(Gem) and Gem.try_activate(lc_path) 151 end 152 nil 153 end 154 155 def each_localized_path(dir, file) 156 return enum_for(:each_localized_path) unless block_given? 157 each_sublocale do |lc| 158 yield lc.nil? ? File.join(dir, LOCALE_DIR, file) : File.join(dir, LOCALE_DIR, lc, file) 159 end 160 end 161 162 def each_sublocale 163 if @lang 164 if @territory 165 if @encoding_name 166 yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier 167 yield "#{@lang}_#{@territory}.#{@encoding_name}" 168 end 169 yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier 170 yield "#{@lang}_#{@territory}" 171 end 172 if @encoding_name 173 yield "#{@lang}.#{@encoding_name}@#{@modifier}" if @modifier 174 yield "#{@lang}.#{@encoding_name}" 175 end 176 yield "#{@lang}@#{@modifier}" if @modifier 177 yield "#{@lang}" 178 end 179 yield nil 180 end 181 end 182end 183