has_translations 1.1.1 → 1.1.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.
- checksums.yaml +4 -4
- data/.travis.yml +8 -3
- data/README.md +57 -37
- data/lib/has_translations/model_additions.rb +2 -3
- data/lib/has_translations/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 72189cc1ab0c12f531b64a98514135a2ba2597d7
|
|
4
|
+
data.tar.gz: 4339a9ba36725b6dd906eac20e3b5dd35036dcda
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 64a21323fc159726b0bc4374e78d963a06b0fd381da3547449d2b4ff08b2c4e88f0335556500b20ce1b8c3c5fdb323db1c74eebfa9c3f836c66a06d1a10895bd
|
|
7
|
+
data.tar.gz: 9e578437d62a26a45b8b359ee35c95ddc4c28d4a0e4c75f1f714df76b7cc0062a7ff52c12b32f1869a3df3d2eebb2f9380545781f2e6df6cd308130f8a4d664f
|
data/.travis.yml
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
rvm:
|
|
2
|
-
- 1.9.3
|
|
3
2
|
- 2.0.0
|
|
4
|
-
- 2.1.
|
|
3
|
+
- 2.1.10
|
|
5
4
|
- 2.2.5
|
|
6
5
|
- 2.3.1
|
|
7
6
|
before_install:
|
|
8
|
-
- "gem install bundler
|
|
7
|
+
- "gem install bundler"
|
|
9
8
|
before_script:
|
|
10
9
|
- "bundle install"
|
|
11
10
|
script: "bundle exec rake test"
|
|
@@ -16,3 +15,9 @@ gemfile:
|
|
|
16
15
|
- gemfiles/4.2.gemfile
|
|
17
16
|
- gemfiles/5.0.gemfile
|
|
18
17
|
cache: bundler
|
|
18
|
+
matrix:
|
|
19
|
+
exclude:
|
|
20
|
+
- rvm: 2.0.0
|
|
21
|
+
gemfile: gemfiles/5.0.gemfile
|
|
22
|
+
- rvm: 2.1.10
|
|
23
|
+
gemfile: gemfiles/5.0.gemfile
|
data/README.md
CHANGED
|
@@ -18,20 +18,26 @@ Plugin support is deprecated in Rails and will be removed soon so this version d
|
|
|
18
18
|
To prevent method shadowing between "translations" class method and "translations" relation in models the class
|
|
19
19
|
method has been renamed has_translations.
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
21
|
+
```ruby
|
|
22
|
+
class Article < ActiveRecord::Base
|
|
23
|
+
translations :title, :text
|
|
24
|
+
end
|
|
25
|
+
```
|
|
24
26
|
|
|
25
27
|
become
|
|
26
28
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
```ruby
|
|
30
|
+
class Article < ActiveRecord::Base
|
|
31
|
+
has_translations :title, :text
|
|
32
|
+
end
|
|
33
|
+
```
|
|
30
34
|
|
|
31
35
|
Installation
|
|
32
36
|
============
|
|
33
37
|
|
|
34
|
-
|
|
38
|
+
```bash
|
|
39
|
+
gem install has_translations
|
|
40
|
+
```
|
|
35
41
|
|
|
36
42
|
Example
|
|
37
43
|
=======
|
|
@@ -40,36 +46,44 @@ For example you have Article model and you want to have title and text to be tra
|
|
|
40
46
|
|
|
41
47
|
Run in command line:
|
|
42
48
|
|
|
43
|
-
|
|
49
|
+
```bash
|
|
50
|
+
rails g translation_for article title:string text:text
|
|
51
|
+
```
|
|
44
52
|
|
|
45
53
|
It will produce ArticleTranslation model and migration.
|
|
46
54
|
|
|
47
55
|
Add to article model `translations :value1, :value2`:
|
|
48
56
|
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
57
|
+
```ruby
|
|
58
|
+
class Article < ActiveRecord::Base
|
|
59
|
+
has_translations :title, :text
|
|
60
|
+
end
|
|
61
|
+
```
|
|
52
62
|
|
|
53
63
|
And that's it. Now you can add your translations using:
|
|
54
64
|
|
|
55
|
-
|
|
65
|
+
```ruby
|
|
66
|
+
article = Article.create
|
|
56
67
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
68
|
+
article.translations.create(:locale => 'en', :title => 'title', :text => 'text') # or ArticleTranslation.create(:article => article, :locale => 'en', :title => 'title', :text => 'text')
|
|
69
|
+
article.translations.create(:locale => 'ru', :title => 'заголовок', :text => 'текст')
|
|
70
|
+
article.reload # reload cached translations association array
|
|
71
|
+
I18n.locale = :en
|
|
72
|
+
article.text # text
|
|
73
|
+
I18n.locale = :ru
|
|
74
|
+
article.title # заголовок
|
|
75
|
+
```
|
|
64
76
|
|
|
65
77
|
You can use text filtering plugins, like acts_as_sanitiled and validations, and anything else that is available to the ActiveRecord:
|
|
66
78
|
|
|
67
|
-
|
|
68
|
-
|
|
79
|
+
```ruby
|
|
80
|
+
class ArticleTranslation < ActiveRecord::Base
|
|
81
|
+
acts_as_sanitiled :title, :text
|
|
69
82
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
83
|
+
validates_presence_of :title, :text
|
|
84
|
+
validates_length_of :title, :maximum => 100
|
|
85
|
+
end
|
|
86
|
+
```
|
|
73
87
|
|
|
74
88
|
Options:
|
|
75
89
|
|
|
@@ -82,32 +96,38 @@ Options:
|
|
|
82
96
|
|
|
83
97
|
It's better to use translations with `accepts_nested_attributes_for`:
|
|
84
98
|
|
|
85
|
-
|
|
99
|
+
```ruby
|
|
100
|
+
accepts_nested_attributes_for :translations
|
|
101
|
+
```
|
|
86
102
|
|
|
87
103
|
To create a form for this you can use `all_translations` method. It's have all
|
|
88
104
|
the locales that you have added using the `I18n.available_locales=` method.
|
|
89
105
|
If translation for one of the locale isn't exists, it will build it with :locale.
|
|
90
106
|
So an example which I used in the production (using `formtastic` gem):
|
|
91
107
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
<% end %>
|
|
108
|
+
```ruby
|
|
109
|
+
<% semantic_form_for [:admin, @article] do |f| %>
|
|
110
|
+
<%= f.error_messages %>
|
|
111
|
+
|
|
112
|
+
<% f.inputs :name => "Basic" do %>
|
|
113
|
+
<% object.all_translations.values.each do |translation| %>
|
|
114
|
+
<% f.semantic_fields_for :translations, translation do |ft| %>
|
|
115
|
+
<%= ft.input :title, :label => "Title #{ft.object.locale.to_s.upcase}" %>
|
|
116
|
+
<%= ft.input :text, :label => "Text #{ft.object.locale.to_s.upcase}" %>
|
|
117
|
+
<%= ft.input :locale, :as => :hidden %>
|
|
103
118
|
<% end %>
|
|
104
119
|
<% end %>
|
|
120
|
+
<% end %>
|
|
121
|
+
<% end %>
|
|
122
|
+
```
|
|
105
123
|
|
|
106
124
|
Sometimes you have validations in the translation model, and if you want to skip
|
|
107
125
|
the translations that you don't want to add to the database, you can use
|
|
108
126
|
`:reject_if` option, which is available for the `accepts_nested_attributes_for`:
|
|
109
127
|
|
|
110
|
-
|
|
128
|
+
```ruby
|
|
129
|
+
accepts_nested_attributes_for :translations, :reject_if => lambda { |attrs| attrs['title'].blank? && attrs['text'].blank? }
|
|
130
|
+
```
|
|
111
131
|
|
|
112
132
|
named_scope `translated(locale)` - with that named_scope you can find only
|
|
113
133
|
those models that is translated only to specific locale. For example if you will
|
|
@@ -4,7 +4,7 @@ module HasTranslations
|
|
|
4
4
|
|
|
5
5
|
module ClassMethods
|
|
6
6
|
def translated(locale)
|
|
7
|
-
where(
|
|
7
|
+
where("#{self.has_translations_options[:translation_class].table_name}.locale = ?", locale).joins(:translations)
|
|
8
8
|
end
|
|
9
9
|
|
|
10
10
|
def has_translations(*attrs)
|
|
@@ -99,8 +99,7 @@ module HasTranslations
|
|
|
99
99
|
end
|
|
100
100
|
|
|
101
101
|
def translation(locale, fallback=has_translations_options[:fallback])
|
|
102
|
-
locale
|
|
103
|
-
find_translation(locale) || (fallback && !translations.blank? ? translations.detect { |t| t.locale == I18n.default_locale.to_s } || translations.first : nil)
|
|
102
|
+
find_translation(locale) || (fallback && !translations.empty? ? find_translation(I18n.default_locale) || translations.first : nil)
|
|
104
103
|
end
|
|
105
104
|
|
|
106
105
|
def all_translations
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: has_translations
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.1.
|
|
4
|
+
version: 1.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Dmitry Polushkin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-08-29 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -151,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
151
151
|
version: '0'
|
|
152
152
|
requirements: []
|
|
153
153
|
rubyforge_project: has_translations
|
|
154
|
-
rubygems_version: 2.
|
|
154
|
+
rubygems_version: 2.5.1
|
|
155
155
|
signing_key:
|
|
156
156
|
specification_version: 4
|
|
157
157
|
summary: Create translations for your ActiveRecord models.
|