Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chain fallback backends #499

Merged
merged 2 commits into from
Jan 8, 2020
Merged

Conversation

vipera
Copy link
Contributor

@vipera vipera commented Nov 12, 2019

Building on #470, if using fnando/i18n-js and some translations are present in a Simple backend, and some in an ActiveRecord backend, the current deep_merge! done in the chain backend will override a valid translation with a nil value if a previously chained backend yields nil.

This patch updates the Hash#deep_merge! implementation to match the activesupport one, and provides a block to prevent nil values from affecting translations yielded by a chained backend.

Use case:
I'm using a chain backend of (Simple, ActiveRecord). The simple backend has an empty or partial YAML, like this:

---
en:
  test:

The activerecord backend has a record that overrides it:

> select * from translations where locale = 'en' and key = 'test';

| locale | key  | value | ...
+--------+------+-------+----
|     en | test |  Test | ...

I think the nil YAML value shouldn't prevent the other backend from yielding its value, that's how I18n::Backend::Chain#translate works, after all.

Thanks for reviewing this!

Activesupport's Hash#deep_merge! has support for a block that
describes how two values are merged.
When merging translations from all backends, fall back to the next
backend if the current backend presents a nil value.
@vipera vipera closed this Dec 17, 2019
@vipera vipera reopened this Dec 17, 2019
@CrAsH1101
Copy link
Contributor

Any updates on this one? I would love to see this one merged ;)

@radar
Copy link
Collaborator

radar commented Jan 8, 2020

LGTM. Thank you!

@radar radar merged commit 719a36a into ruby-i18n:master Jan 8, 2020
@radar
Copy link
Collaborator

radar commented Jan 9, 2020

This change has now gone out in i18n 1.8.0. Thanks!

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.

3 participants