ruby-i18n / ruby-i18n/i18n

Defining translations for nested forms in namespaced resources

Open
#693 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Ruby
Stars
1k
Forks
415
Avg merge
2d 3h
Merged PRs (30d)
1

Description

What I tried to do

I have the below setup

class BudgetLine < ApplicationRecord
  has_many :budget_line_items
  accepts_nested_attributes_for :budget_line_items
end

class BudgetLine::Accommodation < BudgetLine
end

class BudgetLineItem < ApplicationRecord
  belongs_to :budget_line
end

Now I'm trying to specify labels for the nested form of BudgetLineItem inside a BudgetLine::Accommodation form.

What I expected to happen

According to the i18n documentation, you can normally do something like this:

en:
  activerecord:
    attributes:
      user/role:
        admin: "Admin"
        contributor: "Contributor"

What actually happened

This works if the model in question is not namespaced, as with User above. However, when you want to specify a namespaced model it has the same syntax with '/', so it becomes:

en:
  activerecord:
    attributes:
      budget_line/accommodation/budget_line_items:
        unit: Room

which does not work.

I've tried different variations of this, e.g.

en:
  activerecord:
    attributes:
      budget_line:
        accommodation/budget_line_items:
          unit: Room

which also does not work, because this way of specifying namespaces in locales seems to be deprecated..?

As far as I understand, if the label is to be translated correctly in my case, the following should return the correct translation: BudgetLine::Accommodation.human_attribute_name("budget_line_items.unit"), but so far I've been unsuccessful in my attempts to target this.

I'm not sure if this is a limitation with the library, of if there's something I'm missing here..?

Versions of i18n, rails, and anything else you think is necessary

i18n (1.14.4)
rails (7.1.3.2)
´´´

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the BudgetLine, BudgetLine::Accommodation, and BudgetLineItem models and the nested-form YAML examples in the report. Start at BudgetLine::Accommodation.human_attribute_name("budget_line_items.unit") and trace the translation lookup; done means a documented locale key returns "Room" for that attribute and a regression test covers the namespaced case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rails, ruby
Domain
localization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.