Class: Merb::Global::MessageProviders::Yaml

Inherits:
Object
  • Object
show all
Includes:
Base, Base::Exporter, Base::Importer
Defined in:
lib/merb_global/message_providers/yaml.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Base

transfer

Constructor Details

#initializeYaml

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



39
40
41
# File 'lib/merb_global/message_providers/yaml.rb', line 39

def create!
  FileUtils.mkdir_p Merb::Global::MessageProviders.localedir
end

#export(data) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/merb_global/message_providers/yaml.rb', line 58

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

#importObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/merb_global/message_providers/yaml.rb', line 43

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
# File 'lib/merb_global/message_providers/yaml.rb', line 19

def localize(singular, plural, n, locale)
  lang = get_lang
      
  # Extracted yaml_data from this method to support looking through several yaml files.
  lang[locale] = yaml_data(locale) unless lang.include?(locale)
    
  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

#yaml_data(locale) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/merb_global/message_providers/yaml.rb', line 77

def yaml_data(locale)
  files_to_try = ["#{locale}.yaml", "#{locale.language}.yaml"]
  files_to_try.each do |filename|
    file = File.join(Merb::Global::MessageProviders.localedir, filename)
    return YAML.load_file(file) if File.exist?(file)
  end
  nil
end