internationalize 0.4.0 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0a7c08fb66904ff4e73e13f590f4546af940ce8f9adc59d870adf60e71f54fc0
4
- data.tar.gz: e6e5b34d18588fc2b8d1fd18b182d73f39bffe2ad67c7f383976eaa30d8b6ee2
3
+ metadata.gz: e35fc3d32a34b9dde3097192bd791ffb50b8d62a68b1520347da4ed2ac72224f
4
+ data.tar.gz: decad482b4b5f99fedf070f2f64fb655f088ab6397f709a5362623f59570342d
5
5
  SHA512:
6
- metadata.gz: 6bf2d4d4e0cfcb36adbd787f3a839dd6bbe4290468eb2934b99af4c4541d948aef7d3f5ea71329e2f99aabe1f2530c83af8955d544d8991e0a9341b4abd393cb
7
- data.tar.gz: 5ccafbadb398e1ad9b956990b10334c80577f248d32b4e788a94dc5ad39fffdb47fa1c22eb892216af71784ea4770b4d0bfac43ceeaac951cff63575a4ce1152
6
+ metadata.gz: 2a652e6a3725561e8b5455b6676a490d214aff84f78163b5ab02385a4708381e80cdb8125782b1cb36eab9ae386384768e0231a7e988cf637ddbf464a8ff62d8
7
+ data.tar.gz: d4e980bb9d34168a802660b918bf3d092cf6b9be3b62d1b81696aa3d9ee5d026dcb335e4993fa6ca55a9b4964d58a82eaefd7d6fbedd8eae64ede2a99391519b
data/README.md CHANGED
@@ -205,12 +205,50 @@ class Article < ApplicationRecord
205
205
  end
206
206
  ```
207
207
 
208
- Errors from `validates_international` are added to locale-specific keys:
208
+ Errors from `validates_international` are added to the base attribute name for clean user-facing messages:
209
209
 
210
210
  ```ruby
211
- article.errors[:title_en] # => ["has already been taken"]
211
+ article.errors[:title] # => ["has already been taken"]
212
+ # Displays as: "Title has already been taken" (not "Title en has already been taken")
212
213
  ```
213
214
 
215
+ #### Locale-Specific Error Messages
216
+
217
+ For admin interfaces where you need to indicate which specific locale failed validation, use custom validations that add errors to locale-suffixed attributes:
218
+
219
+ ```ruby
220
+ class Article < ApplicationRecord
221
+ include Internationalize::Model
222
+ international :title
223
+
224
+ validate :validate_required_translations
225
+
226
+ private
227
+
228
+ def validate_required_translations
229
+ [:en, :de].each do |locale|
230
+ if send("title_#{locale}").blank?
231
+ errors.add("title_#{locale}", :blank)
232
+ end
233
+ end
234
+ end
235
+ end
236
+ ```
237
+
238
+ When using locale-suffixed error keys, configure Rails I18n to provide user-friendly attribute names:
239
+
240
+ ```yaml
241
+ # config/locales/en.yml
242
+ en:
243
+ activerecord:
244
+ attributes:
245
+ article:
246
+ title_en: "Title (English)"
247
+ title_de: "Title (German)"
248
+ ```
249
+
250
+ This displays as "Title (German) can't be blank" instead of "Title de can't be blank".
251
+
214
252
  ### ActionText Support
215
253
 
216
254
  For rich text with attachments (requires ActionText):
data/context/model-api.md CHANGED
@@ -120,18 +120,45 @@ end
120
120
 
121
121
  ### Error Keys
122
122
 
123
- Standard Rails validations add errors to the virtual attribute:
123
+ Both standard Rails validations and `validates_international` add errors to the base attribute for clean user-facing messages:
124
124
 
125
125
  ```ruby
126
- article.errors[:title] # => ["can't be blank"]
126
+ article.errors[:title] # => ["has already been taken"]
127
+ # Displays as: "Title has already been taken" (not "Title en has already been taken")
127
128
  ```
128
129
 
129
- `validates_international` adds errors to locale-specific keys:
130
+ #### Locale-Specific Error Messages
131
+
132
+ For admin interfaces where you need to indicate which specific locale failed, use custom validations that add errors to locale-suffixed attributes:
130
133
 
131
134
  ```ruby
132
- article.errors[:title_en] # => ["has already been taken"]
135
+ validate :validate_required_translations
136
+
137
+ private
138
+
139
+ def validate_required_translations
140
+ [:en, :de].each do |locale|
141
+ if send("title_#{locale}").blank?
142
+ errors.add("title_#{locale}", :blank)
143
+ end
144
+ end
145
+ end
133
146
  ```
134
147
 
148
+ When using locale-suffixed error keys, configure Rails I18n for user-friendly display:
149
+
150
+ ```yaml
151
+ # config/locales/en.yml
152
+ en:
153
+ activerecord:
154
+ attributes:
155
+ article:
156
+ title_en: "Title (English)"
157
+ title_de: "Title (German)"
158
+ ```
159
+
160
+ This displays as "Title (German) can't be blank" instead of "Title de can't be blank".
161
+
135
162
  ## ActionText Support
136
163
 
137
164
  For rich text with attachments, include `Internationalize::RichText` and use `international_rich_text`:
@@ -60,7 +60,7 @@ module Internationalize
60
60
  locales.each do |locale|
61
61
  value = record.send("#{attr}_#{locale}")
62
62
  if value.blank?
63
- record.errors.add("#{attr}_#{locale}", :blank)
63
+ record.errors.add(attr, :blank)
64
64
  end
65
65
  end
66
66
  end
@@ -76,7 +76,7 @@ module Internationalize
76
76
  scope = scope.where.not(id: record.id) if record.persisted?
77
77
 
78
78
  if scope.exists?
79
- record.errors.add("#{attr}_#{locale}", :taken)
79
+ record.errors.add(attr, :taken)
80
80
  end
81
81
  end
82
82
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Internationalize
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: internationalize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampo Kuokkanen