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

Add an I18n.exists? check that does not fall back #365

Closed
toncid opened this issue Mar 30, 2017 · 7 comments
Closed

Add an I18n.exists? check that does not fall back #365

toncid opened this issue Mar 30, 2017 · 7 comments

Comments

@toncid
Copy link

toncid commented Mar 30, 2017

What I tried to do

Prerequisites: fallbacks are enabled and there is a fallback value for the key.

Check whether a locale entry exists for a key.

What I expected to happen

I18n.exists? used to return false, because the key was not present in the locale and fallbacks were not considered.

What actually happened

I18n.exists? now returns true, because fallbacks are now considered.

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

Please note that this is not a bug report, but rather a feature request.

The behavior change was introduced through #326. I believe that the change is correct and that the previous implementation was erroneous: I18n.exists? should fall back by default when fallbacks are enabled.

However, now there is no way to check for key presence without falling back automatically and thus getting false results. I recommend either:

  • Introducing an optional parameter or in the options hash: def exists?(..., skip_fallbacks = false)
  • Introducing another method: def exists_in_locale?
@yesthatguy
Copy link

yesthatguy commented Sep 6, 2018

My workaround for the moment is to raise and catch errors, but it would be nice for exists? to accept the fallback parameter similar to translate

begin
  I18n.t(key, locale: locale, fallback: false, raise: true)
rescue I18n::MissingTranslationData
  false
else
  true
end

@toncid
Copy link
Author

toncid commented Sep 10, 2018

Interesting workaround. I am thinking of monkey-patching it into I18n as my_exists? until something similar is re-introduced into the gem.

@svenfuchs any thoughts on extending exists? with optional parameters and thus unblocking us from v0.7.0?

@andi-dev
Copy link

translate and exists? are meant to behave the same, it would make sense to me if exists? would also consider options, e.g. fallback: false.

Currently 'exists?' calls 'lookup', which in return takes options (translate is passing the fallback option to lookup).

So if I am not mistaken it should be as easy as allowing to pass options through exists? to lookup.

voodoorai2000 added a commit to consuldemocracy/consuldemocracy that referenced this issue Oct 4, 2018
There where two issues with the current implementation:

- There was a possible duplication between looking up the language name in key "locale" and in key "i18n.language.name"

- The "default" option was not being picked up, as the fallback always returned the default locale's translation, "English"

With this implementation there is only a single place to put the language name: i18n.language.name. I think this place is easier to find and understand for Crowdin translators than a "locale" key hidden in general.yml

If the translation is not found we display the language key, instead of English, which makes more sense to me too 😌

Solution based on recent comments[1] on a related I18n issue

[1] ruby-i18n/i18n#365 (comment)
voodoorai2000 added a commit to consuldemocracy/consuldemocracy that referenced this issue Oct 5, 2018
There where two issues with the current implementation:

- There was a possible duplication between looking up the language name in key "locale" and in key "i18n.language.name"

- The "default" option was not being picked up, as the fallback always returned the default locale's translation, "English"

With this implementation there is only a single place to put the language name: i18n.language.name. I think this place is easier to find and understand for Crowdin translators than a "locale" key hidden in general.yml

If the translation is not found we display the language key, instead of English, which makes more sense to me too 😌

Solution based on recent comments[1] on a related I18n issue

[1] ruby-i18n/i18n#365 (comment)
voodoorai2000 added a commit to AyuntamientoMadrid/consul that referenced this issue Oct 10, 2018
There where two issues with the current implementation:

- There was a possible duplication between looking up the language name in key "locale" and in key "i18n.language.name"

- The "default" option was not being picked up, as the fallback always returned the default locale's translation, "English"

With this implementation there is only a single place to put the language name: i18n.language.name. I think this place is easier to find and understand for Crowdin translators than a "locale" key hidden in general.yml

If the translation is not found we display the language key, instead of English, which makes more sense to me too 😌

Solution based on recent comments[1] on a related I18n issue

[1] ruby-i18n/i18n#365 (comment)
@jeffjyang
Copy link
Contributor

👋 Hi everyone, is this something we still want to add to the library? If so I've opened a PR for it, let me know what you guys think :)

#482

cc: @radar

@toncid
Copy link
Author

toncid commented Jan 20, 2020

From a dev perspective, this is exactly what I need. :)

@toncid
Copy link
Author

toncid commented Jan 22, 2020

#482 was merged into master. Closing...

@toncid toncid closed this as completed Jan 22, 2020
peteryates added a commit to x-govuk/govuk-form-builder that referenced this issue Apr 30, 2020
When Rails is configured with `config.i18n.fallbacks = true`[0] the
behaviour of `I18n.exists?` is unclear[1]. It returns true when passed
`nil` because fallbacks are considered.

This could be fixed by specifying `fallback: false` as a keyword arg to
`I18n.exists?`[2] but if we have no key there's no point in proceeding to
the check stage.

[0] https://guides.rubyonrails.org/v6.0.0/configuring.html#configuring-i18n
[1] ruby-i18n/i18n#365
[2] ruby-i18n/i18n#482
@Aryk
Copy link

Aryk commented Oct 1, 2022

How can one get the language for the key after the fallbacks are applied? For example, if I want to see what language it falls back to, how could I do that?

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

5 participants