lost_in_translations 1.4.2 → 1.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 32e8b3114257ea7a04833848621b662ac170db3e
4
- data.tar.gz: 70d1fd6bb408f437056bfaaf90c05453e0b2bf78
2
+ SHA256:
3
+ metadata.gz: 94899fa31aad1c22083125064d145f3f955cd5c7d895cabf547c89e218baf907
4
+ data.tar.gz: 48cf57640178e2f5f22210eb95ce71d784885fed9cf06f4e2594f1cbac8bcf6b
5
5
  SHA512:
6
- metadata.gz: 8cadaef4095afee2cafa73aebd6500b2d160e2dd318bea23c2025b60f0c396764b1b9026d826d4ca6fc2c202733acdd13019c1d08270b1f126bbeb06e5c36858
7
- data.tar.gz: 2b252e278302c01e130e3eb753bdeb9bc3d836e7e396922d940640a073ab7c6b00b12a5cfa1e6f41bf3c0e85de2e64b3205b2acb94e6d5d14d9ae690a7bccce3
6
+ metadata.gz: 476dec88ac5c73978729a095955d124e84e38b542c4bd31f0448f65bd182f695faeb86ae509689b65f5495d6d718f828e7d6b1e9721d2463a58e677b4c06cdae
7
+ data.tar.gz: ea672a70d24764931eb85f42d80c0909c06446707987ca9f0166d1c0a22ce060434c47b58a7783e20cf8270ef18315cf1bfd12222c9dae4457c7741c59790c2e
data/README.md CHANGED
@@ -102,7 +102,7 @@ class CreateUsers < ActiveRecord::Migration
102
102
  User.create \
103
103
  title: 'Cavaleiro',
104
104
  first_name: 'Joao',
105
- last_name: 'Neve'
105
+ last_name: 'Neve',
106
106
  translation_data: {
107
107
  en: { first_name: 'Jon', last_name: 'Snow' },
108
108
  fr: { first_name: 'Jean', last_name: 'Neige' }
@@ -128,7 +128,69 @@ I18n.locale = :fr
128
128
  @user.title # returns nil
129
129
  ```
130
130
 
131
- ## 3) Instalation
131
+ ## 3) Forcing locales
132
+
133
+ There are many cases when you'll need to translate only a part of your data,
134
+ specially if this part is returned in a list of translated data.
135
+
136
+ For those cases you only have to define the field (virtual or persisted)
137
+ `force_locale` with one valid locale:
138
+
139
+ ```ruby
140
+ class User < ActiveRecord::Base
141
+ include LostInTranslations
142
+
143
+ translate :title, :first_name
144
+ end
145
+
146
+ User.create(
147
+ title: 'Cavaleiro',
148
+ first_name: 'Joao',
149
+ last_name: 'Neve',
150
+ force_locale: nil,
151
+ translation_data: {
152
+ en: { first_name: 'Jon', last_name: 'Snow' },
153
+ fr: { first_name: 'Jean', last_name: 'Neige' }
154
+ }
155
+ )
156
+
157
+ User.create(
158
+ title: 'Cavaleiro',
159
+ first_name: 'Joao',
160
+ last_name: 'Neve',
161
+ force_locale: 'fr'
162
+ translation_data: {
163
+ en: { first_name: 'Jon', last_name: 'Snow' },
164
+ fr: { first_name: 'Jean', last_name: 'Neige' }
165
+ }
166
+ )
167
+
168
+ User.create(
169
+ title: 'Cavaleiro',
170
+ first_name: 'Joao',
171
+ last_name: 'Neve',
172
+ force_locale: 'en'
173
+ translation_data: {
174
+ en: { first_name: 'Jon', last_name: 'Snow' },
175
+ fr: { first_name: 'Jean', last_name: 'Neige' }
176
+ }
177
+ )
178
+
179
+ I18n.locale = :pt
180
+
181
+ User.all.each do |user|
182
+ puts user.name
183
+ end
184
+
185
+ # It'll automatically output:
186
+ # Joao
187
+ # Jean
188
+ # Jon
189
+
190
+ ```
191
+
192
+
193
+ ## 4) Instalation
132
194
 
133
195
  Add this to your Gemfile:
134
196
  ```
@@ -147,7 +209,7 @@ LostInTranslations.reload
147
209
  ```
148
210
  Because Rails starts with a set of **I18n.available_locales** different from when it finished loading and **LostInTranslations** needs to redefine new methods for those new locales in your classes.
149
211
 
150
- ## 4) F.A.Q.
212
+ ## 5) F.A.Q.
151
213
 
152
214
  - [I18n.available_locales as changed, how do I recreate the new translation methods?](https://github.com/Streetbees/lost-in-translations/wiki/Redefining-translation-methods)
153
215
  - [Your "translation_data" method (in all of your objects) is not called "translation_data"](https://github.com/Streetbees/lost-in-translations/wiki/translation_data-configuration-version-1)
@@ -22,17 +22,23 @@ module LostInTranslations
22
22
 
23
23
  def fallback_to_default_locale(locale)
24
24
  LostInTranslations.config.fallback_to_default_locale ||
25
- locale.to_sym == I18n.default_locale.to_sym
25
+ locale.to_sym == I18n.default_locale.to_sym
26
26
  end
27
27
 
28
28
  module ClassMethods
29
29
  attr_writer :translation_data_field
30
+ attr_writer :force_locale_field
30
31
 
31
32
  def translation_data_field
32
33
  @translation_data_field ||
33
34
  LostInTranslations.config.translation_data_field
34
35
  end
35
36
 
37
+ def force_locale_field
38
+ @force_locale_field ||
39
+ LostInTranslations.config.force_locale_field
40
+ end
41
+
36
42
  def translation_fields
37
43
  @translation_fields ||= []
38
44
  end
@@ -1,4 +1,5 @@
1
1
  module LostInTranslations
2
2
  Config = Struct.new \
3
- :translation_data_field, :translator, :fallback_to_default_locale
3
+ :translation_data_field, :translator, :fallback_to_default_locale,
4
+ :force_locale_field
4
5
  end
@@ -1,7 +1,9 @@
1
1
  module LostInTranslations
2
2
  module Translator
3
3
  class Base
4
- def self.translate(object, field, locale)
4
+ def self.translate(object, field, original_locale)
5
+ locale = check_forcing_locale(object, original_locale)
6
+
5
7
  data = translation_data(object)
6
8
 
7
9
  translations = data[locale.to_sym] || data[locale.to_s] || {}
@@ -42,6 +44,18 @@ module LostInTranslations
42
44
 
43
45
  translation_data_field
44
46
  end
47
+
48
+ def self.check_forcing_locale(object, locale)
49
+ force_locale_field = object.class.force_locale_field
50
+
51
+ if object.respond_to?(force_locale_field)
52
+ force_locale = object.send(force_locale_field)
53
+
54
+ return force_locale unless force_locale.to_s.length < 2
55
+ end
56
+
57
+ locale
58
+ end
45
59
  end
46
60
  end
47
61
  end
@@ -1,3 +1,3 @@
1
1
  module LostInTranslations
2
- VERSION = '1.4.2'.freeze
2
+ VERSION = '1.4.3'.freeze
3
3
  end
@@ -16,7 +16,12 @@ module LostInTranslations
16
16
  end
17
17
 
18
18
  def self.config
19
- @config ||= Config.new('translation_data', Translator::Base)
19
+ @config ||= Config.new(
20
+ 'translation_data',
21
+ Translator::Base,
22
+ nil,
23
+ 'force_locale'
24
+ )
20
25
  end
21
26
 
22
27
  def self.translate(*args)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lost_in_translations
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - StreetBees Dev Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-04 00:00:00.000000000 Z
11
+ date: 2018-07-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
171
  version: '0'
172
172
  requirements: []
173
173
  rubyforge_project:
174
- rubygems_version: 2.5.1
174
+ rubygems_version: 2.7.6
175
175
  signing_key:
176
176
  specification_version: 4
177
177
  summary: Ruby Translation Gem