lost_in_translations 1.2.0 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +1 -0
- data/.travis.yml +0 -1
- data/Gemfile.lock +4 -10
- data/README.md +19 -11
- data/Rakefile +10 -0
- data/lib/lost_in_translations/active_record.rb +1 -1
- data/lib/lost_in_translations/ruby.rb +3 -1
- data/lib/lost_in_translations/version.rb +1 -1
- data/lib/lost_in_translations.rb +19 -4
- data/lost_in_translations.gemspec +2 -2
- data/spec/lost_in_translations/lost_in_translations_spec.rb +46 -0
- data/spec/spec_helper.rb +1 -1
- data/spec/support/db/test.sqlite3 +0 -0
- metadata +20 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29ebb7845e6d024febce53bff8436ac9797c75b9
|
4
|
+
data.tar.gz: aaffaef479abb13f14eb6c84de725c28f4af50c4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 657717a980be03b4eafadf654520d5aa464f2c39690f6fa055a15538978c29dbc341d340daf5b002a42977c42affdeae7b65b431752cd1d2e9655d2b4a32c171
|
7
|
+
data.tar.gz: a437cb00356bc9b232f17cc4c1fc81ddd326c7b6674fe41a07ad7111482864d858c9d47d7e040c1d89a7b4d523d365adc3bba8e91231ea5a6c194a94e2d4ac88
|
data/.rubocop.yml
CHANGED
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lost_in_translations (1.
|
4
|
+
lost_in_translations (1.3.0)
|
5
5
|
i18n (~> 0.7)
|
6
6
|
|
7
7
|
GEM
|
@@ -23,7 +23,6 @@ GEM
|
|
23
23
|
arel (6.0.3)
|
24
24
|
ast (2.2.0)
|
25
25
|
builder (3.2.2)
|
26
|
-
byebug (8.2.5)
|
27
26
|
codeclimate-test-reporter (0.4.8)
|
28
27
|
simplecov (>= 0.7.1, < 1.0.0)
|
29
28
|
coderay (1.1.1)
|
@@ -40,10 +39,8 @@ GEM
|
|
40
39
|
coderay (~> 1.1.0)
|
41
40
|
method_source (~> 0.8.1)
|
42
41
|
slop (~> 3.4)
|
43
|
-
pry-byebug (3.3.0)
|
44
|
-
byebug (~> 8.0)
|
45
|
-
pry (~> 0.10)
|
46
42
|
rainbow (2.1.0)
|
43
|
+
rake (11.2.2)
|
47
44
|
rspec (3.4.0)
|
48
45
|
rspec-core (~> 3.4.0)
|
49
46
|
rspec-expectations (~> 3.4.0)
|
@@ -81,17 +78,14 @@ PLATFORMS
|
|
81
78
|
|
82
79
|
DEPENDENCIES
|
83
80
|
activerecord (= 4.2.6)
|
84
|
-
byebug (= 8.2.5)
|
85
81
|
codeclimate-test-reporter (= 0.4.8)
|
86
82
|
lost_in_translations!
|
87
|
-
pry
|
83
|
+
pry (= 0.10.3)
|
84
|
+
rake (= 11.2.2)
|
88
85
|
rspec (= 3.4.0)
|
89
86
|
rubocop (= 0.37.2)
|
90
87
|
simplecov (= 0.11.2)
|
91
88
|
sqlite3 (= 1.3.11)
|
92
89
|
|
93
|
-
RUBY VERSION
|
94
|
-
ruby 2.3.0p0
|
95
|
-
|
96
90
|
BUNDLED WITH
|
97
91
|
1.12.5
|
data/README.md
CHANGED
@@ -5,7 +5,7 @@ Super light Translation Ruby Gem agnostic to your framework and source data
|
|
5
5
|
[![Test Coverage](https://codeclimate.com/github/Streetbees/lost_in_translations/badges/coverage.svg)](https://codeclimate.com/github/Streetbees/lost_in_translations/coverage)
|
6
6
|
[![Build Status](https://travis-ci.org/Streetbees/lost_in_translations.svg?branch=master)](https://travis-ci.org/Streetbees/lost_in_translations)
|
7
7
|
|
8
|
-
## 1
|
8
|
+
## 1) Basic Usage
|
9
9
|
```ruby
|
10
10
|
class User < Struct.new(:title, :first_name, :last_name)
|
11
11
|
include LostInTranslations
|
@@ -20,7 +20,7 @@ class User < Struct.new(:title, :first_name, :last_name)
|
|
20
20
|
end
|
21
21
|
end
|
22
22
|
```
|
23
|
-
Class method
|
23
|
+
Class method ```.translate``` will redefine ```#title``` and ```#first_name``` instance methods in order to return the values that match the ```I18n.locale``` and the ```attribute name``` from the Hash returned by ```#translation_data```, otherwise calls the original redefined method.
|
24
24
|
|
25
25
|
```ruby
|
26
26
|
@user = User.new('Cavaleiro', 'Joao', 'Neve')
|
@@ -44,15 +44,23 @@ I18n.with_locale(:de) do
|
|
44
44
|
end
|
45
45
|
```
|
46
46
|
|
47
|
-
Instance method
|
47
|
+
Instance method ```#translate``` is also available:
|
48
48
|
```ruby
|
49
49
|
@user.translate(:first_name, :fr) # returns 'Jean'
|
50
50
|
@user.translate(:first_name, :en) # returns 'Jon'
|
51
51
|
@user.translate(:first_name, :de) # returns 'Joao'
|
52
52
|
```
|
53
53
|
|
54
|
-
|
55
|
-
|
54
|
+
And ```#<I18n.available_locales>_<translated_field>``` is also available:
|
55
|
+
```ruby
|
56
|
+
@user.fr_first_name # returns 'Jean'
|
57
|
+
@user.en_first_name # returns 'Jon'
|
58
|
+
@user.pt_first_name # returns 'Joao'
|
59
|
+
@user.de_first_name # returns 'Joao'
|
60
|
+
```
|
61
|
+
|
62
|
+
## 2) Ideal usage
|
63
|
+
If your ActiveRecord Model has a json attribute called ```translation_data```.
|
56
64
|
```ruby
|
57
65
|
class CreateUsers < ActiveRecord::Migration
|
58
66
|
def up
|
@@ -60,7 +68,7 @@ class CreateUsers < ActiveRecord::Migration
|
|
60
68
|
t.string :title
|
61
69
|
t.string :first_name
|
62
70
|
t.string :last_name
|
63
|
-
t.
|
71
|
+
t.jsonb :translation_data
|
64
72
|
t.timestamps
|
65
73
|
end
|
66
74
|
|
@@ -93,9 +101,9 @@ I18n.locale = :fr
|
|
93
101
|
@user.title # returns 'Cavaleiro'
|
94
102
|
```
|
95
103
|
|
96
|
-
## 3
|
104
|
+
## 3) Configuration
|
97
105
|
|
98
|
-
### 3.1 Your "translation_data" method (in all of your objects) is not called "translation_data"
|
106
|
+
### 3.1) Your "translation_data" method (in all of your objects) is not called "translation_data"
|
99
107
|
```ruby
|
100
108
|
LostInTranslations.configure do |config|
|
101
109
|
config.translation_data_field = 'my_translation_data_field'
|
@@ -121,7 +129,7 @@ I18n.locale = :fr
|
|
121
129
|
User.find(1).first_name # returns 'Jean'
|
122
130
|
```
|
123
131
|
|
124
|
-
### 3.2 Your "translation_data" method (in a particular object) is not called "translation_data"
|
132
|
+
### 3.2) Your "translation_data" method (in a particular object) is not called "translation_data"
|
125
133
|
```ruby
|
126
134
|
class User < ActiveRecord::Base
|
127
135
|
include LostInTranslations
|
@@ -143,7 +151,7 @@ I18n.locale = :fr
|
|
143
151
|
User.find(1).first_name # returns 'Jean'
|
144
152
|
```
|
145
153
|
|
146
|
-
### 3.3 Custom translation mechanism
|
154
|
+
### 3.3) Custom translation mechanism
|
147
155
|
```ruby
|
148
156
|
class MyTranslator
|
149
157
|
def self.translate(object, field, locale)
|
@@ -189,7 +197,7 @@ I18n.locale = :fr
|
|
189
197
|
User.find(1).first_name # returns 'Jean' from redis or yaml file
|
190
198
|
```
|
191
199
|
|
192
|
-
## 4 Instalation
|
200
|
+
## 4) Instalation
|
193
201
|
|
194
202
|
Add your application's Gemfile:
|
195
203
|
```
|
data/Rakefile
ADDED
@@ -14,10 +14,12 @@ module LostInTranslations
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def self.included(base_class)
|
17
|
-
base_class.include Base
|
17
|
+
base_class.send(:include, Base)
|
18
18
|
base_class.extend ClassMethods
|
19
19
|
end
|
20
20
|
|
21
|
+
include Base
|
22
|
+
|
21
23
|
def translate(field, locale = I18n.locale)
|
22
24
|
LostInTranslations.translate(self, field, locale) ||
|
23
25
|
Ruby.call_original_field(self, field)
|
data/lib/lost_in_translations.rb
CHANGED
@@ -10,9 +10,9 @@ module LostInTranslations
|
|
10
10
|
def self.included(base_class)
|
11
11
|
if defined?(::ActiveRecord::Base) &&
|
12
12
|
base_class.ancestors.include?(::ActiveRecord::Base)
|
13
|
-
base_class.include LostInTranslations::ActiveRecord
|
13
|
+
base_class.send(:include, LostInTranslations::ActiveRecord)
|
14
14
|
else
|
15
|
-
base_class.include Ruby
|
15
|
+
base_class.send(:include, Ruby)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
@@ -26,9 +26,24 @@ module LostInTranslations
|
|
26
26
|
|
27
27
|
def self.define_translation_methods(object, *fields)
|
28
28
|
fields.each do |field|
|
29
|
+
define_dynamic_translation_method(object, field)
|
30
|
+
define_particular_translation_method(object, field)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.define_dynamic_translation_method(object, method_name)
|
35
|
+
object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
36
|
+
def #{method_name}
|
37
|
+
translate(:#{method_name}, I18n.locale)
|
38
|
+
end
|
39
|
+
RUBY
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.define_particular_translation_method(object, method_name)
|
43
|
+
I18n.available_locales.each do |locale|
|
29
44
|
object.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
30
|
-
def #{
|
31
|
-
translate(:#{
|
45
|
+
def #{locale}_#{method_name}
|
46
|
+
translate(:#{method_name}, :#{locale})
|
32
47
|
end
|
33
48
|
RUBY
|
34
49
|
end
|
@@ -19,12 +19,12 @@ Gem::Specification.new do |gem|
|
|
19
19
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
20
|
gem.require_paths = ['lib']
|
21
21
|
|
22
|
+
gem.add_development_dependency 'pry', '0.10.3'
|
23
|
+
gem.add_development_dependency 'rake', '11.2.2'
|
22
24
|
gem.add_development_dependency 'rspec', '3.4.0'
|
23
|
-
gem.add_development_dependency 'byebug', '8.2.5'
|
24
25
|
gem.add_development_dependency 'sqlite3', '1.3.11'
|
25
26
|
gem.add_development_dependency 'rubocop', '0.37.2'
|
26
27
|
gem.add_development_dependency 'simplecov', '0.11.2'
|
27
|
-
gem.add_development_dependency 'pry-byebug', '3.3.0'
|
28
28
|
gem.add_development_dependency 'activerecord', '4.2.6'
|
29
29
|
gem.add_development_dependency 'codeclimate-test-reporter', '0.4.8'
|
30
30
|
|
@@ -2,6 +2,52 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe LostInTranslations do
|
4
4
|
|
5
|
+
describe "#define_translation_methods" do
|
6
|
+
context "when the passed methods do not exist" do
|
7
|
+
before do
|
8
|
+
@user_class = Struct.new(:first_name, :last_name) do
|
9
|
+
include LostInTranslations
|
10
|
+
end
|
11
|
+
|
12
|
+
LostInTranslations.define_translation_methods(@user_class, :unknown)
|
13
|
+
|
14
|
+
@user = @user_class.new('Joao', 'Neve')
|
15
|
+
end
|
16
|
+
|
17
|
+
it "#unknown should be defined" do
|
18
|
+
expect(@user.respond_to?(:unknown)).to be true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "#<I18n.available_locales>_unknown should be defined" do
|
22
|
+
I18n.available_locales.each do |locale|
|
23
|
+
expect(@user.respond_to?("#{locale}_unknown")).to be true
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when the passed methods do exist" do
|
29
|
+
before do
|
30
|
+
@user_class = Struct.new(:first_name, :last_name) do
|
31
|
+
include LostInTranslations
|
32
|
+
end
|
33
|
+
|
34
|
+
LostInTranslations.define_translation_methods(@user_class, :first_name)
|
35
|
+
|
36
|
+
@user = @user_class.new('Joao', 'Neve')
|
37
|
+
end
|
38
|
+
|
39
|
+
it "#first_name should be defined" do
|
40
|
+
expect(@user.respond_to?(:first_name)).to be true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "#<I18n.available_locales>_first_name should be defined" do
|
44
|
+
I18n.available_locales.each do |locale|
|
45
|
+
expect(@user.respond_to?("#{locale}_first_name")).to be true
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
5
51
|
describe "#included" do
|
6
52
|
context "when the object inherits from ActiveRecord" do
|
7
53
|
before do
|
data/spec/spec_helper.rb
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lost_in_translations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- StreetBees Dev Team
|
@@ -11,89 +11,89 @@ cert_chain: []
|
|
11
11
|
date: 2016-07-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: pry
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.10.3
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.10.3
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 11.2.2
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 11.2.2
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - '='
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 3.4.0
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - '='
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 3.4.0
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: sqlite3
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - '='
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 1.3.11
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - '='
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
68
|
+
version: 1.3.11
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rubocop
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - '='
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 0.
|
75
|
+
version: 0.37.2
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - '='
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 0.
|
82
|
+
version: 0.37.2
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: simplecov
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
87
|
- - '='
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version:
|
89
|
+
version: 0.11.2
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - '='
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version:
|
96
|
+
version: 0.11.2
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: activerecord
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -151,6 +151,7 @@ files:
|
|
151
151
|
- Gemfile
|
152
152
|
- Gemfile.lock
|
153
153
|
- README.md
|
154
|
+
- Rakefile
|
154
155
|
- lib/lost_in_translations.rb
|
155
156
|
- lib/lost_in_translations/active_record.rb
|
156
157
|
- lib/lost_in_translations/base.rb
|