Externalize Rails' model attribute names via gettext
Problem
In the GitLab codebase we have two ways of externalizing strings. I18n
and gettext
. Although we prefer gettext
there's still config/locales
(for I18n
) which is used to externalize model attribute names. However, these translations are not picked up by gettext
thus are not being translated.
Example
Although GrafanaIntegration
has translations for its attribute names in config/locales
they are not picked up by gettext
so the translations is missing:
pry(main)> I18n.locale = :zh_CN
=> :zh_CN
pry(main)> GrafanaIntegration.new.tap(&:valid?).errors.full_messages
=> ["Grafana url must be a valid URL", "Encrypted token 不能为空字符", "项目 不能为空字符"]
Proposed solutions
Both solutions below suggest to stop externalizing model attribute names in config/locales/**/*.yml
.
1️⃣ Run rake gettext:store_model_attributes
As mentioned in https://github.com/grosser/gettext_i18n_rails/blob/master/Readme.md#activerecord---error-messages one can use rake gettext:store_model_attributes
to generate locale/model_attributes.rb
which contains all model name attribute (6878 lines). Those externalizations are picked by gettext
:
...
_('GrafanaIntegration|Enabled')
_('GrafanaIntegration|Encrypted token')
_('GrafanaIntegration|Encrypted token iv')
_('GrafanaIntegration|Grafana url')
...
We could run rake gettext:store_model_attributes
before rake gettext:regenerate
is run.
2️⃣ Add model attribute externalizations only if needed
Instead of generating externalizations for all model attributes we could only add them if needed.
See !89996 (diffs, comment 1011700729)
Blockers
Currently, translating attribute names via gettext
is disabled in application configuration via 628d641b. We should investigate if enabling this option now works again.