Class: Ramaze::Helper::Localize::Dictionary
- Defined in:
- lib/ramaze/helper/localize.rb
Instance Attribute Summary collapse
-
#dict ⇒ Object
readonly
Returns the value of attribute dict.
Instance Method Summary collapse
- #[](locale) ⇒ Object
- #[]=(locale, dict) ⇒ Object
-
#initialize ⇒ Dictionary
constructor
A new instance of Dictionary.
- #load(locale, options = {}) ⇒ Object
- #locales ⇒ Object
- #lookup(string, locales) ⇒ Object
- #translate(string, locales, substitute) ⇒ Object
Constructor Details
#initialize ⇒ Dictionary
Returns a new instance of Dictionary.
28 29 30 |
# File 'lib/ramaze/helper/localize.rb', line 28 def initialize @dict = {} end |
Instance Attribute Details
#dict ⇒ Object (readonly)
Returns the value of attribute dict.
26 27 28 |
# File 'lib/ramaze/helper/localize.rb', line 26 def dict @dict end |
Instance Method Details
#[](locale) ⇒ Object
63 64 65 |
# File 'lib/ramaze/helper/localize.rb', line 63 def [](locale) @dict[arg_to_locale(locale)] end |
#[]=(locale, dict) ⇒ Object
67 68 69 |
# File 'lib/ramaze/helper/localize.rb', line 67 def []=(locale, dict) @dict[arg_to_locale(locale)] = dict end |
#load(locale, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ramaze/helper/localize.rb', line 71 def load(locale, = {}) if file = .delete(:yaml) dict = ::YAML.load_file(file) elsif hash = .delete(:hash) dict = hash elsif marshal = .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 ⇒ Object
59 60 61 |
# File 'lib/ramaze/helper/localize.rb', line 59 def locales @dict.keys end |
#lookup(string, locales) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/ramaze/helper/localize.rb', line 49 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) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/ramaze/helper/localize.rb', line 32 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 |