Skip to content

Extract Grape::Util::Translation for shared I18n logic#2662

Merged
dblock merged 1 commit intomasterfrom
extract_translation
Mar 11, 2026
Merged

Extract Grape::Util::Translation for shared I18n logic#2662
dblock merged 1 commit intomasterfrom
extract_translation

Conversation

@ericproulx
Copy link
Contributor

@ericproulx ericproulx commented Mar 7, 2026

Summary

Isolates all I18n logic into a single Grape::Util::Translation module, eliminating duplicated locale-fallback code and direct I18n calls scattered across exceptions and validators.

Changes

Grape::Util::Translation (new module)

  • #translate wraps I18n.translate with a MISSING sentinel to reliably detect absent keys (vs. blank translations)
  • Automatically falls back to :en when the current locale has no translation, unless enforce_available_locales: true and :en is not in the available locales list
  • Extra keyword args (**) are forwarded verbatim to I18n as interpolation variables

Consumers

  • Exceptions::Base — includes Translation; removes local translate, fallback_message, and related constants; Hash message branch uses pattern matching (=>) to destructure { key:, **opts }
  • Exceptions::Validationattr_accessorattr_reader for params and message_key; params always normalized to Array; Hash message support ({ key:, **opts }) for message_key extraction and translation
  • Exceptions::ValidationErrors — removes ERRORS_FORMAT_KEY / DEFAULT_ERRORS_FORMAT; delegates to translate
  • Validators::Base — includes Translation; custom validators inherit translate automatically
  • LengthValidator / SameAsValidator — replaced format I18n.t(...) with translate

Spec — I18n isolation

  • translation_spec: new spec for Grape::Util::Translation directly, including I18n::ReservedInterpolationKey propagation
  • coerce_validator_spec: replaced I18n.load_path << mutation with store_translations; uses I18n.with_locale at call sites; no global locale mutation
  • base_i18n_spec: new spec verifying request-time locale is used regardless of the locale active at definition time
  • base_spec: all I18n state changes (available_locales, enforce_available_locales) are self-contained via around + ensure
  • validation_spec: expanded with Hash message (interpolation), Proc message, and String message cases
  • Deleted zh-CN.yml: translations stored inline via store_translations

Test plan

  • bundle exec rspec
  • bundle exec rspec --seed 16961 (order-sensitive check)

🤖 Generated with Claude Code

@github-actions
Copy link

github-actions bot commented Mar 7, 2026

Danger Report

No issues found.

View run

@ericproulx ericproulx force-pushed the extract_translation branch from 70253be to e82e52e Compare March 7, 2026 15:17
@ericproulx ericproulx marked this pull request as draft March 7, 2026 15:31
@ericproulx ericproulx force-pushed the extract_translation branch 3 times, most recently from 7b25994 to daeec9c Compare March 7, 2026 17:22
@ericproulx ericproulx requested a review from dblock March 7, 2026 17:26
@ericproulx ericproulx force-pushed the extract_translation branch 2 times, most recently from 46be620 to 7119a08 Compare March 7, 2026 22:55
@ericproulx ericproulx marked this pull request as ready for review March 7, 2026 22:55
@ericproulx ericproulx force-pushed the extract_translation branch 2 times, most recently from 714a735 to ae49717 Compare March 9, 2026 22:12
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@ericproulx ericproulx force-pushed the extract_translation branch from ae49717 to e0c075d Compare March 9, 2026 22:15
ericproulx added a commit that referenced this pull request Mar 9, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
ericproulx added a commit that referenced this pull request Mar 9, 2026
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dblock dblock merged commit 77efd52 into master Mar 11, 2026
81 checks passed
@dblock dblock deleted the extract_translation branch March 11, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants