Class: Merb::Global::MessageProviders::Sequel

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

Overview

:nodoc: all

Defined Under Namespace

Classes: Language, Translation

Instance Method Summary collapse

Methods included from Base

transfer

Instance Method Details

#create!Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/merb_global/message_providers/sequel.rb', line 27

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



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/sequel.rb', line 59

def export(data)
  DB.transaction do
    Language.delete_all
    Translation.delete_all
    data.each do |lang_name, lang|
      lang_obj = Language.create(:name => lang_name,
                                 :plural => lang[:plural],
                                 :nplural => lang[:nplural]) or raise
      lang_id = lang_obj[: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) or raise
            end
          end
        end
      end
    end
  end
end

#importObject



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/sequel.rb', line 39

def import
  data = {}
  DB.transaction do
    Language.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
  data
end

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/merb_global/message_providers/sequel.rb', line 12

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