gettext_column_mapping 0.2.2
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/.gitignore +5 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +102 -0
- data/Rakefile +67 -0
- data/VERSION +1 -0
- data/examples/config/column_mapping.yml +9 -0
- data/examples/config/gettext_db_extract.yml +8 -0
- data/gettext_column_mapping.gemspec +104 -0
- data/init.rb +10 -0
- data/install.rb +7 -0
- data/lib/gettext_column_mapping/backends/base.rb +78 -0
- data/lib/gettext_column_mapping/backends/gettext_activerecord.rb +29 -0
- data/lib/gettext_column_mapping/backends/gettext_i18n_rails.rb +34 -0
- data/lib/gettext_column_mapping/initializer.rb +64 -0
- data/lib/gettext_column_mapping/mapper.rb +53 -0
- data/lib/gettext_column_mapping/model_attributes_finder.rb +93 -0
- data/lib/gettext_column_mapping/parent_level/attr_methods.rb +45 -0
- data/lib/gettext_column_mapping/parent_level.rb +78 -0
- data/lib/gettext_column_mapping/parser/yaml.rb +11 -0
- data/lib/gettext_column_mapping/parser.rb +38 -0
- data/lib/gettext_column_mapping/railtie.rb +21 -0
- data/lib/gettext_column_mapping/tasks.rb +147 -0
- data/lib/gettext_column_mapping.rb +46 -0
- data/tasks/gettext_column_mapping.rake +132 -0
- data/test/.gitignore +1 -0
- data/test/activerecord_test.rb +43 -0
- data/test/config/column_mapping.yml +16 -0
- data/test/config/database.yml +9 -0
- data/test/config/parent_level_column_mapping.yml +6 -0
- data/test/db/fixtures/categories.yml +19 -0
- data/test/db/fixtures/rubriques.yml +7 -0
- data/test/db/migrate/001_create_utilisateurs.rb +16 -0
- data/test/db/migrate/002_create_rubriques.rb +15 -0
- data/test/db/migrate/003_create_categories.rb +16 -0
- data/test/extend_lib_path.rb +3 -0
- data/test/fast_gettext_helper.rb +13 -0
- data/test/gettext_column_mapping_test.rb +24 -0
- data/test/gettext_helper.rb +10 -0
- data/test/helper.rb +39 -0
- data/test/log/.gitkeep +0 -0
- data/test/mapper_test.rb +42 -0
- data/test/models/categorie.rb +3 -0
- data/test/models/rubrique.rb +3 -0
- data/test/models/utilisateur.rb +4 -0
- data/test/po/fr/gettext_column_mapping.po +70 -0
- data/test/po/gettext_column_mapping.pot +71 -0
- data/test/static/data.rb +15 -0
- data/test/static/pluralization.rb +2 -0
- data/test/test_helper.rb +17 -0
- data/uninstall.rb +2 -0
- metadata +128 -0
@@ -0,0 +1,93 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
module GettextColumnMapping
|
4
|
+
#write all found models/columns to a file where GetTexts ruby parser can find them
|
5
|
+
def self.store_model_attributes(options)
|
6
|
+
file = options[:to] || 'data/model_attributes.rb'
|
7
|
+
File.open(file,'w') do |f|
|
8
|
+
f.puts "# coding: utf-8"
|
9
|
+
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
|
10
|
+
ModelAttributesFinder.new.find(options).each do |model,column_names|
|
11
|
+
|
12
|
+
f.puts("s_('#{model.to_s_with_gettext}')") #!Keep in sync with ActiveRecord::Base.human_name
|
13
|
+
|
14
|
+
#all columns namespaced under the model
|
15
|
+
column_names.each do |attribute|
|
16
|
+
translation = model.gettext_translation_for_attribute_name(attribute)
|
17
|
+
f.puts("s_('#{translation}')")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
if GettextColumnMapping.config.use_parent_level
|
21
|
+
# Select all classes with parent level
|
22
|
+
GettextColumnMapping::ParentLevel.each_config do |klass_name,columns,parent_association,parent_key,conditions|
|
23
|
+
model = klass_name.constantize
|
24
|
+
options_hash = {}
|
25
|
+
if parent_association
|
26
|
+
options_hash.merge!(:conditions => conditions, :include => parent_association)
|
27
|
+
end
|
28
|
+
model.find_each do |record|
|
29
|
+
columns.each do |column|
|
30
|
+
f.puts("s_('#{record.msgid_for_attribute(column)}')")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
# For each selected class, batch 200 include parents with key
|
35
|
+
# f.puts(instance.msgid_for_attribute(column))
|
36
|
+
end
|
37
|
+
|
38
|
+
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class ModelAttributesFinder
|
43
|
+
|
44
|
+
def initialize
|
45
|
+
return unless Object.const_defined?(:Rails)
|
46
|
+
# HOOK ... HOOK wow ... berk
|
47
|
+
# Is it safe ?
|
48
|
+
$rails_rake_task = false
|
49
|
+
# Eager load all classes !! In order to load all ActiveRecord::Base
|
50
|
+
|
51
|
+
# Rails 3.x power
|
52
|
+
if Rails::VERSION::MAJOR > 2
|
53
|
+
Rails.application.eager_load!
|
54
|
+
else
|
55
|
+
Rails::Initializer.run(:load_application_classes,Rails.configuration) do |config|
|
56
|
+
config.cache_classes = true
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
# options:
|
62
|
+
# :ignore_tables => ['cars',/_settings$/,...]
|
63
|
+
# :ignore_columns => ['id',/_id$/,...]
|
64
|
+
# current connection ---> {'cars'=>['model_name','type'],...}
|
65
|
+
def find(options)
|
66
|
+
found = Hash.new([])
|
67
|
+
|
68
|
+
GettextColumnMapping.activerecord_subclasses.each do |subclass|
|
69
|
+
next if table_ignored?(subclass,options[:ignore_tables])
|
70
|
+
subclass.columns.each do |column|
|
71
|
+
unless column_ignored?(subclass,column,options[:ignore_columns])
|
72
|
+
found[subclass] += [column.name]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
found
|
78
|
+
end
|
79
|
+
|
80
|
+
def table_ignored?(subclass, patterns)
|
81
|
+
ignored?(subclass.table_name,patterns) || subclass.untranslate_all? || !subclass.table_exists?
|
82
|
+
end
|
83
|
+
|
84
|
+
def column_ignored?(subclass,column,patterns)
|
85
|
+
ignored?(column.name,patterns) || subclass.untranslate?(column.name)
|
86
|
+
end
|
87
|
+
|
88
|
+
def ignored?(name,patterns)
|
89
|
+
return false unless patterns
|
90
|
+
patterns.detect{|p| (p.is_a?(Regexp) && name=~p) || (p.to_s == name.to_s)}
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module GettextColumnMapping
|
2
|
+
module ParentLevel
|
3
|
+
module AttrMethods
|
4
|
+
|
5
|
+
def self.extended(base)
|
6
|
+
base.class_eval do
|
7
|
+
class << self
|
8
|
+
def inherited_with_column_mapping_parent_level(subclass)
|
9
|
+
self.inherited_without_column_mapping_parent_level(subclass)
|
10
|
+
|
11
|
+
parent = GettextColumnMapping::ParentLevel.parent_attributes_translation(subclass.to_s)
|
12
|
+
attributes = GettextColumnMapping::ParentLevel.column_attributes_translation(subclass)
|
13
|
+
subclass.gettext_column_mapping_accessor(attributes,parent)
|
14
|
+
end
|
15
|
+
alias_method_chain :inherited, :column_mapping_parent_level
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def gettext_column_mapping_accessor(method_syms=[],parent=nil)
|
21
|
+
if parent
|
22
|
+
parent_association = parent[:association]
|
23
|
+
parent_key = parent[:key]
|
24
|
+
end
|
25
|
+
|
26
|
+
class_eval(<<-CODE,__FILE__,__LINE__)
|
27
|
+
def msgid_for_attribute(method)
|
28
|
+
parent_record = #{parent_association ? parent_association : 'nil'}
|
29
|
+
GettextColumnMapping::ParentLevel.prefix_method(self,method,parent_record,'#{parent_key}')
|
30
|
+
end
|
31
|
+
CODE
|
32
|
+
|
33
|
+
[method_syms].flatten.each do |method_sym|
|
34
|
+
class_eval(<<-STR,__FILE__,__LINE__)
|
35
|
+
|
36
|
+
def #{method_sym}
|
37
|
+
s_(msgid_for_attribute('#{method_sym}'))
|
38
|
+
end
|
39
|
+
STR
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'gettext_column_mapping/parent_level/attr_methods'
|
2
|
+
module GettextColumnMapping
|
3
|
+
|
4
|
+
module ParentLevel
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def data_prefix
|
9
|
+
GettextColumnMapping.config.data_prefix
|
10
|
+
end
|
11
|
+
|
12
|
+
def locate_parser(mod)
|
13
|
+
case mod
|
14
|
+
when Module
|
15
|
+
mod
|
16
|
+
when Symbol
|
17
|
+
require "gettext_column_mapping/parser/#{mod.to_s}"
|
18
|
+
GettextColumnMapping::Parser.const_get(mod.to_s.classify)
|
19
|
+
else
|
20
|
+
require "gettext_column_mapping/parser/yaml"
|
21
|
+
GettextColumnMapping::Parser::Yaml
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def load_config
|
26
|
+
@column_attributes_translation = {}
|
27
|
+
@attributes_translation = {}
|
28
|
+
mod = GettextColumnMapping.config.parent_level_parser
|
29
|
+
file = GettextColumnMapping.config.parent_level_file
|
30
|
+
@parent_level_config = HashWithIndifferentAccess.new(locate_parser(mod).parse(file))
|
31
|
+
end
|
32
|
+
|
33
|
+
def prefix(klass = nil,column = nil,parent = nil,key = nil)
|
34
|
+
prefixes = [data_prefix]
|
35
|
+
prefixes << "#{klass.to_s_with_gettext}" if klass
|
36
|
+
prefixes << "#{parent[key]}" if parent
|
37
|
+
prefixes << "#{klass.column_map_attribute(column.to_s)}" if column
|
38
|
+
prefixes.join("|")
|
39
|
+
end
|
40
|
+
|
41
|
+
def prefix_method(object,method,parent_record,parent_key)
|
42
|
+
[prefix(object.class,method,parent_record,parent_key), object[method] ].join("|")
|
43
|
+
end
|
44
|
+
|
45
|
+
def translate_key_for_column?(klass, column)
|
46
|
+
column_attributes_translation(klass)[column]
|
47
|
+
end
|
48
|
+
|
49
|
+
def each_config(&block)
|
50
|
+
@column_attributes_translation.each do |klass_name,columns|
|
51
|
+
parent = parent_attributes_translation(klass_name)
|
52
|
+
if parent
|
53
|
+
parent_key = parent[:key]
|
54
|
+
parent_association = parent[:association]
|
55
|
+
conditions = parent[:conditions]
|
56
|
+
end
|
57
|
+
yield(klass_name,columns,parent_association, parent_key,conditions)
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
def attributes_translation(klass_name)
|
63
|
+
@attributes_translation[klass_name] = @parent_level_config[klass_name.underscore]
|
64
|
+
end
|
65
|
+
|
66
|
+
def parent_attributes_translation(klass_name)
|
67
|
+
attributes_translation(klass_name) && attributes_translation(klass_name)[:parent]
|
68
|
+
end
|
69
|
+
|
70
|
+
def column_attributes_translation(klass)
|
71
|
+
@column_attributes_translation[klass.name] ||= (attributes_translation(klass.name) && attributes_translation(klass.name)[:columns]) || []
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
::ActiveRecord::Base.extend GettextColumnMapping::ParentLevel::AttrMethods
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module GettextColumnMapping
|
3
|
+
module Parser
|
4
|
+
|
5
|
+
class << self
|
6
|
+
|
7
|
+
attr_reader :parser
|
8
|
+
|
9
|
+
def init(config)
|
10
|
+
@parser = locate_parser(config.config_parser)
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse(file)
|
14
|
+
results = {}
|
15
|
+
h = @parser.parse(file)
|
16
|
+
h.each do |k,v|
|
17
|
+
results[k.to_s] = HashWithIndifferentAccess.new(v)
|
18
|
+
end if h
|
19
|
+
results
|
20
|
+
end
|
21
|
+
|
22
|
+
def locate_parser(mod)
|
23
|
+
case mod
|
24
|
+
when Module
|
25
|
+
mod
|
26
|
+
when Symbol
|
27
|
+
require "gettext_column_mapping/parser/#{mod.to_s}"
|
28
|
+
GettextColumnMapping::Parser.const_get(mod.to_s.classify)
|
29
|
+
else
|
30
|
+
require "gettext_column_mapping/parser/yaml"
|
31
|
+
GettextColumnMapping::Parser::Yaml
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'rails'
|
3
|
+
require 'gettext_column_mapping'
|
4
|
+
require 'gettext_column_mapping/initializer'
|
5
|
+
|
6
|
+
module GettextColumnMapping
|
7
|
+
class Railtie < Rails::Railtie
|
8
|
+
|
9
|
+
config.gettext_column_mapping = GettextColumnMapping.config
|
10
|
+
|
11
|
+
initializer "gettext_column_mapping.after_initialize" do |app|
|
12
|
+
GettextColumnMapping::Initializer.run(app.config.gettext_column_mapping)
|
13
|
+
end
|
14
|
+
|
15
|
+
rake_tasks do
|
16
|
+
load File.expand_path('../tasks.rb',__FILE__)
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
module GettextColumnMapping
|
3
|
+
class Tasks < Rake::TaskLib
|
4
|
+
|
5
|
+
attr_accessor :text_domain, :lib_paths, :test_paths, :require_test_libs, :require_libs, :require_files, :options_store, :po_pattern, :mo_args, :locale_path, :options_finder
|
6
|
+
attr_reader :version
|
7
|
+
|
8
|
+
def initialize(version, domain = nil, &block)
|
9
|
+
@test_paths = []
|
10
|
+
@require_libs = []
|
11
|
+
@require_test_libs = []
|
12
|
+
@require_files = []
|
13
|
+
@options_store = {}
|
14
|
+
@po_pattern = nil
|
15
|
+
@options_finder = {}
|
16
|
+
@locale_path = 'locale'
|
17
|
+
@version = version
|
18
|
+
@text_domain = domain
|
19
|
+
yield self if block_given?
|
20
|
+
verify_variables
|
21
|
+
define
|
22
|
+
end
|
23
|
+
|
24
|
+
def verify_variables
|
25
|
+
@options_finder = {:to => File.join(locale_path, 'data.rb')}.merge(@options_finder)
|
26
|
+
@options_store = {:po_root => locale_path}.merge(@options_store)
|
27
|
+
@po_pattern ||= "locale/data.rb"
|
28
|
+
end
|
29
|
+
|
30
|
+
def text_domain
|
31
|
+
@text_domain ||= ENV['TEXTDOMAIN'] || "gettext_column_mapping"
|
32
|
+
end
|
33
|
+
|
34
|
+
def load_gettext
|
35
|
+
require 'gettext'
|
36
|
+
require 'gettext/utils'
|
37
|
+
end
|
38
|
+
|
39
|
+
def po_root
|
40
|
+
options_store[:po_root] || locale_path
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def define
|
46
|
+
require_libs.each do |lib|
|
47
|
+
require lib
|
48
|
+
end
|
49
|
+
|
50
|
+
task :environment
|
51
|
+
namespace :gettext_column_mapping do
|
52
|
+
desc "Redo gettext"
|
53
|
+
task :all => [:"gettext_column_mapping:find", :"gettext_column_mapping:updatepo", :"gettext_column_mapping:makemo" ]do
|
54
|
+
end
|
55
|
+
|
56
|
+
desc "Find translation in databases"
|
57
|
+
task :find => [:environment] do
|
58
|
+
test_paths.each do |path|
|
59
|
+
$:.unshift(path)
|
60
|
+
end
|
61
|
+
require_test_libs.each do |lib|
|
62
|
+
require lib
|
63
|
+
end
|
64
|
+
require_files.each do |file_path|
|
65
|
+
require file_path
|
66
|
+
end
|
67
|
+
|
68
|
+
require 'gettext_column_mapping/model_attributes_finder'
|
69
|
+
GettextColumnMapping.store_model_attributes(options_finder)
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
desc "Update po"
|
74
|
+
task :updatepo do
|
75
|
+
|
76
|
+
load_gettext
|
77
|
+
|
78
|
+
if GetText.respond_to? :update_pofiles_org
|
79
|
+
GetText.update_pofiles_org(
|
80
|
+
text_domain(),
|
81
|
+
Dir.glob(po_pattern),
|
82
|
+
version,
|
83
|
+
options_store
|
84
|
+
)
|
85
|
+
else #we are on a version < 2.0
|
86
|
+
puts "install new GetText with gettext:install to gain more features..."
|
87
|
+
#kill ar parser...
|
88
|
+
require 'gettext_activerecord/parser'
|
89
|
+
# Need to use this eval hook since we define a module in a method
|
90
|
+
Object.eval <<-HOOK
|
91
|
+
module GetText
|
92
|
+
module ActiveRecordParser
|
93
|
+
module_function
|
94
|
+
def init(x);end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
HOOK
|
98
|
+
|
99
|
+
#parse files.. (models are simply parsed as ruby files)
|
100
|
+
GetText.update_pofiles(
|
101
|
+
text_domain,
|
102
|
+
Dir.glob(po_pattern),
|
103
|
+
version,
|
104
|
+
options_store[:po_root]
|
105
|
+
)
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
end
|
110
|
+
|
111
|
+
desc "Create mo-files for L10n"
|
112
|
+
task :makemo do
|
113
|
+
load_gettext
|
114
|
+
GetText.create_mofiles(options_store[:verbose], po_root, locale_path)
|
115
|
+
end
|
116
|
+
|
117
|
+
desc "add a new language"
|
118
|
+
task :add_language, [:language] do |_, args|
|
119
|
+
language = args.language || ENV["LANGUAGE"]
|
120
|
+
|
121
|
+
# Let's make some pre-verification of the environment.
|
122
|
+
if language.nil?
|
123
|
+
puts "You need to specify the language to add. Either 'LANGUAGE=eo rake test_lib:gettext:add_languange' or 'rake test_lib:gettext:add_languange[eo]'"
|
124
|
+
next
|
125
|
+
end
|
126
|
+
pot = File.join(po_root, "#{text_domain}.pot")
|
127
|
+
if !File.exists?(pot)
|
128
|
+
puts "You don't have a pot file yet, you probably should run 'rake gettext:find' at least once. Tried '#{pot}'."
|
129
|
+
next
|
130
|
+
end
|
131
|
+
|
132
|
+
# Create the directory for the new language.
|
133
|
+
dir = File.join(po_root, language)
|
134
|
+
puts "Creating directory #{dir}"
|
135
|
+
FileUtils.mkdir_p(dir)
|
136
|
+
|
137
|
+
# Create the po file for the new language.
|
138
|
+
new_po = File.join(po_root, language, "#{text_domain}.po")
|
139
|
+
puts "Initializing #{new_po} from #{pot}."
|
140
|
+
system "msginit --locale=#{language} --input=#{pot} --output=#{new_po}"
|
141
|
+
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
end
|
147
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
# Check the version of active_support to requiring all libs
|
3
|
+
require 'active_support/version'
|
4
|
+
if Gem::Version.new(ActiveSupport::VERSION::STRING) > Gem::Version.new("2")
|
5
|
+
require 'active_support/all'
|
6
|
+
else
|
7
|
+
require 'active_support'
|
8
|
+
end
|
9
|
+
|
10
|
+
# We need active_record
|
11
|
+
require 'active_record'
|
12
|
+
|
13
|
+
|
14
|
+
require 'gettext_column_mapping/mapper'
|
15
|
+
require 'gettext_column_mapping/initializer'
|
16
|
+
|
17
|
+
module GettextColumnMapping
|
18
|
+
mattr_accessor :config
|
19
|
+
mattr_reader :mapper
|
20
|
+
@@mapper = GettextColumnMapping::Mapper.new
|
21
|
+
|
22
|
+
# Configuration default
|
23
|
+
|
24
|
+
self.config = ActiveSupport::OrderedOptions.new
|
25
|
+
self.config.config_file = nil
|
26
|
+
self.config.config_parser = :yaml
|
27
|
+
self.config.backend = :gettext_i18n_rails
|
28
|
+
self.config.backend_class = nil
|
29
|
+
self.config.model_prefix = "Model"
|
30
|
+
self.config.data_prefix = "Data"
|
31
|
+
self.config.charset = :utf8
|
32
|
+
|
33
|
+
# Additional plugins
|
34
|
+
self.config.use_parent_level = false
|
35
|
+
self.config.parent_level_file = nil
|
36
|
+
self.config.parent_level_parser = :yaml
|
37
|
+
|
38
|
+
def self.charset
|
39
|
+
case config.charset.to_s
|
40
|
+
when /utf8|utf-8/i
|
41
|
+
'UTF-8'
|
42
|
+
else
|
43
|
+
'UTF-8'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,132 @@
|
|
1
|
+
require 'activesupport'
|
2
|
+
|
3
|
+
def load_gettext
|
4
|
+
require 'gettext'
|
5
|
+
require 'gettext/utils'
|
6
|
+
require 'gettext_column_mapping' # Include this library in order to unable column mapping and untranslating
|
7
|
+
end
|
8
|
+
|
9
|
+
@application = "aimfar"
|
10
|
+
@version = ENV['VERSION'] || "0.0.1"
|
11
|
+
@application_domain = "Adperf Publisher"
|
12
|
+
|
13
|
+
def directory_for_lang(lang)
|
14
|
+
File.join('po',lang)
|
15
|
+
end
|
16
|
+
|
17
|
+
task :ensure_environment_defined do
|
18
|
+
@po_dir = 'po'
|
19
|
+
@lang = ENV['GLANG']
|
20
|
+
|
21
|
+
if @lang.blank?
|
22
|
+
puts "*** The 'GLANG' environment variable must be precised. E.g. rake gettext:create_lang GLANG=es_ES ***"
|
23
|
+
exit
|
24
|
+
else
|
25
|
+
@lang.split('_').first
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
task :create_directory => :ensure_environment_defined do
|
30
|
+
dir =directory_for_lang(@lang)
|
31
|
+
unless File.exist?(dir)
|
32
|
+
puts "### Creating directory #{dir} ###"
|
33
|
+
FileUtils.mkdir_p(dir)
|
34
|
+
else
|
35
|
+
puts "### Directory #{dir} already exists! Not creating. ###"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def out_file(f)
|
40
|
+
File.join(@po_dir,@lang,File.basename(f,'.pot') + '.po')
|
41
|
+
end
|
42
|
+
|
43
|
+
namespace :gettext do
|
44
|
+
|
45
|
+
desc "Update pot/po files."
|
46
|
+
task :updatepo => [:environment] do
|
47
|
+
load_gettext
|
48
|
+
require 'gettext_i18n_rails/haml_parser'
|
49
|
+
|
50
|
+
|
51
|
+
unless ENV['NO_EXTRACT_DB']
|
52
|
+
Rake::Task['gettext:store_model_columns'].invoke
|
53
|
+
end
|
54
|
+
puts "### Updating po/pot files. ###"
|
55
|
+
FileUtils.rm_f("po/#{@application}.pot")
|
56
|
+
|
57
|
+
if GetText.respond_to? :update_pofiles_org
|
58
|
+
GetText.update_pofiles_org(@application, Dir.glob("{app,lib,bin,data,static_data}/**/*.{builder,rb,erb,rjs}"), "#{@application_domain} #{@version}", :msgmerge => [:no_wrap,:sort_by_file, :no_fuzzy_matching, :previous],:verbose => true)
|
59
|
+
else
|
60
|
+
puts "install new GetText with gettext:install to gain more features..."
|
61
|
+
#kill ar parser...
|
62
|
+
require 'gettext/parser/active_record'
|
63
|
+
module GetText
|
64
|
+
module ActiveRecordParser
|
65
|
+
module_function
|
66
|
+
def init(x);end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
GetText.update_pofiles(@application, Dir.glob("{app,lib,bin,data,static_data}/**/*.{builder,rb,erb,rjs}"), "#{@application_domain} #{@version}")#, :msgmerge => [:no_wrap,:sort_by_file, :no_fuzzy_matching, :previous],:verbose => true)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
desc "Create mo-files"
|
75
|
+
task :makemo => [:environment] do
|
76
|
+
load_gettext
|
77
|
+
puts "### Creating mo-files. ###"
|
78
|
+
GetText.create_mofiles(true, "po", "locale")
|
79
|
+
# GetText.create_mofiles(true, "po", "locale")
|
80
|
+
end
|
81
|
+
|
82
|
+
desc "Create a language file for lang GLANG (fr_FR,en_US etc..)."
|
83
|
+
task :create_lang => :create_directory do
|
84
|
+
|
85
|
+
# LANG=es_ES msginit -i ../site.pot -o site.po
|
86
|
+
pot_files = Dir.glob(File.join(@po_dir,'*.pot'))
|
87
|
+
if pot_files.blank?
|
88
|
+
puts 'Unable to find pot files'
|
89
|
+
exit 1
|
90
|
+
end
|
91
|
+
pot_files.each do |f|
|
92
|
+
out_f = out_file(f)
|
93
|
+
unless File.exist?(out_f) # Prevent from erasing the old .po
|
94
|
+
puts "### Creating #{out_f} ! ###"
|
95
|
+
exec "`msginit -i #{f} -o #{out_f} -l #{@lang} --no-wrap`"
|
96
|
+
else
|
97
|
+
puts "### #{out_f} already present! Not created. ###"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
desc "Extract needed data from DB for gettext"
|
104
|
+
task :extract_db do
|
105
|
+
puts "Extracting data from databases"
|
106
|
+
# don't require this file since it loads all rails framework.
|
107
|
+
# Also use system because exec exits as finished
|
108
|
+
system("ruby utils/extract_data_from_db_for_gettext.rb")
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
# This is just an example
|
113
|
+
# Please be inspired!!! :D
|
114
|
+
|
115
|
+
desc "write the data/model_attributes.rb"
|
116
|
+
task :store_model_columns => [:extract_db,:environment] do # => [ :extract_db] do
|
117
|
+
require 'gettext_column_mapping/model_attributes_finder'
|
118
|
+
FastGettext.silence_errors
|
119
|
+
storage_file = 'data/model_attributes.rb'
|
120
|
+
puts "writing model translations to: #{storage_file}"
|
121
|
+
ignore_tables = [/^sitemap_/, /_versions$/, 'schema_migrations', 'sessions']
|
122
|
+
|
123
|
+
# GettextColumnMapping::ModelAttributesFinder.new
|
124
|
+
# puts ActiveRecord::Base.send(:subclasses).size
|
125
|
+
GettextColumnMapping.store_model_attributes(
|
126
|
+
:to => storage_file,
|
127
|
+
:ignore_columns => ['id', 'type', 'created_at', 'updated_at'],
|
128
|
+
:ignore_tables => ignore_tables
|
129
|
+
)
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|
data/test/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
log/*
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'test_helper'
|
3
|
+
require 'models/utilisateur'
|
4
|
+
require 'models/rubrique'
|
5
|
+
require 'models/categorie'
|
6
|
+
class ActiverecordTest < ActiveSupport::TestCase
|
7
|
+
|
8
|
+
fixtures :categories,:rubriques
|
9
|
+
|
10
|
+
def test_human_attribute_name
|
11
|
+
GettextColumnMapping.locale = 'en'
|
12
|
+
assert_equal 'First name', Utilisateur.human_attribute_name('prenom')
|
13
|
+
assert_equal 'Last name', Utilisateur.human_attribute_name('nom')
|
14
|
+
assert_equal 'Gender', Utilisateur.human_attribute_name('sexe')
|
15
|
+
assert_equal 'Label', Rubrique.human_attribute_name('libelle')
|
16
|
+
assert_equal 'Label', Categorie.human_attribute_name('libelle')
|
17
|
+
GettextColumnMapping.locale = 'fr'
|
18
|
+
assert_equal 'Prénom', Utilisateur.human_attribute_name('prenom')
|
19
|
+
assert_equal 'Nom', Utilisateur.human_attribute_name('nom')
|
20
|
+
assert_equal 'Sexe', Utilisateur.human_attribute_name('sexe')
|
21
|
+
assert_equal 'Libellé', Rubrique.human_attribute_name('libelle')
|
22
|
+
assert_equal 'Libellé', Categorie.human_attribute_name('libelle')
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def test_parent_level
|
27
|
+
cat = categories(:"event-wc")
|
28
|
+
assert_equal 'Data|Model|Category|Event|Label|World Cup', cat.msgid_for_attribute('libelle')
|
29
|
+
GettextColumnMapping.locale = 'en'
|
30
|
+
assert_equal 'World Cup', cat.libelle
|
31
|
+
GettextColumnMapping.locale = 'fr'
|
32
|
+
assert_equal 'Evènement - Coupe du monde', cat.libelle
|
33
|
+
# Sport Football
|
34
|
+
cat = categories(:"sport-foot")
|
35
|
+
assert_equal 'Data|Model|Category|Sport|Label|Football', cat.msgid_for_attribute('libelle')
|
36
|
+
GettextColumnMapping.locale = 'en'
|
37
|
+
assert_equal 'Football', cat.libelle
|
38
|
+
GettextColumnMapping.locale = 'fr'
|
39
|
+
assert_equal 'Sport - Football', cat.libelle
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|