2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
|
|
|
# irb/locale.rb - internationalization module
|
2009-07-07 15:36:20 +04:00
|
|
|
# $Release Version: 0.9.6$
|
2001-04-30 21:38:21 +04:00
|
|
|
# $Revision$
|
2005-04-13 19:27:09 +04:00
|
|
|
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
|
|
|
# --
|
|
|
|
#
|
2009-03-06 06:56:38 +03:00
|
|
|
#
|
2001-04-30 21:38:21 +04:00
|
|
|
#
|
2012-12-13 09:22:30 +04:00
|
|
|
module IRB # :nodoc:
|
2001-04-30 21:38:21 +04:00
|
|
|
class Locale
|
|
|
|
|
2008-12-18 16:09:26 +03:00
|
|
|
LOCALE_NAME_RE = %r[
|
2011-01-09 10:39:16 +03:00
|
|
|
(?<language>[[:alpha:]]{2,3})
|
|
|
|
(?:_ (?<territory>[[:alpha:]]{2,3}) )?
|
|
|
|
(?:\. (?<codeset>[^@]+) )?
|
|
|
|
(?:@ (?<modifier>.*) )?
|
2008-12-18 16:09:26 +03:00
|
|
|
]x
|
2001-04-30 21:38:21 +04:00
|
|
|
LOCALE_DIR = "/lc/"
|
|
|
|
|
2008-12-18 16:09:26 +03:00
|
|
|
@@legacy_encoding_alias_map = {}.freeze
|
2019-11-25 01:27:15 +03:00
|
|
|
@@loaded = []
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2008-12-18 16:09:26 +03:00
|
|
|
def initialize(locale = nil)
|
2020-01-14 09:40:03 +03:00
|
|
|
@override_encoding = nil
|
2008-12-20 05:01:26 +03:00
|
|
|
@lang = @territory = @encoding_name = @modifier = nil
|
2009-03-06 06:56:38 +03:00
|
|
|
@locale = locale || ENV["IRB_LANG"] || ENV["LC_MESSAGES"] || ENV["LC_ALL"] || ENV["LANG"] || "C"
|
2008-12-18 16:09:26 +03:00
|
|
|
if m = LOCALE_NAME_RE.match(@locale)
|
2014-08-09 05: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]
|
2017-12-12 14:56:25 +03:00
|
|
|
warn(("%s is obsolete. use %s" % ["#{@lang}_#{@territory}.#{@encoding_name}", "#{@lang}_#{@territory}.#{@encoding.name}"]), uplevel: 1)
|
2014-08-09 05:36:49 +04:00
|
|
|
end
|
|
|
|
@encoding = Encoding.find(@encoding_name) rescue nil
|
|
|
|
end
|
2003-07-24 09:18:47 +04:00
|
|
|
end
|
2008-12-18 16:09:26 +03:00
|
|
|
@encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
|
2003-07-24 09:18:47 +04:00
|
|
|
end
|
2008-12-18 16:09:26 +03:00
|
|
|
|
2020-01-14 09:40:03 +03:00
|
|
|
attr_reader :lang, :territory, :modifier
|
|
|
|
|
|
|
|
def encoding
|
|
|
|
@override_encoding || @encoding
|
|
|
|
end
|
2002-11-22 12:14:24 +03:00
|
|
|
|
2001-04-30 21:38:21 +04:00
|
|
|
def String(mes)
|
|
|
|
mes = super(mes)
|
2020-01-14 09:40:03 +03:00
|
|
|
if encoding
|
|
|
|
mes.encode(encoding, undef: :replace)
|
2001-04-30 21:38:21 +04:00
|
|
|
else
|
2014-08-09 05:36:49 +04:00
|
|
|
mes
|
2001-04-30 21: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 19:35:04 +04:00
|
|
|
super(*ary)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def printf(*opts)
|
|
|
|
s = format(*opts)
|
|
|
|
print s
|
|
|
|
end
|
|
|
|
|
|
|
|
def puts(*opts)
|
2006-12-14 12:27:36 +03:00
|
|
|
ary = opts.collect{|opt| String(opt)}
|
2001-06-27 19:35:04 +04:00
|
|
|
super(*ary)
|
2001-04-30 21: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-09 05:36:49 +04:00
|
|
|
begin
|
|
|
|
load(file, priv)
|
|
|
|
$".push file
|
|
|
|
return true
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
when /\.(so|o|sl)$/
|
2014-08-09 05:36:49 +04:00
|
|
|
return super
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2014-08-09 05:36:49 +04:00
|
|
|
load(f = file + ".rb")
|
|
|
|
$".push f #"
|
|
|
|
return true
|
2001-04-30 21:38:21 +04:00
|
|
|
rescue LoadError
|
2014-08-09 05:36:49 +04:00
|
|
|
return ruby_require(file)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
alias toplevel_load load
|
2009-03-06 06:56:38 +03:00
|
|
|
|
2001-04-30 21:38:21 +04:00
|
|
|
def load(file, priv=nil)
|
2011-01-05 04:13:12 +03:00
|
|
|
found = find(file)
|
|
|
|
if found
|
2019-11-25 01:27:15 +03:00
|
|
|
unless @@loaded.include?(found)
|
|
|
|
@@loaded << found # cache
|
|
|
|
return real_load(found, priv)
|
|
|
|
end
|
2011-01-05 04:13:12 +03:00
|
|
|
else
|
|
|
|
raise LoadError, "No such file to load -- #{file}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(file , paths = $:)
|
2001-04-30 21:38:21 +04:00
|
|
|
dir = File.dirname(file)
|
|
|
|
dir = "" if dir == "."
|
|
|
|
base = File.basename(file)
|
|
|
|
|
2011-01-05 04:13:12 +03: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 21:38:21 +04:00
|
|
|
end
|
2009-03-06 06:56:38 +03:00
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
|
2011-01-05 04:13:12 +03:00
|
|
|
private
|
2001-04-30 21:38:21 +04:00
|
|
|
def real_load(path, priv)
|
2008-12-18 16:09:26 +03:00
|
|
|
src = MagicFile.open(path){|f| f.read}
|
2005-01-17 01:09:29 +03:00
|
|
|
if priv
|
2014-08-09 05:36:49 +04:00
|
|
|
eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
|
2005-01-17 01:09:29 +03:00
|
|
|
else
|
2014-08-09 05:36:49 +04:00
|
|
|
eval(src, TOPLEVEL_BINDING, path)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-05 04:13:12 +03:00
|
|
|
# @param paths load paths in which IRB find a localized file.
|
2011-05-15 15:55:52 +04:00
|
|
|
# @param dir directory
|
2011-01-05 04:13:12 +03: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 08:46:50 +03:00
|
|
|
redo if defined?(Gem) and Gem.try_activate(lc_path)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2011-01-05 04:13:12 +03:00
|
|
|
def each_localized_path(dir, file)
|
|
|
|
return enum_for(:each_localized_path) unless block_given?
|
2008-12-18 16:09:26 +03:00
|
|
|
each_sublocale do |lc|
|
2011-01-05 04:13:12 +03:00
|
|
|
yield lc.nil? ? File.join(dir, LOCALE_DIR, file) : File.join(dir, LOCALE_DIR, lc, file)
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|
2008-12-18 16:09:26 +03:00
|
|
|
|
|
|
|
def each_sublocale
|
|
|
|
if @lang
|
2014-08-09 05: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 10:39:16 +03:00
|
|
|
if @encoding_name
|
|
|
|
yield "#{@lang}.#{@encoding_name}@#{@modifier}" if @modifier
|
|
|
|
yield "#{@lang}.#{@encoding_name}"
|
|
|
|
end
|
2014-08-09 05:36:49 +04:00
|
|
|
yield "#{@lang}@#{@modifier}" if @modifier
|
|
|
|
yield "#{@lang}"
|
2008-12-18 16:09:26 +03:00
|
|
|
end
|
|
|
|
yield nil
|
|
|
|
end
|
2001-04-30 21:38:21 +04:00
|
|
|
end
|
|
|
|
end
|