diff options
author | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-01-16 12:34:36 +0000 |
---|---|---|
committer | yugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-01-16 12:34:36 +0000 |
commit | e58b0f52b03e23c13a8b323e8ef60b30c43708be (patch) | |
tree | da394e370674c80db55fd23209df47c3b88f2f42 /lib | |
parent | 67cabb2f17ef84216e2614e4131aae9e92b4cc99 (diff) |
merges r30448 and r30498 from trunk into ruby_1_9_2.
--
* lib/irb/locale.rb (IRB::Locale#search_file): make it possible
to load a localization from a gem.
(IRB::Locale#lc_path): obsoleted because of the change of #search_file
(IRB::Locale#each_localized_path): new private method, based on lc_path
(IRB::Locale#find): follows the change of #search_file.
(IRB::Locale#load): removed duplicate with #find.
--
* lib/irb/locale.rb (IRB::Locale::LOCALE_NAME_RE):
some platoform has a locale without territory but with
encoding.
(#each_sub_locale): ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_2@30563 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/irb/locale.rb | 91 |
1 files changed, 39 insertions, 52 deletions
diff --git a/lib/irb/locale.rb b/lib/irb/locale.rb index 9924409ba1..142b9a5951 100644 --- a/lib/irb/locale.rb +++ b/lib/irb/locale.rb @@ -13,16 +13,10 @@ module IRB @RCS_ID='-$Id$-' LOCALE_NAME_RE = %r[ - (?<language>[[:alpha:]]{2}) - (?:_ - (?<territory>[[:alpha:]]{2,3}) - (?:\. - (?<codeset>[^@]+) - )? - )? - (?:@ - (?<modifier>.*) - )? + (?<language>[[:alpha:]]{2,3}) + (?:_ (?<territory>[[:alpha:]]{2,3}) )? + (?:\. (?<codeset>[^@]+) )? + (?:@ (?<modifier>.*) )? ]x LOCALE_DIR = "/lc/" @@ -50,7 +44,7 @@ module IRB def String(mes) mes = super(mes) if @encoding - mes.encode(@encoding) + mes.encode(@encoding, undef: :replace) else mes end @@ -111,22 +105,27 @@ module IRB alias toplevel_load load def load(file, priv=nil) + found = find(file) + if found + return real_load(found, priv) + else + raise LoadError, "No such file to load -- #{file}" + end + end + + def find(file , paths = $:) dir = File.dirname(file) dir = "" if dir == "." base = File.basename(file) - 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 + if dir.start_with?('/') + return each_localized_path(dir, base).find{|full_path| File.readable? full_path} + else + return search_file(paths, dir, base) end - raise LoadError, "No such file to load -- #{file}" end + private def real_load(path, priv) src = MagicFile.open(path){|f| f.read} if priv @@ -135,41 +134,30 @@ module IRB eval(src, TOPLEVEL_BINDING, path) end end - private :real_load - def find(file , paths = $:) - dir = File.dirname(file) - dir = "" if dir == "." - base = File.basename(file) - if dir =~ /^\// - return lc_path = search_file(dir, base) - else - for path in $: - if lc_path = search_file(path + "/" + dir, base) - return lc_path - end - end + # @param paths load paths in which IRB find a localized file. + # @param dir directory + # @param file basename to be localized + # + # typically, for the parameters and a <path> in paths, it searches + # <path>/<dir>/<locale>/<file> + def search_file(lib_paths, dir, file) + each_localized_path(dir, file) do |lc_path| + lib_paths.each do |libpath| + full_path = File.join(libpath, lc_path) + return full_path if File.readable?(full_path) + end + redo if Gem.try_activate(lc_path) end nil end - def search_file(path, file) + def each_localized_path(dir, file) + return enum_for(:each_localized_path) unless block_given? 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 = @locale) - if lc.nil? - LOCALE_DIR + file - else - LOCALE_DIR + @lang + "/" + file + yield lc.nil? ? File.join(dir, LOCALE_DIR, file) : File.join(dir, LOCALE_DIR, lc, file) end end - private :lc_path def each_sublocale if @lang @@ -181,15 +169,14 @@ module IRB yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier yield "#{@lang}_#{@territory}" end + if @encoding_name + yield "#{@lang}.#{@encoding_name}@#{@modifier}" if @modifier + yield "#{@lang}.#{@encoding_name}" + end yield "#{@lang}@#{@modifier}" if @modifier yield "#{@lang}" end yield nil end - private :each_sublocale end end - - - - |