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

Translation not missing if default contains nil #431

Open
mkamensky opened this issue Sep 22, 2018 · 3 comments
Open

Translation not missing if default contains nil #431

mkamensky opened this issue Sep 22, 2018 · 3 comments

Comments

@mkamensky
Copy link

The way I read the docs, calling

I18n.translate(:foo, default: [nil])

Should return nil rather than raise an exception (when :foo does not exist). However, this is not what happens in the code (or in real life).

@dillonwelch
Copy link
Contributor

I think it depends on your settings. The exception is only raised if your have i18n configured to raise errors on missing translations. For example, here's what I get in my rails console.

[7] pry(main)> I18n.translate(:foo, default: [nil])
=> "translation missing: en.foo"

The way I read the docs

Can you link to the part of the docs where you read that this should return nil?

@mkamensky
Copy link
Author

I think it depends on your settings. The exception is only raised if your have i18n configured to raise errors on missing translations. For example, here's what I get in my rails console.

[7] pry(main)> I18n.translate(:foo, default: [nil])
=> "translation missing: en.foo"

Thanks. The point is not the exception, it is the expectation to get nil rather than an exception, or 'translate missing'. I do want to raise an exception when the translation is missing and I did not supply default:nil

The way I read the docs

Can you link to the part of the docs where you read that this should return nil?

I don't actual remember what I was referring to, and can't find it now... In any case, I think the behaviour I was expecting is more consistent and more useful: The current behaviour is that the function goes through the list of defaults one by one as long as the previous translation was missing. This happens for all values except nil, so I would expect it to happen for nil as well. It is also more useful, since it allows checking if either the translation or the defaults exist, and if not continue in some other manner, rather than raising an exception (of course, there are other means to achieve the same, but less convenient).

@rahim
Copy link

rahim commented Aug 28, 2020

I hit this too.

For what it's worth for anyone stumbling on the same problem, I had something like this in a Rails view that I was doing some maintenance on:

<% if t("a.b.#{dynamic_key}", default: nil) %>
  <p><%= t("a.b.#{dynamic_key}") %></p>
<% end %>

With raise_on_missing_translations = true that default: nil is not enough to hint we're sometimes expecting missing translations here.

I found the following was though:

<% if t("a.b.#{dynamic_key}", default: "").present? %>
  <p><%= t("a.b.#{dynamic_key}") %></p>
<% end %>

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

No branches or pull requests

3 participants