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 +4 -4
- data/README.md +40 -2
- data/context/model-api.md +31 -4
- data/lib/internationalize/validations.rb +2 -2
- data/lib/internationalize/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e35fc3d32a34b9dde3097192bd791ffb50b8d62a68b1520347da4ed2ac72224f
|
|
4
|
+
data.tar.gz: decad482b4b5f99fedf070f2f64fb655f088ab6397f709a5362623f59570342d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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[:
|
|
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
|
-
|
|
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] # => ["
|
|
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
|
-
|
|
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
|
-
|
|
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(
|
|
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(
|
|
79
|
+
record.errors.add(attr, :taken)
|
|
80
80
|
end
|
|
81
81
|
end
|
|
82
82
|
end
|