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
|
|
|
#
|
2012-12-13 00:22:30 -05:00
|
|
|
module IRB # :nodoc:
|
2001-04-30 13:38:21 -04:00
|
|
|
class Locale
|
|
|
|
|
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)
|
2014-08-08 21:36:49 -04:00
|
|
|
@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
|
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
|
|
|
|
2014-07-04 16:05:54 -04:00
|
|
|
attr_reader :lang, :territory, :encoding, :modifier
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
mes.encode(@encoding, undef: :replace)
|
2001-04-30 13:38:21 -04:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
mes
|
2001-04-30 13:38:21 -04:00
|
|
|
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$/
|
2014-08-08 21:36:49 -04:00
|
|
|
begin
|
|
|
|
load(file, priv)
|
|
|
|
$".push file
|
|
|
|
return true
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
when /\.(so|o|sl)$/
|
2014-08-08 21:36:49 -04:00
|
|
|
return super
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2014-08-08 21:36:49 -04:00
|
|
|
load(f = file + ".rb")
|
|
|
|
$".push f #"
|
|
|
|
return true
|
2001-04-30 13:38:21 -04:00
|
|
|
rescue LoadError
|
2014-08-08 21:36:49 -04:00
|
|
|
return ruby_require(file)
|
2001-04-30 13:38:21 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
|
2005-01-16 17:09:29 -05:00
|
|
|
else
|
2014-08-08 21:36:49 -04:00
|
|
|
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.
|
2011-05-15 07:55:52 -04:00
|
|
|
# @param dir directory
|
2011-01-04 20:13:12 -05:00
|
|
|
# @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
|
2014-08-08 21:36:49 -04:00
|
|
|
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
|
2014-08-08 21:36:49 -04:00
|
|
|
yield "#{@lang}@#{@modifier}" if @modifier
|
|
|
|
yield "#{@lang}"
|
2008-12-18 08:09:26 -05:00
|
|
|
end
|
|
|
|
yield nil
|
|
|
|
end
|
2001-04-30 13:38:21 -04:00
|
|
|
end
|
|
|
|
end
|