Class: Merb::Global::MessageProviders::DataMapper

Inherits:
Object
  • Object
show all
Includes:
Merb::Global::MessageProviders, Base::Exporter, Base::Importer
Defined in:
lib/merb_global/message_providers/data_mapper.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: Language, Translation

Instance Method Summary collapse

Methods included from Merb::Global::MessageProviders

localedir, provider

Methods included from Providers

included

Instance Method Details

#create!Object



33
34
35
36
# File 'lib/merb_global/message_providers/data_mapper.rb', line 33

def create!
  Language.auto_migrate!
  Translation.auto_migrate!
end

#export(data) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/merb_global/message_providers/data_mapper.rb', line 59

def export(data)
  ::DataMapper::Transaction.new(Language, Translation) do
    Translation.all.each {|translation| translation.destroy}
    Language.all.each {|language| language.destroy}
    data.each do |lang_name, lang|
      lang_obj = Language.create!(:name => lang_name,
                                  :plural => lang[:plural],
                                  :nplural => lang[:nplural])
      lang.each do |msgid, msgstr_hash|
        if msgstr_hash.is_a? Hash
          plural = msgstr_hash[:plural]
          msgstr_hash.each do |msgstr_index, msgstr|
            if msgstr_index.nil? or msgstr_index.is_a? Fixnum
              Translation.create!(:language_id => lang_obj.id,
                                  :msgid => msgid,
                                  :msgid_plural => plural,
                                  :msgstr => msgstr,
                                  :msgstr_index => msgstr_index) or
                                                                  raise
            end
          end
        end
      end
    end
  end
end

#importObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/merb_global/message_providers/data_mapper.rb', line 38

def import
  data = {}
  ::DataMapper::Transaction.new(Language, Translation) do
    Language.all.each do |language|
      data[language.name] = lang_hash = {
        :plural => language.plural,
        :nplural => language.nplural
      }
      language.translations(:fields => Translation.properties.to_a).
               each do |translation|
        lang_hash[translation.msgid] ||= {
          :plural => translation.msgid_plural
        }
        lang_hash[translation.msgid][translation.msgstr_index] =
          translation.msgstr
      end
    end
  end
  data
end

#localize(singular, plural, n, locale) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/merb_global/message_providers/data_mapper.rb', line 13

def localize(singular, plural, n, locale)
  # I hope it's from MemCache
  language = Language.first :name => locale.to_s
  unless language.nil?
    unless plural.nil?
      pn = Plural.which_form n, language.plural
      translation = Translation.first :language_id => language.id,
                                      :msgid => singular,
                                      :msgstr_index => pn
    else
      translation = Translation.first :language_id => language.id,
                                      :msgid => singular,
                                      :msgstr_index => nil
    end
    return translation.msgstr unless translation.nil?
  end
  # Fallback if not in database
  return n != 1 ? plural : singular
end