class Ramaze::Helper::Localize::Dictionary

Attributes

dict[R]

Public Class Methods

new() click to toggle source
# File lib/ramaze/helper/localize.rb, line 38
def initialize
  @dict = {}
end

Public Instance Methods

[](locale) click to toggle source
# File lib/ramaze/helper/localize.rb, line 73
def [](locale)
  @dict[arg_to_locale(locale)]
end
[]=(locale, dict) click to toggle source
# File lib/ramaze/helper/localize.rb, line 77
def []=(locale, dict)
  @dict[arg_to_locale(locale)] = dict
end
load(locale, options = {}) click to toggle source
# File lib/ramaze/helper/localize.rb, line 81
def load(locale, options = {})
  if file = options.delete(:yaml)
    dict = ::YAML.load_file(file)
  elsif hash = options.delete(:hash)
    dict = hash
  elsif marshal = options.delete(:marshal)
    dict = Marshal.load(File.read(marshal))
  else
    raise ArgumentError, "either :yaml, :marshal, or :hash"
  end

  @dict[arg_to_locale(locale)] = dict
end
locales() click to toggle source
# File lib/ramaze/helper/localize.rb, line 69
def locales
  @dict.keys
end
lookup(string, locales) click to toggle source
# File lib/ramaze/helper/localize.rb, line 59
def lookup(string, locales)
  locales.each do |locale|
    next unless dict = self[locale]
    next unless translated = dict[string]
    return translated
  end

  string
end
translate(string, locales, substitute) click to toggle source
# File lib/ramaze/helper/localize.rb, line 42
def translate(string, locales, substitute)
  target = string.to_s.dup
  locales = locales.flatten.uniq

  if substitute
    substitute.each do |key, value|
      target.gsub!(/\{#{Regexp.escape(key)}\}/, lookup(value, locales))
    end
    return target
  elsif target =~ /\{/
    target.gsub!(/\{([^\}]+)\}/){ lookup($1, locales) }
    return target
  else
    lookup(target, locales)
  end
end

Private Instance Methods

arg_to_locale(arg, raises = true) click to toggle source
# File lib/ramaze/helper/localize.rb, line 97
def arg_to_locale(arg, raises = true)
  if raises and not arg
    raise(ArgumentError, "%p cannot be converted to a Locale" % arg)
  end
  arg.respond_to?(:language) ? arg : ::Locale::Tag.parse(arg.to_s)
end