Class: IRB::Locale

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

Overview

:nodoc:

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
42
# File 'lib/irb/locale.rb', line 26

def initialize(locale = nil)
  @override_encoding = 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

#langObject (readonly)

Returns the value of attribute lang.



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

def lang
  @lang
end

#modifierObject (readonly)

Returns the value of attribute modifier.



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

def modifier
  @modifier
end

#territoryObject (readonly)

Returns the value of attribute territory.



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

def territory
  @territory
end

Instance Method Details

#encodingObject



46
47
48
# File 'lib/irb/locale.rb', line 46

def encoding
  @override_encoding || @encoding
end

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



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/irb/locale.rb', line 125

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



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

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

#gets(*rs) ⇒ Object



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

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

#load(file, priv = nil) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/irb/locale.rb', line 113

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


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

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

#printf(*opts) ⇒ Object



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

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

#puts(*opts) ⇒ Object



81
82
83
84
# File 'lib/irb/locale.rb', line 81

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

#readline(*rs) ⇒ Object



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

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

#require(file, priv = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/irb/locale.rb', line 86

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



50
51
52
53
54
55
56
57
# File 'lib/irb/locale.rb', line 50

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

#toplevel_loadObject



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

alias toplevel_load load