# frozen_string_literal: false
# irb/locale.rb - internationalization module
# $Release Version: 0.9.6$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
(?<language>[[:alpha:]]{2,3})
(?:_ (?<territory>[[:alpha:]]{2,3}) )?
(?:\. (?<codeset>[^@]+) )?
@@legacy_encoding_alias_map = {}.freeze
def initialize(locale = nil)
@lang = @territory = @encoding_name = @modifier = 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]
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}"]), uplevel: 1)
@encoding = Encoding.find(@encoding_name) rescue nil
@encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
attr_reader :lang, :territory, :modifier
@override_encoding || @encoding
mes.encode(encoding, undef: :replace)
ary = opts.collect{|opt| String(opt)}
ary = opts.collect{|opt| String(opt)}
def require(file, priv = nil)
rex = Regexp.new("lc/#{Regexp.quote(file)}\.(so|o|sl|rb)?")
return false if $".find{|f| f =~ rex}
return ruby_require(file)
unless @@loaded.include?(found)
@@loaded << found # cache
return real_load(found, priv)
raise LoadError, "No such file to load -- #{file}"
def find(file , paths = $:)
base = File.basename(file)
return each_localized_path(dir, base).find{|full_path| File.readable? full_path}
return search_file(paths, dir, base)
def real_load(path, priv)
src = MagicFile.open(path){|f| f.read}
eval("self", TOPLEVEL_BINDING).extend(Module.new {eval(src, nil, path)})
eval(src, TOPLEVEL_BINDING, path)
# @param paths load paths in which IRB find a localized file.
# @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)
redo if defined?(Gem) and Gem.try_activate(lc_path)
def each_localized_path(dir, file)
return enum_for(:each_localized_path) unless block_given?
yield lc.nil? ? File.join(dir, LOCALE_DIR, file) : File.join(dir, LOCALE_DIR, lc, file)
yield "#{@lang}_#{@territory}.#{@encoding_name}@#{@modifier}" if @modifier
yield "#{@lang}_#{@territory}.#{@encoding_name}"
yield "#{@lang}_#{@territory}@#{@modifier}" if @modifier
yield "#{@lang}_#{@territory}"
yield "#{@lang}.#{@encoding_name}@#{@modifier}" if @modifier
yield "#{@lang}.#{@encoding_name}"
yield "#{@lang}@#{@modifier}" if @modifier