class ReVIEW::I18n

Constants

ALPHA_L
ALPHA_LW
ALPHA_U
ALPHA_UW
ARABIC_LW
ARABIC_UW
JAPAN
ROMAN_L
ROMAN_U
ROMAN_UW

Attributes

locale[RW]

Public Class Methods

get(word, locale = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 54
def self.get(word, locale = nil)
  @i18n.get(word, locale)
end
i18n(*_args) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 30
def self.i18n(*_args)
  raise NotImplementedError, 'I18n.i18n is obsoleted. Please use I18n.setup(locale, [ymlfile])'
end
locale=(locale) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 38
def self.locale=(locale)
  if @i18n
    @i18n.locale = locale
  else
    I18n.setup(locale)
  end
end
new(locale = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 60
def initialize(locale = nil)
  @locale = locale
  load_default
end
setup(locale = 'ja', ymlfile = 'locale.yml') click to toggle source
# File ../../../../../lib/review/i18n.rb, line 16
def self.setup(locale = 'ja', ymlfile = 'locale.yml')
  @i18n = ReVIEW::I18n.new(locale)

  lfile = nil
  if ymlfile
    lfile = File.expand_path(ymlfile, Dir.pwd)

    # backward compatibility
    raise ReVIEW::ConfigError, 'locale.yaml is obsoleted. Please use locale.yml.' if !File.exist?(lfile) && (ymlfile == 'locale.yml') && File.exist?(File.expand_path('locale.yaml', Dir.pwd))
  end

  @i18n.update_localefile(lfile) if lfile && File.file?(lfile)
end
t(str, args = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 34
def self.t(str, args = nil)
  @i18n.t(str, args)
end
Also aliased as: v, v
update(user_i18n, locale = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 50
def self.update(user_i18n, locale = nil)
  @i18n.update(user_i18n, locale)
end
v(str, args = nil)
Alias for: t

Public Instance Methods

get(word, locale = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 100
def get(word, locale = nil)
  locale ||= @locale
  @store[locale][word]
end
load_default() click to toggle source
# File ../../../../../lib/review/i18n.rb, line 65
def load_default
  load_file(File.expand_path('i18n.yml', File.dirname(__FILE__)))
end
load_file(path) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 69
def load_file(path)
  @store = YAML.load_file(path)
end
t(str, args = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 105
def t(str, args = nil)
  frmt = @store[@locale][str].dup
  frmt.gsub!('%%', '##')

  unless args.is_a?(Array)
    args = args.nil? && frmt !~ /\%/ ? [] : [args]
  end

  percents = frmt.scan(/%[A-Za-z]{1,3}/)
  remove_args = []
  percents.each_with_index do |i, idx|
    case i
    when '%pA'
      frmt.sub!(i, ALPHA_U[args[idx]])
      remove_args << idx
    when '%pa'
      frmt.sub!(i, ALPHA_L[args[idx]])
      remove_args << idx
    when '%pAW'
      frmt.sub!(i, ALPHA_UW[args[idx]])
      remove_args << idx
    when '%paW'
      frmt.sub!(i, ALPHA_LW[args[idx]])
      remove_args << idx
    when '%pR'
      frmt.sub!(i, ROMAN_U[args[idx]])
      remove_args << idx
    when '%pr'
      frmt.sub!(i, ROMAN_L[args[idx]])
      remove_args << idx
    when '%pRW'
      frmt.sub!(i, ROMAN_UW[args[idx]])
      remove_args << idx
    when '%pJ'
      frmt.sub!(i, JAPAN[args[idx]])
      remove_args << idx
    when '%pdW'
      frmt.sub!(i, ARABIC_LW[args[idx]])
      remove_args << idx
    when '%pDW'
      frmt.sub!(i, ARABIC_UW[args[idx]])
      remove_args << idx
    end
  end
  remove_args.reverse_each do |idx|
    args.delete_at idx
  end
  args_matched = (frmt.count('%') <= args.size)
  frmt.gsub!('##', '%%')
  args_matched ? (frmt % args) : frmt
rescue
  str
end
update(user_i18n, locale = nil) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 91
def update(user_i18n, locale = nil)
  locale ||= @locale
  if @store[locale]
    @store[locale].merge!(user_i18n)
  else
    @store[locale] = user_i18n
  end
end
update_localefile(path) click to toggle source
# File ../../../../../lib/review/i18n.rb, line 73
def update_localefile(path)
  user_i18n = YAML.load_file(path)
  locale = user_i18n['locale']
  if locale
    user_i18n.delete('locale')
    if @store[locale]
      @store[locale].merge!(user_i18n)
    else
      @store[locale] = user_i18n
    end
  else
    user_i18n.each do |key, values|
      raise KeyError, "Invalid locale file: #{path}" unless values.is_a? Hash
      @store[key].merge!(values)
    end
  end
end