internationalize 0.2.0 → 0.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 520c17d4993a0145c00081a95a30f21c9a3b56b0879589fc87f4731564be7f1e
4
- data.tar.gz: 24478d271a6500c4104c8e26db4238ed1857b533b25e5c351e30cfc45fe7c13a
3
+ metadata.gz: a78574c3b203b7ae3dd699ab2c4c429503922d134f40c990e99dea103e241b12
4
+ data.tar.gz: 2c0ff80e85f73e66e9e04ad572d4b711150ce8853aab53880b326c65be8a551f
5
5
  SHA512:
6
- metadata.gz: a15ca8db8848c85feec7d912c47905a0cbd665aed43721642b97e039f607699edbeb207666d3af98e92e412d6c6ac0c89298b4ac4aba5c9643bf2554c2f59a08
7
- data.tar.gz: b380cf31e89bd26994935e733c89df8b23771e83a73606f39dd6092f833aec11f0a4e471ec8c8c8ff914cd3079a15b0d05a4d016962936adc707302acaec5683
6
+ metadata.gz: bbfe57de55d2e7f0d52b02c34eac3c79fa0a71c353dbccf0f65e1c82053e77f66f83ebe40fa69262a123d81bd4c7f78624bc15de6b5001d9227e7032d697633e
7
+ data.tar.gz: d45d9c2a223eb3e740e2da4e17266d4eb0a3590162425f9fb52620224cbe4844dfc987af7394893d42b1a601689f163fff3b05d0531009c296659efd74535162
data/CHANGELOG.md CHANGED
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.2] - 2024-11-29
11
+
12
+ ### Added
13
+
14
+ - Validation for hyphenated locales (e.g., `zh-TW`) - raises helpful error suggesting underscore format (`zh_TW`)
15
+ - Auto-load `Internationalize::RichText` when ActionText is available (no manual require needed)
16
+
17
+ ## [0.2.1] - 2024-11-29
18
+
19
+ ### Added
20
+
21
+ - ActionText documentation in agent context files
22
+
10
23
  ## [0.2.0] - 2024-11-29
11
24
 
12
25
  ### Added
@@ -14,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
14
27
  - ActionText support via `international_rich_text` (optional, requires ActionText)
15
28
  - Generates `has_rich_text` for each locale with unified accessor
16
29
  - Full attachment support per locale
30
+ - Validation for `title_translations=` setter - rejects non-Hash values and invalid locales
17
31
 
18
32
  ### Removed
19
33
 
data/README.md CHANGED
@@ -175,11 +175,9 @@ article.title # => "Hello" (falls back to :en)
175
175
 
176
176
  ### ActionText Support
177
177
 
178
- For rich text with attachments, use `international_rich_text` (requires ActionText):
178
+ For rich text with attachments, use `international_rich_text` (auto-loaded when ActionText is available):
179
179
 
180
180
  ```ruby
181
- require "internationalize/rich_text"
182
-
183
181
  class Article < ApplicationRecord
184
182
  include Internationalize::Model
185
183
  include Internationalize::RichText
@@ -86,6 +86,7 @@ Article.international_order(:title, :desc)
86
86
  - No JOINs - data lives in the same table
87
87
  - Automatic fallback to default locale
88
88
  - Works with SQLite, PostgreSQL, and MySQL
89
+ - ActionText support via `international_rich_text` (see Model API)
89
90
 
90
91
  ## Important: Column Defaults
91
92
 
data/context/model-api.md CHANGED
@@ -21,7 +21,7 @@ For each `international :title` declaration:
21
21
  | `title=` | Set translation for current `I18n.locale` |
22
22
  | `title?` | Check if translation exists |
23
23
  | `title_translations` | Get raw hash of all translations |
24
- | `title_translations=` | Set all translations at once |
24
+ | `title_translations=` | Set all translations at once (validates locale keys) |
25
25
 
26
26
  ### Locale-Specific Accessors
27
27
 
@@ -79,3 +79,37 @@ article.title_en = "Hello"
79
79
  I18n.locale = :de
80
80
  article.title # => "Hello" (falls back to default locale)
81
81
  ```
82
+
83
+ ## ActionText Support
84
+
85
+ For rich text with attachments, use `international_rich_text` (auto-loaded when ActionText is available):
86
+
87
+ ```ruby
88
+ class Article < ApplicationRecord
89
+ include Internationalize::Model
90
+ include Internationalize::RichText
91
+
92
+ international_rich_text :content
93
+ end
94
+ ```
95
+
96
+ This generates `has_rich_text :content_en`, `has_rich_text :content_de`, etc. for each locale.
97
+
98
+ ### Generated Methods
99
+
100
+ | Method | Description |
101
+ |--------|-------------|
102
+ | `content` | Get rich text for current locale (with fallback) |
103
+ | `content=` | Set rich text for current locale |
104
+ | `content?` | Check if rich text exists |
105
+ | `content_en` | Direct access to English rich text |
106
+ | `content_de` | Direct access to German rich text |
107
+ | `content_translated?(:de)` | Check if translation exists |
108
+ | `content_translated_locales` | Array of locales with content |
109
+
110
+ ```ruby
111
+ article.content = "<p>Hello</p>" # Sets for current locale
112
+ article.content # Gets for current locale (with fallback)
113
+ article.content.body # ActionText::Content object
114
+ article.content.embeds # Attachments work per-locale
115
+ ```
@@ -324,7 +324,21 @@ module Internationalize
324
324
 
325
325
  # Set all translations at once
326
326
  define_method("#{attr}_translations=") do |hash|
327
- write_attribute(translations_column, hash&.stringify_keys || {})
327
+ return write_attribute(translations_column, {}) if hash.nil?
328
+
329
+ unless hash.is_a?(Hash)
330
+ raise ArgumentError, "#{attr}_translations must be a Hash, got #{hash.class}"
331
+ end
332
+
333
+ allowed_locales = Internationalize.locales.map(&:to_s)
334
+ hash.each_key do |key|
335
+ unless allowed_locales.include?(key.to_s)
336
+ raise ArgumentError, "Invalid locale '#{key}' for #{attr}_translations. " \
337
+ "Allowed locales: #{allowed_locales.join(', ')}"
338
+ end
339
+ end
340
+
341
+ write_attribute(translations_column, hash.stringify_keys)
328
342
  end
329
343
  end
330
344
 
@@ -334,6 +348,12 @@ module Internationalize
334
348
 
335
349
  Internationalize.locales.each do |locale|
336
350
  locale_str = locale.to_s
351
+
352
+ if locale_str.include?("-")
353
+ raise ArgumentError, "Locale '#{locale}' contains a hyphen which is invalid for Ruby method names. " \
354
+ "Use underscore format instead: :#{locale_str.tr('-', '_')}"
355
+ end
356
+
337
357
  getter_method = :"#{attr}_#{locale}"
338
358
 
339
359
  # Getter: article.title_en
@@ -26,6 +26,15 @@ module Internationalize
26
26
  # @param name [Symbol] the attribute name
27
27
  #
28
28
  def international_rich_text(name)
29
+ # Validate locales don't contain hyphens (invalid for Ruby method names)
30
+ Internationalize.locales.each do |locale|
31
+ locale_str = locale.to_s
32
+ if locale_str.include?("-")
33
+ raise ArgumentError, "Locale '#{locale}' contains a hyphen which is invalid for Ruby method names. " \
34
+ "Use underscore format instead: :#{locale_str.tr('-', '_')}"
35
+ end
36
+ end
37
+
29
38
  # Generate has_rich_text for each locale
30
39
  Internationalize.locales.each do |locale|
31
40
  rich_text_name = :"#{name}_#{locale}"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Internationalize
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.2"
5
5
  end
@@ -6,6 +6,7 @@ require "active_record"
6
6
  require_relative "internationalize/version"
7
7
  require_relative "internationalize/adapters"
8
8
  require_relative "internationalize/model"
9
+ require_relative "internationalize/rich_text" if defined?(ActionText)
9
10
 
10
11
  module Internationalize
11
12
  class << self
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.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sampo Kuokkanen