diff options
author | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-18 13:09:26 +0000 |
---|---|---|
committer | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-12-18 13:09:26 +0000 |
commit | 5c1bd53c92f888f45f6a843f2ef70f2fcdac077f (patch) | |
tree | 5e59d6e126eff13c9e661aa12f0b33f5db20b6c8 /lib/irb/locale.rb | |
parent | 52d481d8de7ee3e0dfaef7322d540f95223bf41b (diff) | |
download | ruby-5c1bd53c92f888f45f6a843f2ef70f2fcdac077f.tar.gz |
* lib/irb/init.rb (IRB.opt_parse): (M17N) adds -U and -E as command
line options. [ruby-dev:37161]. Fixes #711.
improved long optinos.
* lib/irb/init.rb (IRB.set_encoding): new subroutine for IRB.opt_parse
* lib/irb/input-method.rb (IRB::StdioInputMethod): (M17N) regards
scripts that user types as encoded in the external_encoding.
* lib/irb/input-method.rb (IRB::ReadlineInputMethod): ditto.
* lib/irb/input-method.rb (IRB::FileInputMethod): (M17N) respects
magic comment.
* lib/irb/help.rb (IRB.print_usage): (M17N) respects magic comment
in the resource file.
* lib/irb/lc/help-message: adds -U and -E.
* lib/irb/lc/ja/help-message: ditto. re-encoded from ISO-2022-JP into UTF-8.
* lib/irb/lc/ja/encoding_aliases.rb: new file. provides Japanese specific
character encoding name table for backward compatibility.
* lib/irb/lc/ja/error.rb: re-eoncoded from ISO-2022-JP into UTF-8.
magic comment.
* lib/irb/locale.rb: integrated with Ruby 1.9's M17N support.
* lib/irb/magic-file.rb: new file. utility to handle files with magic
comment and opens in the correct encoding.
* lib/irb/ruby-lex.rb (RubyLex#each_top_level_statement): recovers
character encoding for a statement after it lexed so that irb can
eval the statement in correct encoding.
* lib/irb/src_encoding.rb: new file. utility.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@20862 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/irb/locale.rb')
-rw-r--r-- | lib/irb/locale.rb | 109 |
1 files changed, 60 insertions, 49 deletions
diff --git a/lib/irb/locale.rb b/lib/irb/locale.rb index 855be31052..d36cc5a805 100644 --- a/lib/irb/locale.rb +++ b/lib/irb/locale.rb @@ -8,43 +8,51 @@ # # # - -autoload :Kconv, "kconv" - module IRB class Locale @RCS_ID='-$Id$-' - JPDefaultLocale = "ja" + LOCALE_NAME_RE = %r[ + (?<language>[[:alpha:]]{2}) + (?:_ + (?<territory>[[:alpha:]]{2,3}) + (?:\. + (?<codeset>[^@]+) + )? + )? + (?:@ + (?<modifier>.*) + )? + ]x LOCALE_DIR = "/lc/" - def initialize(locale = nil) - @lang = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C" - end - - attr_reader :lang + @@legacy_encoding_alias_map = {}.freeze - def lc2kconv(lang) - case lang - when "ja_JP.ujis", "ja_JP.euc", "ja_JP.eucJP", "ja_JP.EUC-JP" - Kconv::EUC - when "ja_JP.sjis", "ja_JP.SJIS" - Kconv::SJIS - when /ja_JP.utf-?8/i - Kconv::UTF8 + def initialize(locale = nil) + @locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C" + if m = LOCALE_NAME_RE.match(@locale) + @lang, @territory, @encoding_name, @modifier = m[:language], m[:territory], m[:codeset], m[:modifier] + + if @encoding_name + begin; load 'irb/encoding_aliases.rb' rescue LoadError; end + if @encoding = @@legacy_encoding_alias_map[@encoding_name] + warn "%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"] + end + @encoding = Encoding.find(@encoding_name) rescue nil + end end + @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT) end - private :lc2kconv + + attr_reader :lang, :territory, :encoding, :modifieer def String(mes) mes = super(mes) - case @lang - when /^ja/ - mes = Kconv::kconv(mes, lc2kconv(@lang)) + if @encoding + mes.encode(@encoding) else mes end - mes end def format(*opts) @@ -106,27 +114,20 @@ module IRB dir = "" if dir == "." base = File.basename(file) - if /^ja(_JP)?$/ =~ @lang - back, @lang = @lang, "C" + if dir[0] == ?/ #/ + lc_path = search_file(dir, base) + return real_load(lc_path, priv) if lc_path end - begin - if dir[0] == ?/ #/ - lc_path = search_file(dir, base) - return real_load(lc_path, priv) if lc_path - end - - for path in $: - lc_path = search_file(path + "/" + dir, base) - return real_load(lc_path, priv) if lc_path - end - ensure - @lang = back if back + + for path in $: + lc_path = search_file(path + "/" + dir, base) + return real_load(lc_path, priv) if lc_path end raise LoadError, "No such file to load -- #{file}" end def real_load(path, priv) - src = self.String(File.read(path)) + src = MagicFile.open(path){|f| f.read} if priv eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)}) else @@ -152,29 +153,39 @@ module IRB end def search_file(path, file) - if File.exist?(p1 = path + lc_path(file, "C")) - if File.exist?(p2 = path + lc_path(file)) - return p2 - else - end - return p1 - else + each_sublocale do |lc| + full_path = path + lc_path(file, lc) + return full_path if File.exist?(full_path) end nil end private :search_file - def lc_path(file = "", lc = @lang) - case lc - when "C" + def lc_path(file = "", lc = @locale) + if lc.nil? LOCALE_DIR + file - when /^ja/ - LOCALE_DIR + "ja/" + file else LOCALE_DIR + @lang + "/" + file end end private :lc_path + + def each_sublocale + if @lang + if @territory + if @encoding_name + yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier + yield "#{@lang}_#{@territory}.#{@encoding_name}" + end + yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier + yield "#{@lang}_#{@territory}" + end + yield "#{@lang}@#{@modifier}" if @modifier + yield "#{@lang}" + end + yield nil + end + private :each_sublocale end end |