Class: Merb::Global::MessageProviders::ActiveRecord

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

Overview

:nodoc: all

Defined Under Namespace

Classes: Language, Translation

Instance Method Summary collapse

Methods included from Base

transfer

Instance Method Details

#create!Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/merb_global/message_providers/active_record.rb', line 37

def create!
  migration_exists = Dir[File.join(Merb.root, 'schema',
                                   'migrations', '*.rb')].detect do |f|
    f =~ /translations\.rb/
  end
  if migration_exists
    puts "\nThe Translation Migration File already exists\n\n"
  else
    sh %{merb-gen translations_migration}
  end
end

#export(data) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/merb_global/message_providers/active_record.rb', line 71

def export(data)
  Language.transaction do
    Translation.transaction do
      Translation.delete_all
      Language.delete_all
      data.each do |lang_name, lang|
        lang_id = Language.create!(:name => lang_name,
                                   :plural => lang[:plural],
                                   :nplural => lang[:nplural]).id
        lang.each do |msgid, msgstrs|
          if msgid.is_a? String
            plural = msgstrs[:plural]
            msgstrs.each do |index, msgstr|
              if index.nil? or index.is_a? Fixnum
                Translation.create! :language_id => lang_id,
                                    :msgid => msgid,
                                    :msgid_plural => plural,
                                    :msgstr => msgstr,
                                    :msgstr_index => index
              end
            end
          end
        end
      end
    end
  end
end

#importObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/merb_global/message_providers/active_record.rb', line 49

def import
  data = {}
  Language.transaction do
    Translation.transaction do
      Language.find(:all).each do |lang|
        data[lang.name] = lang_hash = {
          :plural => lang.plural,
          :nplural => lang.nplural
        }
        lang.translations.each do |translation|
          lang_hash[translation.msgid] ||= {
            :plural => translation.msgid_plural
          }
          lang_hash[translation.msgid][translation.msgstr_index] =
            translation.msgstr
        end
      end
    end
  end
  data
end

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/merb_global/message_providers/active_record.rb', line 16

def localize(singular, plural, n, locale)
  language = Language.find :first,
                           :conditions => {:name => locale.to_s}
  unless language.nil?
    unless plural.nil?
      pn = Plural.which_form n, language.plural
      translation = Translation.find [language.id, singular, pn]
    else
      # Bug of composite_primary_keys?
      conditions = {
        :language_id  => language.id,
        :msgid => singular,
        :msgstr_index => nil
      }
      translation = Translation.find(:first, conditions)
    end
    return translation.msgstr
  end rescue nil
  return n > 1 ? plural : singular # Fallback if not in database
end