Class: Ramaze::Helper::Localize::Dictionary
- Inherits:
-
Object
- Object
- Ramaze::Helper::Localize::Dictionary
- Defined in:
- lib/ramaze/helper/localize.rb
Instance Attribute Summary (collapse)
-
- (Object) dict
readonly
Returns the value of attribute dict.
Instance Method Summary (collapse)
- - (Object) [](locale)
- - (Object) []=(locale, dict)
-
- (Dictionary) initialize
constructor
A new instance of Dictionary.
- - (Object) load(locale, options = {})
- - (Object) locales
- - (Object) lookup(string, locales)
- - (Object) translate(string, locales, substitute)
Constructor Details
- (Dictionary) initialize
A new instance of Dictionary
38 39 40 |
# File 'lib/ramaze/helper/localize.rb', line 38 def initialize @dict = {} end |
Instance Attribute Details
- (Object) dict (readonly)
Returns the value of attribute dict
36 37 38 |
# File 'lib/ramaze/helper/localize.rb', line 36 def dict @dict end |
Instance Method Details
- (Object) [](locale)
73 74 75 |
# File 'lib/ramaze/helper/localize.rb', line 73 def [](locale) @dict[arg_to_locale(locale)] end |
- (Object) []=(locale, dict)
77 78 79 |
# File 'lib/ramaze/helper/localize.rb', line 77 def []=(locale, dict) @dict[arg_to_locale(locale)] = dict end |
- (Object) load(locale, options = {})
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/ramaze/helper/localize.rb', line 81 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 |
- (Object) locales
69 70 71 |
# File 'lib/ramaze/helper/localize.rb', line 69 def locales @dict.keys end |
- (Object) lookup(string, locales)
59 60 61 62 63 64 65 66 67 |
# 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 |
- (Object) translate(string, locales, substitute)
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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 |