gettext_column_mapping 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.gitignore +5 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +102 -0
  4. data/Rakefile +67 -0
  5. data/VERSION +1 -0
  6. data/examples/config/column_mapping.yml +9 -0
  7. data/examples/config/gettext_db_extract.yml +8 -0
  8. data/gettext_column_mapping.gemspec +104 -0
  9. data/init.rb +10 -0
  10. data/install.rb +7 -0
  11. data/lib/gettext_column_mapping/backends/base.rb +78 -0
  12. data/lib/gettext_column_mapping/backends/gettext_activerecord.rb +29 -0
  13. data/lib/gettext_column_mapping/backends/gettext_i18n_rails.rb +34 -0
  14. data/lib/gettext_column_mapping/initializer.rb +64 -0
  15. data/lib/gettext_column_mapping/mapper.rb +53 -0
  16. data/lib/gettext_column_mapping/model_attributes_finder.rb +93 -0
  17. data/lib/gettext_column_mapping/parent_level/attr_methods.rb +45 -0
  18. data/lib/gettext_column_mapping/parent_level.rb +78 -0
  19. data/lib/gettext_column_mapping/parser/yaml.rb +11 -0
  20. data/lib/gettext_column_mapping/parser.rb +38 -0
  21. data/lib/gettext_column_mapping/railtie.rb +21 -0
  22. data/lib/gettext_column_mapping/tasks.rb +147 -0
  23. data/lib/gettext_column_mapping.rb +46 -0
  24. data/tasks/gettext_column_mapping.rake +132 -0
  25. data/test/.gitignore +1 -0
  26. data/test/activerecord_test.rb +43 -0
  27. data/test/config/column_mapping.yml +16 -0
  28. data/test/config/database.yml +9 -0
  29. data/test/config/parent_level_column_mapping.yml +6 -0
  30. data/test/db/fixtures/categories.yml +19 -0
  31. data/test/db/fixtures/rubriques.yml +7 -0
  32. data/test/db/migrate/001_create_utilisateurs.rb +16 -0
  33. data/test/db/migrate/002_create_rubriques.rb +15 -0
  34. data/test/db/migrate/003_create_categories.rb +16 -0
  35. data/test/extend_lib_path.rb +3 -0
  36. data/test/fast_gettext_helper.rb +13 -0
  37. data/test/gettext_column_mapping_test.rb +24 -0
  38. data/test/gettext_helper.rb +10 -0
  39. data/test/helper.rb +39 -0
  40. data/test/log/.gitkeep +0 -0
  41. data/test/mapper_test.rb +42 -0
  42. data/test/models/categorie.rb +3 -0
  43. data/test/models/rubrique.rb +3 -0
  44. data/test/models/utilisateur.rb +4 -0
  45. data/test/po/fr/gettext_column_mapping.po +70 -0
  46. data/test/po/gettext_column_mapping.pot +71 -0
  47. data/test/static/data.rb +15 -0
  48. data/test/static/pluralization.rb +2 -0
  49. data/test/test_helper.rb +17 -0
  50. data/uninstall.rb +2 -0
  51. metadata +128 -0
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.swp
2
+ *~
3
+ test/locale/
4
+ coverage
5
+ rdoc
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [Ramihajamalala hery <hery at rails-royce.org>]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,102 @@
1
+ = gettext_column_mapping
2
+
3
+ If you use mutoh/gettext or grosser/fast_gettext to localize your active_record models, this library is for you.
4
+ This gem only works for now with mysql
5
+
6
+
7
+ == Note on Patches/Pull Requests
8
+
9
+ * Fork the project.
10
+ * Make your feature addition or bug fix.
11
+ * Add tests for it. This is important so I don't break it in a
12
+ future version unintentionally.
13
+ * Commit, do not mess with rakefile, version, or history.
14
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
15
+ * Send me a pull request. Bonus points for topic branches.
16
+
17
+ == Requirements
18
+ Gem requirements :
19
+ * mutoh/gettext_activerecord >= 2.1.0 or grosser/fast_gettext or your own gettext implementation
20
+ * activerecord >= 2.3.0
21
+ * mysql >= 2.7
22
+
23
+ == Features/Problems
24
+
25
+ This library is extracted from another application with its requirements. So it may not fit your project
26
+ It must be refactored and improved.
27
+
28
+ * Better API methods naming ?
29
+ * Add more atomic tests
30
+ * Must accept different output than UTF-8
31
+
32
+ == Example
33
+
34
+ gem install gettext_column_mapping
35
+ gem install fast_gettext gettext_i18n_rails
36
+
37
+
38
+ Rails 2.3.x Usage
39
+
40
+ # config/environment.rb
41
+
42
+ config.gem 'gettext_column_mapping'
43
+
44
+ # config/initializers/gettext.rb
45
+
46
+ GettextColumnMapping::Initializer.run do |config|
47
+ config.config_file = File.expand_path('../../column_mapping.yml', __FILE__)
48
+ end
49
+
50
+ Rails 3 Usage
51
+
52
+ # Gemfile
53
+ gem 'gettext_column_mapping', :source => 'git://github.com/hallelujah/gettext_column_mapping.git', :require => 'gettext_column_mapping/railtie'
54
+
55
+ # config/application.rb
56
+
57
+ config.gettext_column_mapping.config_file = File.expand_path('../column_mapping.yml',__FILE__)
58
+
59
+ All Rails
60
+
61
+ # config/initializers/gettext.rb
62
+
63
+ # Configure FastGettext
64
+ FastGettext.add_text_domain 'app_domain', :path => File.join(RAILS_ROOT,'locale')
65
+ FastGettext.default_available_locales = ['en','fr','es'] #all you want to allow
66
+ FastGettext.default_text_domain = 'app_domain'
67
+
68
+ # config/column_mapping.yml
69
+
70
+ my_namespace/foo:
71
+ class_name: OtherFoo
72
+ column_names:
73
+ my_label: Label
74
+
75
+
76
+ See a working {gettext_column_mapping rails examples}[http://github.com/hallelujah/gettext_column_mapping_rails_example.git]
77
+
78
+ Rails is not mandatory, you can use it in your other project taht satisfies all requirements above !!
79
+
80
+ Just use the GettextColumnMapping::Initializer.run method
81
+
82
+
83
+ == Rake task
84
+
85
+ You can add in your Rakefile to have gettext_column_mapping:.* tasks
86
+
87
+ GettextColumnMapping::Tasks.new("version","text_domain") do |t|
88
+ t.options_store = {
89
+ :po_root => "./po",
90
+ :msgmerge => [:verbose, :no_fuzzy_matching]
91
+ }
92
+ t.locale_path = './locale'
93
+ t.options_finder = {:to => "./static/data.rb", :ignore_tables => "schema_migrations"}
94
+ end
95
+
96
+
97
+ == Maintainer
98
+ Ramihajamalala Hery <hery at rails-royce.org>
99
+
100
+ == Copyright
101
+
102
+ Copyright (c) 2010 Ramihajamalala Hery < hery at rails-royce.org >, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,67 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "gettext_column_mapping"
8
+ gem.summary = %Q{Translate your database columns with gettext}
9
+ gem.description = %Q{Translate your database columns with gettext}
10
+ gem.email = "hery@rails-royce.org"
11
+ gem.homepage = "http://github.com/hallelujah/gettext_column_mapping"
12
+ gem.authors = ["hallelujah"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "gettext_column_mapping #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
53
+
54
+ # Load my tasks
55
+ $:.unshift(File.expand_path('../lib',__FILE__))
56
+ require 'gettext_column_mapping/tasks.rb'
57
+ GettextColumnMapping::Tasks.new("version 0.0.1") do |t|
58
+ test_root = File.expand_path('../test',__FILE__)
59
+ t.test_paths << test_root
60
+ t.require_test_libs = ['fast_gettext_helper','gettext_column_mapping/model_attributes_finder']
61
+ t.require_files = Dir["#{test_root}/models/**/*.rb"].map{|filename| 'models/' + File.basename(filename,'.rb')}
62
+ t.options_store = { :po_root => "#{test_root}/po" , :msgmerge=>['--sort-output'] }
63
+ t.options_finder = {:to => File.join(test_root,'static','data.rb')}
64
+ t.po_pattern = "#{test_root}/{models,static}/**/*.{rb,erb,rjs,rxml}"
65
+ t.locale_path = "#{test_root}/locale"
66
+ t.mo_args = [true,"#{test_root}/po","#{test_root}/locale"]
67
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.2
@@ -0,0 +1,9 @@
1
+ # Columns and table mapping
2
+ # It is only used with gettext and gettext_activerecord.
3
+ # It only maps the human_attribute_name, class_name of your ActiveRecord::Base classes.
4
+ #
5
+ #Example
6
+ #client:
7
+ # class_name: customer
8
+ # column_names:
9
+ # code_client: customer_code
@@ -0,0 +1,8 @@
1
+ #ciblage_attribut:
2
+ # parent:
3
+ # klass: ciblage_famille
4
+ # key: label
5
+ # columns:
6
+ # label
7
+ # conditions: "(ciblage_familles.variable NOT LIKE 'REGION_%' AND ciblage_familles.variable NOT LIKE '%_CLUSTER%' AND ciblage_familles.variable NOT LIKE 'CITY_%' AND ciblage_familles.variable not like '%PROVIDER' AND ciblage_familles.variable NOT LIKE '%_DEPARTEMENT%')"
8
+ #
@@ -0,0 +1,104 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{gettext_column_mapping}
8
+ s.version = "0.2.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["hallelujah"]
12
+ s.date = %q{2010-07-06}
13
+ s.description = %q{Translate your database columns with gettext}
14
+ s.email = %q{hery@rails-royce.org}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "VERSION",
24
+ "examples/config/column_mapping.yml",
25
+ "examples/config/gettext_db_extract.yml",
26
+ "gettext_column_mapping.gemspec",
27
+ "init.rb",
28
+ "install.rb",
29
+ "lib/gettext_column_mapping.rb",
30
+ "lib/gettext_column_mapping/backends/base.rb",
31
+ "lib/gettext_column_mapping/backends/gettext_activerecord.rb",
32
+ "lib/gettext_column_mapping/backends/gettext_i18n_rails.rb",
33
+ "lib/gettext_column_mapping/initializer.rb",
34
+ "lib/gettext_column_mapping/mapper.rb",
35
+ "lib/gettext_column_mapping/model_attributes_finder.rb",
36
+ "lib/gettext_column_mapping/parent_level.rb",
37
+ "lib/gettext_column_mapping/parent_level/attr_methods.rb",
38
+ "lib/gettext_column_mapping/parser.rb",
39
+ "lib/gettext_column_mapping/parser/yaml.rb",
40
+ "lib/gettext_column_mapping/railtie.rb",
41
+ "lib/gettext_column_mapping/tasks.rb",
42
+ "tasks/gettext_column_mapping.rake",
43
+ "test/.gitignore",
44
+ "test/activerecord_test.rb",
45
+ "test/config/column_mapping.yml",
46
+ "test/config/database.yml",
47
+ "test/config/parent_level_column_mapping.yml",
48
+ "test/db/fixtures/categories.yml",
49
+ "test/db/fixtures/rubriques.yml",
50
+ "test/db/migrate/001_create_utilisateurs.rb",
51
+ "test/db/migrate/002_create_rubriques.rb",
52
+ "test/db/migrate/003_create_categories.rb",
53
+ "test/extend_lib_path.rb",
54
+ "test/fast_gettext_helper.rb",
55
+ "test/gettext_column_mapping_test.rb",
56
+ "test/gettext_helper.rb",
57
+ "test/helper.rb",
58
+ "test/log/.gitkeep",
59
+ "test/mapper_test.rb",
60
+ "test/models/categorie.rb",
61
+ "test/models/rubrique.rb",
62
+ "test/models/utilisateur.rb",
63
+ "test/po/fr/gettext_column_mapping.po",
64
+ "test/po/gettext_column_mapping.pot",
65
+ "test/static/data.rb",
66
+ "test/static/pluralization.rb",
67
+ "test/test_helper.rb",
68
+ "uninstall.rb"
69
+ ]
70
+ s.homepage = %q{http://github.com/hallelujah/gettext_column_mapping}
71
+ s.rdoc_options = ["--charset=UTF-8"]
72
+ s.require_paths = ["lib"]
73
+ s.rubygems_version = %q{1.3.7}
74
+ s.summary = %q{Translate your database columns with gettext}
75
+ s.test_files = [
76
+ "test/helper.rb",
77
+ "test/static/data.rb",
78
+ "test/static/pluralization.rb",
79
+ "test/activerecord_test.rb",
80
+ "test/mapper_test.rb",
81
+ "test/fast_gettext_helper.rb",
82
+ "test/gettext_column_mapping_test.rb",
83
+ "test/extend_lib_path.rb",
84
+ "test/gettext_helper.rb",
85
+ "test/db/migrate/001_create_utilisateurs.rb",
86
+ "test/db/migrate/003_create_categories.rb",
87
+ "test/db/migrate/002_create_rubriques.rb",
88
+ "test/models/categorie.rb",
89
+ "test/models/rubrique.rb",
90
+ "test/models/utilisateur.rb",
91
+ "test/test_helper.rb"
92
+ ]
93
+
94
+ if s.respond_to? :specification_version then
95
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
96
+ s.specification_version = 3
97
+
98
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
99
+ else
100
+ end
101
+ else
102
+ end
103
+ end
104
+
data/init.rb ADDED
@@ -0,0 +1,10 @@
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
+ require 'gettext_column_mapping'
data/install.rb ADDED
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+ # Install hook code here
3
+ %{column_mapping.yml gettext_db_extract.yml}.each do |file|
4
+ unless File.exist?(File.join(RAILS_ROOT,'examples/config',file))
5
+ File.copy(File.join(File.dirname(__FILE__),'examples/config',file),File.join(Rails.root,'config'))
6
+ end
7
+ end
@@ -0,0 +1,78 @@
1
+ # coding: utf-8
2
+ module GettextColumnMapping
3
+
4
+ mattr_accessor :activerecord_subclasses
5
+ self.activerecord_subclasses = []
6
+
7
+ module Backends
8
+ module Base
9
+
10
+ def self.included(base)
11
+ base.class_eval do
12
+ class << self
13
+
14
+ def inherited_with_translation(subclass)
15
+ inherited_without_translation(subclass)
16
+ # subclass.attr_accessor_with_column_translation(subclass.translation_column_attributes,subclass.translation_parent_attributes)
17
+ GettextColumnMapping.activerecord_subclasses << subclass
18
+ end
19
+ alias_method_chain :inherited, :translation
20
+
21
+ def untranslate_all? #_with_untranslatable_columns?
22
+ ! translate_class_name?
23
+ end
24
+
25
+ # Checks if the column has to be translated
26
+ def untranslate?(columnname)
27
+ ! translate_key?(columnname)
28
+ end
29
+
30
+ def to_mapping
31
+ Gettext.mapper.class_mapping(self)
32
+ rescue
33
+ name.underscore
34
+ end
35
+
36
+ def gettext_translation_for_attribute_name(attribute_key_name)
37
+ GettextColumnMapping.mapper.column_translation_for_attribute(self,attribute_key_name.to_s)
38
+ end
39
+
40
+ def translate_key?(attr_name)
41
+ GettextColumnMapping.mapper.translate_key?(self,attr_name)
42
+ end
43
+
44
+ def translate_class_name?
45
+ GettextColumnMapping.mapper.translate_class_name?(self)
46
+ end
47
+
48
+ def column_map_attribute(key)
49
+ GettextColumnMapping.mapper.map_attribute(self,key)
50
+ end
51
+
52
+ unless method_defined?(:to_s_with_gettext)
53
+ def to_s_with_gettext
54
+ to_s
55
+ end
56
+ end
57
+
58
+ def to_s_with_gettext_with_translation
59
+ GettextColumnMapping.mapper.to_s_with_gettext(self)
60
+ end
61
+ alias_method_chain :to_s_with_gettext, :translation
62
+
63
+ def human_name_without_translation
64
+ to_s_with_gettext
65
+ end
66
+
67
+ def human_name
68
+ s_(human_name_without_translation)
69
+ end
70
+
71
+
72
+ end # singleton proxy
73
+ end # class_eval
74
+ end # included definition
75
+
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ require 'gettext_activerecord'
3
+ Object.send :include, GetText
4
+ GetText.output_charset = GettextColumnMapping.charset
5
+ module GettextColumnMapping
6
+
7
+ class << self
8
+ delegate :locale, :locale=, :to => GetText
9
+ end
10
+
11
+ module Backends
12
+ module GettextActiverecord
13
+
14
+ def self.included(base)
15
+ base.class_eval do
16
+ class << self
17
+
18
+ def human_attribute_name_with_translation(attribute_key_name)
19
+ GetText.s_(gettext_translation_for_attribute_name(attribute_key_name))
20
+ end
21
+ alias_method_chain :human_attribute_name, :translation
22
+
23
+ end
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ # Weird Hack on ActionController constant error
3
+ module ActionController
4
+ end
5
+ require 'gettext_i18n_rails'
6
+
7
+ module GettextColumnMapping
8
+
9
+ class << self
10
+ delegate :locale, :locale=, :to => FastGettext
11
+ end
12
+
13
+ module Backends
14
+ module GettextI18nRails
15
+
16
+ def self.included(base)
17
+ base.class_eval do
18
+ class << self
19
+
20
+ def human_attribute_name_without_translation(attr)
21
+ human_attribute_name(attr)
22
+ end
23
+
24
+ def human_attribute_name_without_gettext_activerecord(attr)
25
+ human_attribute_name_without_translation(attr)
26
+ end
27
+
28
+ end
29
+ end
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,64 @@
1
+ # coding: utf-8
2
+ require 'yaml'
3
+ require 'gettext_column_mapping/parser'
4
+ module GettextColumnMapping
5
+
6
+ class Initializer
7
+
8
+ class BackendNotLoadedError < LoadError; end
9
+ class ConfigFileNotFoundError < Errno::ENOENT; end
10
+
11
+ cattr_accessor :config
12
+
13
+ def self.run(config = nil,&block)
14
+ self.config = (config || GettextColumnMapping.config)
15
+ yield(self.config) if block_given?
16
+ Parser.init(self.config)
17
+ load_config_file
18
+ require_backend
19
+ require_backend_base
20
+ extend_active_record
21
+ end
22
+
23
+ private
24
+
25
+ class << self
26
+
27
+ def require_backend_base
28
+ require 'gettext_column_mapping/backends/base'
29
+ end
30
+
31
+ def require_backend
32
+
33
+ case config.backend
34
+ when Symbol
35
+ require(lib = "gettext_column_mapping/backends/#{config.backend}")
36
+ config.backend_class = lib.camelize
37
+ when String
38
+ require config.backend
39
+ # config.backend_ext must be set !!
40
+ raise BackendNotLoadedError, "GettextColumnMapping.config.backend_class must be set if you use your own backend" unless config.bakend_class
41
+ else
42
+ raise BackendNotLoadedError, "You must supply a valid backend :gettext_i18n_rails | :gettext_active_record | 'my_librairie/my_backend', please refer to documentation."
43
+ end
44
+
45
+ end
46
+
47
+ def extend_active_record
48
+ ActiveRecord::Base.send(:include,GettextColumnMapping::Backends::Base)
49
+ ActiveRecord::Base.send(:include, config.backend_class.constantize)
50
+ if config.use_parent_level
51
+ require 'gettext_column_mapping/parent_level'
52
+ GettextColumnMapping::ParentLevel.load_config
53
+ end
54
+ end
55
+
56
+ def load_config_file
57
+ raise ConfigFileNotFoundError, "#{config.config_file.inspect}. Please set up config.config_file" unless config.config_file && File.exist?(config.config_file)
58
+ GettextColumnMapping.mapper.mappings = Parser.parse(config.config_file)
59
+ end
60
+
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,53 @@
1
+ # coding: utf-8
2
+ module GettextColumnMapping
3
+
4
+ class Mapper
5
+ attr_accessor :mappings
6
+ delegate :[], :has_key?, :to => :mappings, :allow_nil => true
7
+
8
+
9
+ def class_mapping(obj)
10
+ self[obj.name.underscore][:class_name] || obj.name.underscore
11
+ end
12
+
13
+ def translate_class_name?(obj)
14
+ begin
15
+ ! self[obj.name.underscore][:class_name].blank?
16
+ rescue
17
+ false
18
+ end
19
+ end
20
+
21
+ def map_attribute(obj,key)
22
+ self[obj.name.underscore][:column_names][key]
23
+ end
24
+
25
+ def translate_key?(obj,key)
26
+ begin
27
+ self.has_key?(obj.name.underscore) && ! self[obj.name.underscore][:column_names][key].blank?
28
+ rescue
29
+ false
30
+ end
31
+ end
32
+
33
+ def column_translation_for_attribute(obj,key)
34
+ if translate_key?(obj,key)
35
+ "#{obj.to_s_with_gettext}|#{map_attribute(obj,key)}"
36
+ else
37
+ "#{obj.to_s_with_gettext}|#{key.to_s.humanize}"
38
+ end
39
+ end
40
+
41
+ def to_s_with_gettext(obj)
42
+ array = [GettextColumnMapping.config.model_prefix]
43
+ if translate_class_name?(obj)
44
+ array += self[obj.name.underscore][:class_name].to_s.split('|').collect(&:humanize).collect{|s| s.split(/\s+/).collect(&:humanize).join(' ')}
45
+ else
46
+ array += [obj.to_s_with_gettext_without_translation]
47
+ end
48
+ array.join('|')
49
+ end
50
+
51
+ end
52
+
53
+ end