merb_global 0.0.3.1 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY +4 -0
- data/Rakefile +2 -2
- data/activerecord_generators/translations_migration/templates/translations_migration.erb +4 -2
- data/lib/merb_global.rb +1 -1
- data/lib/merb_global/base.rb +1 -0
- data/lib/merb_global/merbrake.rb +14 -0
- data/lib/merb_global/provider.rb +5 -1
- data/lib/merb_global/providers.rb +16 -6
- data/lib/merb_global/providers/active_record.rb +58 -4
- data/lib/merb_global/providers/data_mapper.rb +66 -7
- data/lib/merb_global/providers/gettext.rb +40 -1
- data/lib/merb_global/providers/gettext.treetop +74 -0
- data/lib/merb_global/providers/mock.rb +3 -1
- data/lib/merb_global/providers/sequel.rb +58 -5
- data/lib/merb_global/providers/yaml.rb +56 -3
- data/lib/merb_global/transfer.rb +79 -0
- data/sequel_generators/translations_migration/templates/translations_migration.erb +6 -3
- metadata +4 -2
data/HISTORY
CHANGED
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'spec/rake/spectask'
|
|
5
5
|
|
6
6
|
PLUGIN = "merb_global"
|
7
7
|
NAME = "merb_global"
|
8
|
-
GEM_VERSION = "0.0.
|
8
|
+
GEM_VERSION = "0.0.4"
|
9
9
|
AUTHORS = ["Alex Coles", "Maciej Piechotka"]
|
10
10
|
EMAIL = "alex@alexcolesportfolio.com"
|
11
11
|
HOMEPAGE = "http://trac.ikonoklastik.com/merb_global/"
|
@@ -42,7 +42,7 @@ end
|
|
42
42
|
|
43
43
|
desc "Install merb_global"
|
44
44
|
task :install => [:package] do
|
45
|
-
sh %{#{SUDO} gem install pkg/#{NAME}-#{
|
45
|
+
sh %{#{SUDO} gem install pkg/#{NAME}-#{GEM_VERSION}}
|
46
46
|
end
|
47
47
|
|
48
48
|
Rake::RDocTask.new do |rd|
|
@@ -5,6 +5,7 @@ class AddTranslationsMigration < ActiveRecord::Migration
|
|
5
5
|
def self.up
|
6
6
|
create_table :merb_global_languages do |t|
|
7
7
|
t.string :name, :limit => 16
|
8
|
+
t.integer :nplural
|
8
9
|
t.string :plural, :size => 128
|
9
10
|
end
|
10
11
|
add_index :merb_global_languages, :name, :unique => true
|
@@ -13,9 +14,10 @@ class AddTranslationsMigration < ActiveRecord::Migration
|
|
13
14
|
:msgid_hash,
|
14
15
|
:msgstr_index] do |t|
|
15
16
|
t.integer :language_id, :null => false
|
16
|
-
t.
|
17
|
+
t.text :msgid, :null => false
|
18
|
+
t.text :msgid_plural
|
17
19
|
t.text :msgstr, :null => false
|
18
|
-
t.integer :msgstr_index, :null =>
|
20
|
+
t.integer :msgstr_index, :null => true
|
19
21
|
end
|
20
22
|
end
|
21
23
|
def self.down
|
data/lib/merb_global.rb
CHANGED
@@ -4,5 +4,5 @@ if defined? Merb::Plugins
|
|
4
4
|
require Pathname(__FILE__).dirname.expand_path + 'merb_global/base'
|
5
5
|
require Pathname(__FILE__).dirname.expand_path + 'merb_global/controller'
|
6
6
|
|
7
|
-
Merb::Plugins.add_rakefiles 'merb_global/
|
7
|
+
Merb::Plugins.add_rakefiles(Pathname(__FILE__).dirname.expand_path + 'merb_global/merbrake')
|
8
8
|
end
|
data/lib/merb_global/base.rb
CHANGED
data/lib/merb_global/merbrake.rb
CHANGED
@@ -7,4 +7,18 @@ namespace :merb_global do
|
|
7
7
|
task :migration => :merb_start do
|
8
8
|
Merb::Global::Providers.provider.create!
|
9
9
|
end
|
10
|
+
desc 'Transfer the translations from one provider to another'
|
11
|
+
task :transfer => :merb_start do
|
12
|
+
from = Merb::Global.config 'source', 'gettext'
|
13
|
+
into = Merb::Global.config 'provider', 'gettext'
|
14
|
+
if from == 'gettext' and into == 'gettext'
|
15
|
+
# Change po into mo files
|
16
|
+
elsif from == into
|
17
|
+
Merb.logger.error 'Tried transfer from and into the same provider'
|
18
|
+
else
|
19
|
+
from = Merb::Global::Providers[from]
|
20
|
+
into = Merb::Global::Providers[into]
|
21
|
+
Merb::Global::Provider.transfer from, into
|
22
|
+
end
|
23
|
+
end
|
10
24
|
end
|
data/lib/merb_global/provider.rb
CHANGED
@@ -3,7 +3,11 @@ module Merb
|
|
3
3
|
# Merb-global is able to store the translations in different types of
|
4
4
|
# storage. An interface betwean merb-global and those storages are
|
5
5
|
# providers.
|
6
|
-
|
6
|
+
#
|
7
|
+
# Please note that it is not required to include this module - despite it
|
8
|
+
# is recomended both as a documentation part and the more customized
|
9
|
+
# error messages.
|
10
|
+
module Provider
|
7
11
|
# call-seq:
|
8
12
|
# translate_to(singular, plural, opts) => translated
|
9
13
|
#
|
@@ -1,12 +1,22 @@
|
|
1
1
|
module Merb
|
2
2
|
module Global
|
3
3
|
module Providers
|
4
|
-
@@
|
5
|
-
|
4
|
+
@@providers = {}
|
5
|
+
##
|
6
|
+
# Creates a provider and/or returns already created one
|
7
|
+
#
|
8
|
+
# ==== Parames
|
9
|
+
# provider<~to_s,~to_sym>:: A name of provider
|
10
|
+
#
|
11
|
+
# ==== Returns
|
12
|
+
# provider<Provider>:: A new provider
|
13
|
+
def self.[](provider)
|
14
|
+
unless @@providers.include? provider.to_sym
|
15
|
+
require 'merb_global/providers/' + provider
|
16
|
+
klass = "Merb::Global::Providers::#{provider.camel_case}"
|
17
|
+
@@providers[provider.to_sym] = eval "#{klass}.new"
|
6
18
|
end
|
7
|
-
|
8
|
-
require 'merb_global/providers/' + provider
|
9
|
-
@@provider = eval "Merb::Global::Providers::#{provider.camel_case}.new"
|
19
|
+
@@providers[provider.to_sym]
|
10
20
|
end
|
11
21
|
|
12
22
|
# call-seq:
|
@@ -35,7 +45,7 @@ module Merb
|
|
35
45
|
# ==== Returns
|
36
46
|
# provider<Provider>:: Returns provider
|
37
47
|
def self.provider
|
38
|
-
@@provider ||=
|
48
|
+
@@provider ||= self[Merb::Global.config(:provider, 'gettext')]
|
39
49
|
end
|
40
50
|
end
|
41
51
|
end
|
@@ -8,13 +8,21 @@ require 'merb_global/plural'
|
|
8
8
|
module Merb
|
9
9
|
module Global
|
10
10
|
module Providers
|
11
|
-
class ActiveRecord
|
11
|
+
class ActiveRecord #:nodoc: all
|
12
|
+
include Merb::Global::Provider
|
13
|
+
include Merb::Global::Provider::Importer
|
14
|
+
include Merb::Global::Provider::Exporter
|
15
|
+
|
12
16
|
def translate_to(singular, plural, opts)
|
13
17
|
language = Language.find :first,
|
14
18
|
:conditions => {:name => opts[:lang]}
|
15
19
|
unless language.nil?
|
16
|
-
|
17
|
-
|
20
|
+
unless plural.nil?
|
21
|
+
n = Plural.which_form opts[:n], language.plural
|
22
|
+
translation = Translation.find [language.id, singular, n]
|
23
|
+
else
|
24
|
+
translation = Translation.find [language.id, singular, nil]
|
25
|
+
end
|
18
26
|
return translation.msgstr
|
19
27
|
end rescue nil
|
20
28
|
return opts[:n] > 1 ? plural : singular # Fallback if not in database
|
@@ -49,13 +57,59 @@ module Merb
|
|
49
57
|
# "(#{except.join(',')})"])
|
50
58
|
end
|
51
59
|
|
60
|
+
def import(exporter, export_data)
|
61
|
+
Language.transaction do
|
62
|
+
Translation.transaction do
|
63
|
+
Language.find(:all).each do |language|
|
64
|
+
exporter.export_language export_data, language.name,
|
65
|
+
language.nplural,
|
66
|
+
language.plural do |lang|
|
67
|
+
language.translations.each do |translation|
|
68
|
+
exporter.export_string lang, translation.msgid,
|
69
|
+
translation.msgid_plural,
|
70
|
+
translation.msgstr_index,
|
71
|
+
translation.msgstr
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def export
|
80
|
+
Language.transaction do
|
81
|
+
Translation.transaction do
|
82
|
+
Language.delete_all
|
83
|
+
Translation.delete_all
|
84
|
+
yield nil
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def export_language(export_data, language, nplural, plural)
|
90
|
+
yield Language.create!(:language => language, :nplural => nplural,
|
91
|
+
:plural => plural).id
|
92
|
+
end
|
93
|
+
|
94
|
+
def export_string(language_id, msgid, msgid_plural,
|
95
|
+
msgstr, msgstr_index)
|
96
|
+
Translation.create! :language_id => language_id,
|
97
|
+
:msgid => msgid,
|
98
|
+
:msgid_plural => msgid_plural,
|
99
|
+
:msgstr => msgstr,
|
100
|
+
:msgstr_index => msgstr_index
|
101
|
+
end
|
102
|
+
|
52
103
|
class Language < ::ActiveRecord::Base
|
53
104
|
set_table_name :merb_global_languages
|
105
|
+
has_many :translations,
|
106
|
+
:class_name =>
|
107
|
+
"::Merb::Global::Providers::ActiveRecord::Translation"
|
54
108
|
end
|
55
109
|
|
56
110
|
class Translation < ::ActiveRecord::Base
|
57
111
|
set_table_name :merb_global_translations
|
58
|
-
set_primary_keys :language_id, :
|
112
|
+
set_primary_keys :language_id, :msgid, :msgstr_index
|
59
113
|
end
|
60
114
|
end
|
61
115
|
end
|
@@ -6,14 +6,24 @@ module Merb
|
|
6
6
|
module Global
|
7
7
|
module Providers
|
8
8
|
class DataMapper #:nodoc: all
|
9
|
+
include Merb::Global::Provider
|
10
|
+
include Merb::Global::Provider::Importer
|
11
|
+
include Merb::Global::Provider::Exporter
|
12
|
+
|
9
13
|
def translate_to(singular, plural, opts)
|
10
14
|
# I hope it's from MemCache
|
11
15
|
language = Language.first :name => opts[:lang]
|
12
16
|
unless language.nil?
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
unless plural.nil?
|
18
|
+
n = Plural.which_form opts[:n], language.plural
|
19
|
+
translation = Translation.first :language_id => language.id,
|
20
|
+
:msgid => singular,
|
21
|
+
:msgstr_index => n
|
22
|
+
else
|
23
|
+
translation = Translation.first :language_id => language.id,
|
24
|
+
:msgid => singular,
|
25
|
+
:msgstr_index => nil
|
26
|
+
end
|
17
27
|
return translation.msgstr unless translation.nil?
|
18
28
|
end
|
19
29
|
# Fallback if not in database
|
@@ -33,14 +43,62 @@ module Merb
|
|
33
43
|
Language.first(:name.not => except).name
|
34
44
|
end
|
35
45
|
|
46
|
+
def import(exporter, export_data)
|
47
|
+
::DataMapper::Transaction.new(Language, Translation) do
|
48
|
+
Language.all.each do |language|
|
49
|
+
exporter.export_language export_data, language.name,
|
50
|
+
language.nplural,
|
51
|
+
language.plural do |lang|
|
52
|
+
language.translations.each do |translation|
|
53
|
+
exporter.export_string lang, translation.msgid,
|
54
|
+
translation.msgid_plural,
|
55
|
+
translation.msgstr_index,
|
56
|
+
translation.msgstr
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def export
|
64
|
+
::DataMapper::Transaction.new(Language, Translation) do
|
65
|
+
Language.all.each {|language| language.destroy}
|
66
|
+
Translation.all.each {|translation| translation.destroy}
|
67
|
+
yield nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def export_language(export_data, language, nplural, plural)
|
72
|
+
lang = Language.new :language => language, :nplural => nplural,
|
73
|
+
:plural => plural
|
74
|
+
lang.save
|
75
|
+
raise if lang.new_record?
|
76
|
+
yield lang.id
|
77
|
+
end
|
78
|
+
|
79
|
+
def export_string(language_id, msgid, msgid_plural,
|
80
|
+
msgstr, msgstr_index)
|
81
|
+
trans = Translation.new :language_id => language_id,
|
82
|
+
:msgid => msgid,
|
83
|
+
:msgid_plural => msgid_plural,
|
84
|
+
:msgstr => msgstr,
|
85
|
+
:msgstr_index => msgstr_index
|
86
|
+
trans.save
|
87
|
+
raise if lang.new_record?
|
88
|
+
end
|
89
|
+
|
36
90
|
# When table structure becomes stable it *should* be documented
|
37
91
|
class Language
|
38
92
|
include ::DataMapper::Resource
|
39
93
|
storage_names[:default] = 'merb_global_languages'
|
40
94
|
property :id, Integer, :serial => true
|
41
95
|
property :name, String, :unique_index => true
|
96
|
+
property :nplural, Integer
|
42
97
|
property :plural, Text, :lazy => false
|
43
98
|
# validates_is_unique :name
|
99
|
+
has n, :translations,
|
100
|
+
:class_name => "Merb::Global::Providers::DataMapper::Translation",
|
101
|
+
:child_key => [:language_id]
|
44
102
|
end
|
45
103
|
|
46
104
|
class Translation
|
@@ -53,10 +111,11 @@ module Merb
|
|
53
111
|
# - it may be wrong optimalisation
|
54
112
|
# As far I'll leave it in this form. If anybody could measure the
|
55
113
|
# speed of both methods it will be appreciate.
|
56
|
-
property :
|
114
|
+
property :msgid, Text, :nullable => false, :key => true
|
115
|
+
property :msgid_plural, Text, :lazy => true
|
57
116
|
property :msgstr, Text, :nullable => false, :lazy => false
|
58
|
-
property :msgstr_index, Integer, :nullable =>
|
59
|
-
|
117
|
+
property :msgstr_index, Integer, :nullable => true, :key => true
|
118
|
+
belongs_to :language, :class_name => Language.name
|
60
119
|
end
|
61
120
|
end
|
62
121
|
end
|
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'gettext'
|
2
|
+
require 'treetop'
|
3
|
+
require 'pathname'
|
2
4
|
|
3
5
|
# I'm not sure if it is the correct way of doing it.
|
4
6
|
# As far it seems to be simpler.
|
@@ -11,7 +13,10 @@ end
|
|
11
13
|
module Merb
|
12
14
|
module Global
|
13
15
|
module Providers
|
14
|
-
class Gettext
|
16
|
+
class Gettext #:nodoc: all
|
17
|
+
include Merb::Global::Provider
|
18
|
+
include Merb::Global::Provider::Importer
|
19
|
+
|
15
20
|
def translate_to(singular, plural, opts)
|
16
21
|
context = Thread.current.gettext_context
|
17
22
|
context.set_locale opts[:lang], true
|
@@ -39,6 +44,40 @@ module Merb
|
|
39
44
|
dir.first
|
40
45
|
end
|
41
46
|
|
47
|
+
def import(exporter, export_data)
|
48
|
+
Treetop.load(Pathname(__FILE__).dirname.expand_path.to_s +
|
49
|
+
'/gettext')
|
50
|
+
parser = Merb::Global::Providers::GetTextParser.new
|
51
|
+
Dir[Merb::Global::Providers.localedir + '/*.po'].each do |file|
|
52
|
+
lang_name = File.basename file, '.po'
|
53
|
+
lang_tree = nil
|
54
|
+
open file do |data|
|
55
|
+
lang_tree = parser.parse data.read
|
56
|
+
end
|
57
|
+
opts = (lang_tree.to_hash)[''].split("\n")
|
58
|
+
plural_line = nil
|
59
|
+
for l in opts
|
60
|
+
if l[0..."Plural-Forms:".length] == "Plural-Forms:"
|
61
|
+
plural_line = l
|
62
|
+
break
|
63
|
+
end
|
64
|
+
end
|
65
|
+
plural_line =
|
66
|
+
plural_line["Plural-Forms:".length...plural_line.length]
|
67
|
+
plural_line = plural_line[0...plural_line.length-1]
|
68
|
+
plural_line = plural_line.gsub(/[[:space:]]/, '').split(/[=;]/, 4)
|
69
|
+
plural_line = Hash[*plural_line]
|
70
|
+
exporter.export_language export_data, lang_name,
|
71
|
+
plural_line['nplurals'].to_i,
|
72
|
+
plural_line['plural'] do |lang_data|
|
73
|
+
lang_tree.visit do |msgid, msgid_plural, msgstr, index|
|
74
|
+
exporter.export_string lang_data, msgid, msgid_plural,
|
75
|
+
index, msgstr
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
42
81
|
class GettextContext
|
43
82
|
include ::GetText
|
44
83
|
bindtextdomain Merb::Global.config([:gettext, :domain], 'merbapp'),
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Merb
|
2
|
+
module Global
|
3
|
+
module Providers
|
4
|
+
grammar GetText
|
5
|
+
rule po_file
|
6
|
+
entry* {
|
7
|
+
def to_hash
|
8
|
+
hash = {}
|
9
|
+
elements.each {|entry| hash.merge! entry.to_hash}
|
10
|
+
hash
|
11
|
+
end
|
12
|
+
def visit &block
|
13
|
+
elements.each {|elem| elem.visit &block}
|
14
|
+
end
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
18
|
+
rule entry
|
19
|
+
whitespaces*
|
20
|
+
"msgid" whitespaces msgid:strings
|
21
|
+
"msgstr" whitespaces msgstr:strings {
|
22
|
+
def to_hash
|
23
|
+
{msgid.to_string => msgstr.to_string}
|
24
|
+
end
|
25
|
+
def visit &block
|
26
|
+
unless msgid.to_string.empty? or msgstr.to_string.empty?
|
27
|
+
yield msgid.to_string, nil, msgstr.to_string, nil
|
28
|
+
end
|
29
|
+
end
|
30
|
+
}
|
31
|
+
/
|
32
|
+
whitespaces*
|
33
|
+
"msgid" whitespaces msgid:strings
|
34
|
+
"msgid_plural" whitespaces msgid_plural:strings
|
35
|
+
msgstrs:("msgstr[" number:[0-9]+ "]" whitespaces strings)+ {
|
36
|
+
def to_hash
|
37
|
+
hash = {:plural => msgid_plural.to_string}
|
38
|
+
msgstrs.elements.each do |msgstr|
|
39
|
+
hash[msgstr.number.text_value.to_i] = msgstr.strings.to_string
|
40
|
+
end
|
41
|
+
{msgid.to_string => hash}
|
42
|
+
end
|
43
|
+
def visit &block
|
44
|
+
msgstrs.elements.each do |msgstr|
|
45
|
+
yield msgid.to_string, msgid_plural.to_string,
|
46
|
+
msgstr.strings.to_string, msgstr.number.text_value.to_i
|
47
|
+
end
|
48
|
+
end
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
rule strings
|
53
|
+
(string whitespaces?)+ {
|
54
|
+
def to_string
|
55
|
+
elements.collect {|element| element.string.to_string}.join
|
56
|
+
end
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
rule string
|
61
|
+
'"' content:((!'"' ('\"' / .))*) '"' {
|
62
|
+
def to_string
|
63
|
+
content.text_value.gsub(/\\n/, "\n")
|
64
|
+
end
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
rule whitespaces
|
69
|
+
(" " / "\t" / "\n" / ('#' (!"\n" .)* "\n") )+
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -4,12 +4,20 @@ require 'merb_global/plural'
|
|
4
4
|
module Merb
|
5
5
|
module Global
|
6
6
|
module Providers
|
7
|
-
class Sequel
|
7
|
+
class Sequel #:nodoc: all
|
8
|
+
include Merb::Global::Provider
|
9
|
+
include Merb::Global::Provider::Importer
|
10
|
+
include Merb::Global::Provider::Exporter
|
11
|
+
|
8
12
|
def translate_to(singular, plural, opts)
|
9
13
|
language = Language[:name => opts[:lang]] # I hope it's from MemCache
|
10
14
|
unless language.nil?
|
11
|
-
|
12
|
-
|
15
|
+
unless plural.nil?
|
16
|
+
n = Plural.which_form opts[:n], language[:plural]
|
17
|
+
translation = Translation[language.pk, singular, n]
|
18
|
+
else
|
19
|
+
translation = Translation[language.pk, singular, nil]
|
20
|
+
end
|
13
21
|
return translation[:msgstr] unless translation.nil?
|
14
22
|
end
|
15
23
|
return opts[:n] > 1 ? plural : singular # Fallback if not in database
|
@@ -32,14 +40,59 @@ module Merb
|
|
32
40
|
end
|
33
41
|
|
34
42
|
def choose(except)
|
35
|
-
Language.filter
|
43
|
+
Language.filter(~{:name => except}).first[:name]
|
44
|
+
end
|
45
|
+
|
46
|
+
def import(exporter, export_data)
|
47
|
+
DB.transaction do
|
48
|
+
Language.each do |language|
|
49
|
+
exporter.export_language export_data, language[:name],
|
50
|
+
language[:nplural],
|
51
|
+
language[:plural] do |lang|
|
52
|
+
language.translations.each do |translation|
|
53
|
+
exporter.export_string lang,
|
54
|
+
translation[:msgid],
|
55
|
+
translation[:msgid_plural],
|
56
|
+
translation[:msgstr_index],
|
57
|
+
translation[:msgstr]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def export
|
65
|
+
DB.transaction do
|
66
|
+
Language.delete_all
|
67
|
+
Translation.delete_all
|
68
|
+
yield nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def export_language(export_data, language, nplural, plural)
|
73
|
+
lang = Language.create :name => language, :nplural => nplural,
|
74
|
+
:plural => plural
|
75
|
+
raise unless lang
|
76
|
+
yield lang
|
77
|
+
end
|
78
|
+
|
79
|
+
def export_string(language_id, msgid, msgid_plural,
|
80
|
+
msgstr, msgstr_index)
|
81
|
+
Translation.create(:language_id => language_id,
|
82
|
+
:msgid => msgid,
|
83
|
+
:msgid_plural => msgid_plural,
|
84
|
+
:msgstr => msgstr,
|
85
|
+
:msgstr_index => msgstr_index) or raise
|
36
86
|
end
|
37
87
|
|
38
88
|
class Language < ::Sequel::Model(:merb_global_languages)
|
89
|
+
has_many :translations,
|
90
|
+
:class => "Merb::Global::Providers::Sequel::Translation",
|
91
|
+
:key => :language_id
|
39
92
|
end
|
40
93
|
|
41
94
|
class Translation < ::Sequel::Model(:merb_global_translations)
|
42
|
-
set_primary_key :language_id, :
|
95
|
+
set_primary_key :language_id, :msgid, :msgstr_index
|
43
96
|
end
|
44
97
|
end
|
45
98
|
end
|
@@ -1,9 +1,14 @@
|
|
1
1
|
require 'yaml'
|
2
|
+
require 'fileutils'
|
2
3
|
|
3
4
|
module Merb
|
4
5
|
module Global
|
5
6
|
module Providers
|
6
|
-
class Yaml
|
7
|
+
class Yaml #:nodoc:
|
8
|
+
include Merb::Global::Provider
|
9
|
+
include Merb::Global::Provider::Importer
|
10
|
+
include Merb::Global::Provider::Exporter
|
11
|
+
|
7
12
|
def initialize
|
8
13
|
# Not synchronized - make GC do it's work (may be not optimal
|
9
14
|
# but I don't think that some problem will occure).
|
@@ -45,8 +50,7 @@ module Merb
|
|
45
50
|
end
|
46
51
|
|
47
52
|
def create!
|
48
|
-
|
49
|
-
File.mkdirs Merb::Global::Providers.localedir
|
53
|
+
FileUtils.mkdir_p Merb::Global::Providers.localedir
|
50
54
|
end
|
51
55
|
|
52
56
|
def choose(except)
|
@@ -55,6 +59,55 @@ module Merb
|
|
55
59
|
dir.reject! {|lang| except.include? lang}
|
56
60
|
dir.first
|
57
61
|
end
|
62
|
+
|
63
|
+
def import(exporter, export_data)
|
64
|
+
Dir[Merb::Global::Providers.localedir + '/*.yaml'].each do |file|
|
65
|
+
lang_name = File.basename file, '.yaml'
|
66
|
+
lang = YAML.load_file file
|
67
|
+
exporter.export_language export_data,
|
68
|
+
lang_name,
|
69
|
+
lang[:nplural],
|
70
|
+
lang[:plural] do |lang_data|
|
71
|
+
lang.each do |msgid, msgstr|
|
72
|
+
if msgid.is_a? String
|
73
|
+
if msgstr.is_a? Hash
|
74
|
+
msgid_plural = msgstr[:plural]
|
75
|
+
msgstr.each do |msgstr_index, msgstr|
|
76
|
+
if msgstr_index.is_a? Fixnum
|
77
|
+
exporter.export_string lang_data, msgid, msgid_plural,
|
78
|
+
msgstr_index, msgstr
|
79
|
+
end
|
80
|
+
end
|
81
|
+
else
|
82
|
+
exporter.export_string lang_data, msgid, nil, nil, msgstr
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def export
|
91
|
+
yield nil
|
92
|
+
end
|
93
|
+
|
94
|
+
def export_language(export_data, language, nplural, plural)
|
95
|
+
lang = {:nplural => nplural, :plural => plural}
|
96
|
+
yield lang
|
97
|
+
file = "#{Merb::Global::Providers.localedir}/#{language}.yaml"
|
98
|
+
open file, 'w+' do |out|
|
99
|
+
YAML.dump lang, out
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def export_string(language, msgid, msgstr, msgstr_index)
|
104
|
+
if no.nil?
|
105
|
+
language[msgid] = msgstr
|
106
|
+
else
|
107
|
+
language[msgid] ||= {}
|
108
|
+
language[msgid][no] = msgstr
|
109
|
+
end
|
110
|
+
end
|
58
111
|
end
|
59
112
|
end
|
60
113
|
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Merb
|
2
|
+
module Global
|
3
|
+
module Provider
|
4
|
+
##
|
5
|
+
# Transfer data from importer into exporter
|
6
|
+
#
|
7
|
+
# ==== Parameters
|
8
|
+
# importer<Importer>:: The provider providing the information
|
9
|
+
# exporter<Exporter>:: The provider receiving the information
|
10
|
+
def self.transfer(importer, exporter)
|
11
|
+
exporter.export do |export_data|
|
12
|
+
importer.import(exporter, export_data)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
##
|
16
|
+
# Importer is a provider through which one can iterate.
|
17
|
+
# Therefore it is possible to import data from this source
|
18
|
+
module Importer
|
19
|
+
##
|
20
|
+
# This method iterates through the data and calles the export method
|
21
|
+
# of exporter
|
22
|
+
#
|
23
|
+
# ==== Parameters
|
24
|
+
# exporter<Exporter>:: Exporter to which it should be exported
|
25
|
+
# export_data:: Data to pass in calles
|
26
|
+
#
|
27
|
+
# ==== Raises
|
28
|
+
# NoMethodError:: Raised by default implementation.
|
29
|
+
# Should not be thrown.
|
30
|
+
def import(exporter, export_data)
|
31
|
+
raise NoMethodError.new('method import has not been implemented')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Some sources are not only read-only but one can write to them.
|
37
|
+
# The provider is exporter if it handles this sort of source.
|
38
|
+
module Exporter
|
39
|
+
##
|
40
|
+
# This method handles all transaction stuff that is needed.
|
41
|
+
# It also should do a initialization and/or cleanup of all resources
|
42
|
+
# needed specificly to the transfer as well as the final
|
43
|
+
# flush of changes.
|
44
|
+
# ==== Yields
|
45
|
+
# exported:: A data needed for transfer
|
46
|
+
def export # Better name needed
|
47
|
+
Merb.logger.error('No transaction has been set by exporter')
|
48
|
+
yield nil
|
49
|
+
end
|
50
|
+
##
|
51
|
+
# This method exports a single message. Please note that the calles
|
52
|
+
# may be not in any particular order.
|
53
|
+
# ==== Parameters
|
54
|
+
# language:: Language data (yielded by Language call)
|
55
|
+
# msgid<String>:: Orginal string
|
56
|
+
# msgid_plural<String>:: Orginal plural string
|
57
|
+
# msgstr<String>:: The translation
|
58
|
+
# msgstr_index<Integer>:: The number of form (nil if only singular)
|
59
|
+
def export_string(language, msgid, msgid_plural, msgstr, msgstr_index)
|
60
|
+
raise NoMethodError.new('method export has not been implemented')
|
61
|
+
end
|
62
|
+
##
|
63
|
+
# This method export an language. It is guaranteed to be called
|
64
|
+
# before any of the messages will be exported.
|
65
|
+
#
|
66
|
+
# ==== Parameters
|
67
|
+
# export_data:: Data given from transfer
|
68
|
+
# language<String>:: Language call
|
69
|
+
# nplural<Integer>:: Number of forms
|
70
|
+
# plural<String>:: Format of plural
|
71
|
+
# ==== Yields
|
72
|
+
# language:: The data about language
|
73
|
+
def export_language(export_data, language, nplural, plural)
|
74
|
+
raise NoMethodError.new('method export has not been implemented')
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -7,13 +7,16 @@ class AddTranslationsMigration < Sequel::Migration
|
|
7
7
|
create_table :merb_global_languages do
|
8
8
|
primary_key :id
|
9
9
|
varchar :name, :size => 16, :unique => true
|
10
|
+
integer :nplural
|
10
11
|
varchar :plural, :size => 128
|
11
12
|
end
|
12
13
|
create_table :merb_global_translations do
|
13
|
-
|
14
|
-
|
14
|
+
primary_key [:language_id, :msgid_hash, :msgstr_index]
|
15
|
+
foreign_key :language_id, :null => false
|
16
|
+
text :msgid, :null => false
|
17
|
+
text :msgid_plural
|
15
18
|
text :msgstr, :null => false
|
16
|
-
integer :msgstr_index, :null =>
|
19
|
+
integer :msgstr_index, :null => true
|
17
20
|
end
|
18
21
|
end
|
19
22
|
def down
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: merb_global
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Coles
|
@@ -10,7 +10,7 @@ autorequire: merb_global
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-06-
|
13
|
+
date: 2008-06-23 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -55,6 +55,7 @@ files:
|
|
55
55
|
- lib/merb_global/plural.treetop
|
56
56
|
- lib/merb_global/config.rb
|
57
57
|
- lib/merb_global/controller.rb
|
58
|
+
- lib/merb_global/transfer.rb
|
58
59
|
- lib/merb_global/base.rb
|
59
60
|
- lib/merb_global/merbrake.rb
|
60
61
|
- lib/merb_global/providers
|
@@ -64,6 +65,7 @@ files:
|
|
64
65
|
- lib/merb_global/providers/data_mapper.rb
|
65
66
|
- lib/merb_global/providers/mock.rb
|
66
67
|
- lib/merb_global/providers/active_record.rb
|
68
|
+
- lib/merb_global/providers/gettext.treetop
|
67
69
|
- lib/merb_global.rb
|
68
70
|
- sequel_generators/translations_migration
|
69
71
|
- sequel_generators/translations_migration/USAGE
|