2001-04-30 13:38:21 -04:00
|
|
|
#
|
|
|
|
# irb/locale.rb - internationalization module
|
2009-07-07 07:36:20 -04:00
|
|
|
# $Release Version: 0.9.6$
|
2001-04-30 13:38:21 -04:00
|
|
|
# $Revision$
|
2005-04-13 11:27:09 -04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-04-30 13:38:21 -04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2001-04-30 13:38:21 -04:00
|
|
|
#
|
|
|
|
module IRB
|
|
|
|
class Locale
|
|
|
|
@RCS_ID='-$Id$-'
|
|
|
|
|
2008-12-18 08:09:26 -05:00
|
|
|
LOCALE_NAME_RE = %r[
|
2011-01-09 02:39:16 -05:00
|
|
|
(?<language>[[:alpha:]]{2,3})
|
|
|
|
(?:_ (?<territory>[[:alpha:]]{2,3}) )?
|
|
|
|
(?:\. (?<codeset>[^@]+) )?
|
|
|
|
(?:@ (?<modifier>.*) )?
|
2008-12-18 08:09:26 -05:00
|
|
|
]x
|
2001-04-30 13:38:21 -04:00
|
|
|
LOCALE_DIR = "/lc/"
|
|
|
|
|
2008-12-18 08:09:26 -05:00
|
|
|
@@legacy_encoding_alias_map = {}.freeze
|
2001-04-30 13:38:21 -04:00
|
|
|
|
2008-12-18 08:09:26 -05:00
|
|
|
def initialize(locale = nil)
|
2008-12-19 21:01:26 -05:00
|
|
|
@lang = @territory = @encoding_name = @modifier = nil
|
2009-03-05 22:56:38 -05:00
|
|
|
@locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
|
2008-12-18 08:09:26 -05:00
|
|
|
if m = LOCALE_NAME_RE.match(@locale)
|
|
|
|
@lang, @territory, @encoding_name, @modifier = m[:language], m[:territory], m[:codeset], m[:modifier]
|
|
|
|
|
|
|
|
if @encoding_name
|
2008-12-19 21:01:26 -05:00
|
|
|
begin load 'irb/encoding_aliases.rb'; rescue LoadError; end
|
2008-12-18 08:09:26 -05:00
|
|
|
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
|
2003-07-24 01:18:47 -04:00
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
@encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
|
2003-07-24 01:18:47 -04:00
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
|
|
|
|
attr_reader :lang, :territory, :encoding, :modifieer
|
2002-11-22 04:14:24 -05:00
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
def String(mes)
|
|
|
|
mes = super(mes)
|
2008-12-18 08:09:26 -05:00
|
|
|
if @encoding
|
2011-01-04 20:13:12 -05:00
|
|
|
mes.encode(@encoding, undef: :replace)
|
2001-04-30 13:38:21 -04:00
|
|
|
else
|
|
|
|
mes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def format(*opts)
|
|
|
|
String(super(*opts))
|
|
|
|
end
|
|
|
|
|
|
|
|
def gets(*rs)
|
|
|
|
String(super(*rs))
|
|
|
|
end
|
|
|
|
|
|
|
|
def readline(*rs)
|
|
|
|
String(super(*rs))
|
|
|
|
end
|
|
|
|
|
|
|
|
def print(*opts)
|
|
|
|
ary = opts.collect{|opt| String(opt)}
|
2001-06-27 11:35:04 -04:00
|
|
|
super(*ary)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def printf(*opts)
|
|
|
|
s = format(*opts)
|
|
|
|
print s
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts(*opts)
|
2006-12-14 04:27:36 -05:00
|
|
|
ary = opts.collect{|opt| String(opt)}
|
2001-06-27 11:35:04 -04:00
|
|
|
super(*ary)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def require(file, priv = nil)
|
|
|
|
rex = Regexp.new("lc/#{Regexp.quote(file)}\.(so|o|sl|rb)?")
|
|
|
|
return false if $".find{|f| f =~ rex}
|
|
|
|
|
|
|
|
case file
|
|
|
|
when /\.rb$/
|
|
|
|
begin
|
|
|
|
load(file, priv)
|
|
|
|
$".push file
|
|
|
|
return true
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
when /\.(so|o|sl)$/
|
|
|
|
return super
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
load(f = file + ".rb")
|
|
|
|
$".push f #"
|
|
|
|
return true
|
|
|
|
rescue LoadError
|
|
|
|
return ruby_require(file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias toplevel_load load
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2001-04-30 13:38:21 -04:00
|
|
|
def load(file, priv=nil)
|
2011-01-04 20:13:12 -05:00
|
|
|
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 = $:)
|
2001-04-30 13:38:21 -04:00
|
|
|
dir = File.dirname(file)
|
|
|
|
dir = "" if dir == "."
|
|
|
|
base = File.basename(file)
|
|
|
|
|
2011-01-04 20:13:12 -05:00
|
|
|
if dir.start_with?('/')
|
|
|
|
return each_localized_path(dir, base).find{|full_path| File.readable? full_path}
|
|
|
|
else
|
|
|
|
return search_file(paths, dir, base)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
|
2011-01-04 20:13:12 -05:00
|
|
|
private
|
2001-04-30 13:38:21 -04:00
|
|
|
def real_load(path, priv)
|
2008-12-18 08:09:26 -05:00
|
|
|
src = MagicFile.open(path){|f| f.read}
|
2005-01-16 17:09:29 -05:00
|
|
|
if priv
|
|
|
|
eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
|
|
|
|
else
|
|
|
|
eval(src, TOPLEVEL_BINDING, path)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-04 20:13:12 -05:00
|
|
|
# @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
|
2011-01-31 00:46:50 -05:00
|
|
|
redo if defined?(Gem) and Gem.try_activate(lc_path)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2011-01-04 20:13:12 -05:00
|
|
|
def each_localized_path(dir, file)
|
|
|
|
return enum_for(:each_localized_path) unless block_given?
|
2008-12-18 08:09:26 -05:00
|
|
|
each_sublocale do |lc|
|
2011-01-04 20:13:12 -05:00
|
|
|
yield lc.nil? ? File.join(dir, LOCALE_DIR, file) : File.join(dir, LOCALE_DIR, lc, file)
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
|
|
|
|
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
|
2011-01-09 02:39:16 -05:00
|
|
|
if @encoding_name
|
|
|
|
yield "#{@lang}.#{@encoding_name}@#{@modifier}" if @modifier
|
|
|
|
yield "#{@lang}.#{@encoding_name}"
|
|
|
|
end
|
2008-12-18 08:09:26 -05:00
|
|
|
yield "#{@lang}@#{@modifier}" if @modifier
|
|
|
|
yield "#{@lang}"
|
|
|
|
end
|
|
|
|
yield nil
|
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
end
|