gettext_column_mapping 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +1 -1
- data/VERSION +1 -1
- data/gettext_column_mapping.gemspec +1 -1
- data/init.rb +0 -8
- data/lib/gettext_column_mapping/model_attributes_finder.rb +43 -2
- data/lib/gettext_column_mapping/parent_level/attr_methods.rb +1 -1
- data/lib/gettext_column_mapping/parent_level.rb +16 -10
- data/tasks/gettext_column_mapping.rake +1 -132
- metadata +2 -2
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -73,7 +73,7 @@ All Rails
|
|
73
73
|
my_label: Label
|
74
74
|
|
75
75
|
|
76
|
-
See a working {gettext_column_mapping rails examples}[http://github.com/hallelujah/
|
76
|
+
See a working {gettext_column_mapping rails examples}[http://github.com/hallelujah/gettext_column_mapping_example_rails.git]
|
77
77
|
|
78
78
|
Rails is not mandatory, you can use it in your other project taht satisfies all requirements above !!
|
79
79
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/init.rb
CHANGED
@@ -1,10 +1,2 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
# Include hook code here
|
3
|
-
# HOOK inspired by gettext_i18n_rails
|
4
|
-
begin
|
5
|
-
require 'config/initializers/session_store'
|
6
|
-
rescue LoadError
|
7
|
-
# weird bug, when run with rake rails reports error that session
|
8
|
-
# store is not configured, this fixes it somewhat...
|
9
|
-
end
|
10
2
|
require 'gettext_column_mapping'
|
@@ -4,6 +4,14 @@ module GettextColumnMapping
|
|
4
4
|
#write all found models/columns to a file where GetTexts ruby parser can find them
|
5
5
|
def self.store_model_attributes(options)
|
6
6
|
file = options[:to] || 'data/model_attributes.rb'
|
7
|
+
unless options[:separate_files]
|
8
|
+
write_to_unique_file(file,options)
|
9
|
+
else
|
10
|
+
write_to_separate_files(file,options)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.write_to_unique_file(file,options)
|
7
15
|
File.open(file,'w') do |f|
|
8
16
|
f.puts "# coding: utf-8"
|
9
17
|
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
|
@@ -31,14 +39,47 @@ module GettextColumnMapping
|
|
31
39
|
end
|
32
40
|
end
|
33
41
|
end
|
34
|
-
# For each selected class, batch 200 include parents with key
|
35
|
-
# f.puts(instance.msgid_for_attribute(column))
|
36
42
|
end
|
37
43
|
|
38
44
|
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
|
39
45
|
end
|
40
46
|
end
|
41
47
|
|
48
|
+
def self.write_to_separate_files(dir,options)
|
49
|
+
ModelAttributesFinder.new.find(options).each do |model,column_names|
|
50
|
+
file = File.join(dir,model.name.underscore) + ".rb"
|
51
|
+
FileUtils.mkdir_p(File.dirname(file))
|
52
|
+
File.open(file,'w') do |f|
|
53
|
+
f.puts "# coding: utf-8"
|
54
|
+
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
|
55
|
+
#all columns namespaced under the model
|
56
|
+
column_names.each do |attribute|
|
57
|
+
translation = model.gettext_translation_for_attribute_name(attribute)
|
58
|
+
f.puts("s_('#{translation}')")
|
59
|
+
end
|
60
|
+
|
61
|
+
if GettextColumnMapping.config.use_parent_level
|
62
|
+
# Select all classes with parent level
|
63
|
+
GettextColumnMapping::ParentLevel.item_config(model.name) do |klass_name,columns,parent_association,parent_key,conditions|
|
64
|
+
model = klass_name.constantize
|
65
|
+
options_hash = {}
|
66
|
+
if parent_association
|
67
|
+
options_hash.merge!(:conditions => conditions, :include => parent_association)
|
68
|
+
end
|
69
|
+
model.find_each do |record|
|
70
|
+
columns.each do |column|
|
71
|
+
f.puts("s_('#{record.msgid_for_attribute(column)}')")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
f.puts "#DO NOT MODIFY! AUTOMATICALLY GENERATED FILE!"
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
42
83
|
class ModelAttributesFinder
|
43
84
|
|
44
85
|
def initialize
|
@@ -9,7 +9,7 @@ module GettextColumnMapping
|
|
9
9
|
self.inherited_without_column_mapping_parent_level(subclass)
|
10
10
|
|
11
11
|
parent = GettextColumnMapping::ParentLevel.parent_attributes_translation(subclass.to_s)
|
12
|
-
attributes = GettextColumnMapping::ParentLevel.column_attributes_translation(subclass)
|
12
|
+
attributes = GettextColumnMapping::ParentLevel.column_attributes_translation(subclass.to_s)
|
13
13
|
subclass.gettext_column_mapping_accessor(attributes,parent)
|
14
14
|
end
|
15
15
|
alias_method_chain :inherited, :column_mapping_parent_level
|
@@ -43,20 +43,26 @@ module GettextColumnMapping
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def translate_key_for_column?(klass, column)
|
46
|
-
column_attributes_translation(klass)[column]
|
46
|
+
column_attributes_translation(klass.name)[column]
|
47
47
|
end
|
48
48
|
|
49
49
|
def each_config(&block)
|
50
50
|
@column_attributes_translation.each do |klass_name,columns|
|
51
|
-
|
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)
|
51
|
+
yield(*item_config(klass_name))
|
58
52
|
end
|
53
|
+
end
|
59
54
|
|
55
|
+
def item_config(klass_name)
|
56
|
+
columns = column_attributes_translation(klass_name)
|
57
|
+
parent = parent_attributes_translation(klass_name)
|
58
|
+
if parent
|
59
|
+
parent_key = parent[:key]
|
60
|
+
parent_association = parent[:association]
|
61
|
+
conditions = parent[:conditions]
|
62
|
+
end
|
63
|
+
results = [klass_name,columns,parent_association,parent_key,conditions]
|
64
|
+
yield(*results) if block_given?
|
65
|
+
results
|
60
66
|
end
|
61
67
|
|
62
68
|
def attributes_translation(klass_name)
|
@@ -67,8 +73,8 @@ module GettextColumnMapping
|
|
67
73
|
attributes_translation(klass_name) && attributes_translation(klass_name)[:parent]
|
68
74
|
end
|
69
75
|
|
70
|
-
def column_attributes_translation(
|
71
|
-
@column_attributes_translation[
|
76
|
+
def column_attributes_translation(klass_name)
|
77
|
+
@column_attributes_translation[klass_name] ||= (attributes_translation(klass_name) && attributes_translation(klass_name)[:columns]) || []
|
72
78
|
end
|
73
79
|
|
74
80
|
end
|
@@ -1,132 +1 @@
|
|
1
|
-
require '
|
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
|
1
|
+
require 'gettext_column_mapping/tasks'
|