rails-translator 0.0.1

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.
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
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.
@@ -0,0 +1,54 @@
1
+ = Translator
2
+ {<img src="https://travis-ci.org/hzamani/translator.png" />}[https://travis-ci.org/hzamani/translator]
3
+
4
+ This is a rails plugin to translate models data.
5
+
6
+ == Requirements
7
+
8
+ This plugin uses postgres hstore, so postgres is a must.
9
+
10
+ == How to Install
11
+
12
+ Install the gem
13
+ gem install rails-translator
14
+ add
15
+ gem 'activerecord-postgres-hstore'
16
+ gem 'rails-translator'
17
+ to your Gemfile and run bundler
18
+ bundle install
19
+
20
+ == How to Use
21
+
22
+ First be sure to install postgres hstore extension, by
23
+
24
+ rails g hstore:setup
25
+ rake db:migrate
26
+
27
+ Generate your models but use +hstore+ for translatable string and text fields.
28
+
29
+ rails g model Post title:hstore content:hstore published_at:datatime
30
+
31
+ optionally add hstore_index for hstore columns:
32
+
33
+ # migration file
34
+ add_hstore_index :posts, :title
35
+ add_hstore_index :posts, :content
36
+
37
+ tell which fields do you want to have translations:
38
+
39
+ class Post < ActiveRecord::Base
40
+ translates :title, :content
41
+ # ...
42
+ end
43
+
44
+ now post has locale based attribute accessors:
45
+
46
+ I18n.locale = :en
47
+ p = Post.create title: 'first post', content: 'This is my first post.'
48
+ I18n.locale = :fa
49
+ p.title = ''
50
+
51
+
52
+
53
+
54
+
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Translator'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,4 @@
1
+ require 'translator/translates'
2
+
3
+ module Translator
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :translator do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,90 @@
1
+ module Translator
2
+ class << self
3
+ def fallback_locale () @fallback_locale ||= nil end
4
+ def fallback_locale= (locale) @fallback_locale = locale.to_s end
5
+ def prefixed_locales () @prefixed_locales ||= [] end
6
+ def prefixed_locales=(locales) @prefixed_locales = locales end
7
+
8
+ def prefixed name
9
+ if prefixed_locales.empty?
10
+ name.to_sym
11
+ else
12
+ prefixed_locales.map do |locale|
13
+ "#{locale}_#{name}".to_sym
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ module Translates
20
+ extend ActiveSupport::Concern
21
+
22
+ included do
23
+ def translate name, translations
24
+ data = read_attribute name
25
+ raise ArgumentError.new "translations should be a hash" unless translations.is_a?(Hash)
26
+ data.update Hash[translations.map { |k,v| [k.to_s, v] }]
27
+ write_attribute name, data
28
+ data
29
+ end
30
+
31
+ def translated name, locale
32
+ data = read_attribute name
33
+ if data.is_a? Hash
34
+ data[locale.to_s] || data[Translator.fallback_locale]
35
+ else
36
+ data
37
+ end
38
+ end
39
+ end
40
+
41
+ module ClassMethods
42
+ def translates *args
43
+ self.translated_attributes = args
44
+ self.translated_attributes.each do |name|
45
+ translated_attr_accessor name
46
+ translations_attr_accessor name
47
+ end
48
+ end
49
+
50
+ def translates? name
51
+ self.translated_attributes.include? name
52
+ end
53
+
54
+ def translated_attributes
55
+ @translated_attributes ||= []
56
+ end
57
+
58
+ def prefixed_attributes
59
+ @prefixed_attributes ||= translated_attributes.map{ |a| Translator.prefixed(a) }.flatten
60
+ end
61
+
62
+ protected
63
+
64
+ def translated_attributes= value
65
+ @translated_attributes = value
66
+ end
67
+
68
+ def translated_attr_accessor name
69
+ serialize name, ActiveRecord::Coders::Hstore
70
+
71
+ attr_accessible *prefixed_attributes
72
+
73
+ define_method("#{name}") { |locale=nil| translated name, locale || I18n.locale }
74
+ define_method("#{name}=") { |value| translate name, I18n.locale => value }
75
+
76
+ Translator.prefixed_locales.each do |locale|
77
+ define_method("#{locale}_#{name}") { translated name, locale }
78
+ define_method("#{locale}_#{name}=") { |value| translate name, locale => value }
79
+ end
80
+ end
81
+
82
+ def translations_attr_accessor name
83
+ define_method("#{name}_translations") { read_attribute name }
84
+ define_method("#{name}_translations=") { |value| write_attribute name, value }
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ ActiveRecord::Base.send :include, Translator::Translates
@@ -0,0 +1,3 @@
1
+ module Translator
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-translator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Hassan Zamani
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-02 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: activerecord-postgres-hstore
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: pg
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-rails
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Translator uses postgres hstore to keep model translations in your database.
79
+ email:
80
+ - hsn.zamani@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - lib/translator/translates.rb
86
+ - lib/translator/version.rb
87
+ - lib/tasks/translator_tasks.rake
88
+ - lib/rails-translator.rb
89
+ - MIT-LICENSE
90
+ - Rakefile
91
+ - README.rdoc
92
+ homepage: https://github.com/hzamani/translator
93
+ licenses: []
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 1.8.25
113
+ signing_key:
114
+ specification_version: 3
115
+ summary: Rails plugin for model translation.
116
+ test_files: []