Class: Merb::Global::MessageProviders::Yaml
- Inherits:
-
Object
- Object
- Merb::Global::MessageProviders::Yaml
- Includes:
- Base, Base::Exporter, Base::Importer
- Defined in:
- lib/merb_global/message_providers/yaml.rb
Overview
:nodoc:
Instance Method Summary collapse
- #create! ⇒ Object
- #export(data) ⇒ Object
- #import ⇒ Object
-
#initialize ⇒ Yaml
constructor
A new instance of Yaml.
- #localize(singular, plural, n, locale) ⇒ Object
Methods included from Base
Constructor Details
#initialize ⇒ Yaml
Returns a new instance of Yaml.
12 13 14 15 16 17 |
# File 'lib/merb_global/message_providers/yaml.rb', line 12 def initialize # Not synchronized - make GC do it's work (may be not optimal # but I don't think that some problem will occure). # Shouldn't it be sort of cache with some expiration limit? @lang = Hash.new end |
Instance Method Details
#create! ⇒ Object
51 52 53 |
# File 'lib/merb_global/message_providers/yaml.rb', line 51 def create! FileUtils.mkdir_p Merb::Global::MessageProviders.localedir end |
#export(data) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/merb_global/message_providers/yaml.rb', line 70 def export(data) File.unlink *Dir[Merb::Global::MessageProviders.localedir + '/*.yaml'] data.each do |lang_name, lang_orig| lang = {} lang_orig.each do |msgid, msgstr_hash| lang[msgid] = {} msgstr_hash.each do |msgstr_index, msgstr| if msgstr_index.nil? lang[msgid] = msgstr else lang[msgid][msgstr_index] = msgstr end end end YAML.dump File.join(Merb::Global::MessageProviders.localedir, lang_name + '.yaml') end end |
#import ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/merb_global/message_providers/yaml.rb', line 55 def import data = {} Dir[Merb::Global::MessageProviders.localedir + '/*.yaml'].each do |file| lang_name = File.basename file, '.yaml' data[lang_name] = lang = YAML.load_file(file) lang.each do |msgid, msgstr| if msgstr.is_a? String and msgid.is_a? String lang[msgid] = {nil => msgstr, :plural => nil} end end end data end |
#localize(singular, plural, n, locale) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/merb_global/message_providers/yaml.rb', line 19 def localize(singular, plural, n, locale) unless Merb.environment == "development" lang = @lang else lang = {} end unless lang.include? locale file = File.join Merb::Global::MessageProviders.localedir, locale.to_s + '.yaml' if File.exist? file lang[locale] = YAML.load_file file else # TODO: Check if it not opens security risk lang[locale] = nil end end unless lang[locale].nil? lang = lang[locale] unless lang[singular].nil? unless plural.nil? n = Merb::Global::Plural.which_form n, lang[:plural] return lang[singular][n] unless lang[singular][n].nil? else return lang[singular] unless lang[singular].nil? end end end return n > 1 ? plural : singular end |