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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale = nil) ⇒ Locale

Returns a new instance of Locale


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

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}"]
	  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


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

def encoding
  @encoding
end

#langObject (readonly)

Returns the value of attribute lang


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

def lang
  @lang
end

#modifieerObject (readonly)

Returns the value of attribute modifieer


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

def modifieer
  @modifieer
end

#territoryObject (readonly)

Returns the value of attribute territory


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

def territory
  @territory
end

Instance Method Details

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


116
117
118
119
120
121
122
123
124
125
126
# File 'lib/irb/locale.rb', line 116

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


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

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

#gets(*rs) ⇒ Object


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

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

#load(file, priv = nil) ⇒ Object


107
108
109
110
111
112
113
114
# File 'lib/irb/locale.rb', line 107

def load(file, priv=nil)
  found = find(file)
  if found
    return real_load(found, priv)
  else
    raise LoadError, "No such file to load -- #{file}"
  end
end

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

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

#printf(*opts) ⇒ Object


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

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

#puts(*opts) ⇒ Object


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

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

#readline(*rs) ⇒ Object


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

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

#require(file, priv = nil) ⇒ Object


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

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


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

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

#toplevel_loadObject


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

alias toplevel_load load