Class: IRB::Locale

Inherits:
Object show all
Defined in:
lib/irb/locale.rb,
lib/irb/lc/ja/encoding_aliases.rb

Constant Summary collapse

LOCALE_NAME_RE =
%r[
  (?<language>[[:alpha:]]{2,3})
  (?:_  (?<territory>[[:alpha:]]{2,3}) )?
  (?:\. (?<codeset>[^@]+) )?
  (?:@  (?<modifier>.*) )?
]x
LOCALE_DIR =
"/lc/"
@@legacy_encoding_alias_map =
{
  'ujis' => Encoding::EUC_JP,
  'euc' => Encoding::EUC_JP
}.freeze
@@loaded =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = nil) ⇒ Locale

Returns a new instance of Locale.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/irb/locale.rb', line 26

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]

    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}"]), uplevel: 1)
      end
      @encoding = Encoding.find(@encoding_name) rescue nil
    end
  end
  @encoding ||= (Encoding.find('locale') rescue Encoding::ASCII_8BIT)
end

Instance Attribute Details

#encodingObject (readonly)

Returns the value of attribute encoding


43
44
45
# File 'lib/irb/locale.rb', line 43

def encoding
  @encoding
end

#langObject (readonly)

Returns the value of attribute lang


43
44
45
# File 'lib/irb/locale.rb', line 43

def lang
  @lang
end

#modifierObject (readonly)

Returns the value of attribute modifier


43
44
45
# File 'lib/irb/locale.rb', line 43

def modifier
  @modifier
end

#territoryObject (readonly)

Returns the value of attribute territory


43
44
45
# File 'lib/irb/locale.rb', line 43

def territory
  @territory
end

Instance Method Details

#find(file, paths = $:) ⇒ Object


120
121
122
123
124
125
126
127
128
129
130
# File 'lib/irb/locale.rb', line 120

def find(file , paths = $:)
  dir = File.dirname(file)
  dir = "" if dir == "."
  base = File.basename(file)

  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
end

#format(*opts) ⇒ Object


54
55
56
# File 'lib/irb/locale.rb', line 54

def format(*opts)
  String(super(*opts))
end

#gets(*rs) ⇒ Object


58
59
60
# File 'lib/irb/locale.rb', line 58

def gets(*rs)
  String(super(*rs))
end

#load(file, priv = nil) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/irb/locale.rb', line 108

def load(file, priv=nil)
  found = find(file)
  if found
    unless @@loaded.include?(found)
      @@loaded << found # cache
      return real_load(found, priv)
    end
  else
    raise LoadError, "No such file to load -- #{file}"
  end
end

66
67
68
69
# File 'lib/irb/locale.rb', line 66

def print(*opts)
  ary = opts.collect{|opt| String(opt)}
  super(*ary)
end

#printf(*opts) ⇒ Object


71
72
73
74
# File 'lib/irb/locale.rb', line 71

def printf(*opts)
  s = format(*opts)
  print s
end

#puts(*opts) ⇒ Object


76
77
78
79
# File 'lib/irb/locale.rb', line 76

def puts(*opts)
  ary = opts.collect{|opt| String(opt)}
  super(*ary)
end

#readline(*rs) ⇒ Object


62
63
64
# File 'lib/irb/locale.rb', line 62

def readline(*rs)
  String(super(*rs))
end

#require(file, priv = nil) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/irb/locale.rb', line 81

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

#String(mes) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/irb/locale.rb', line 45

def String(mes)
  mes = super(mes)
  if @encoding
    mes.encode(@encoding, undef: :replace)
  else
    mes
  end
end

#toplevel_loadObject


106
# File 'lib/irb/locale.rb', line 106

alias toplevel_load load