-
-
Notifications
You must be signed in to change notification settings - Fork 410
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
Comments
My workaround for the moment is to raise and catch errors, but it would be nice for begin
I18n.t(key, locale: locale, fallback: false, raise: true)
rescue I18n::MissingTranslationData
false
else
true
end |
Interesting workaround. I am thinking of monkey-patching it into @svenfuchs any thoughts on extending |
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. |
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)
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)
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)
From a dev perspective, this is exactly what I need. :) |
#482 was merged into master. Closing... |
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
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? |
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 returnfalse
, because the key was not present in the locale and fallbacks were not considered.What actually happened
I18n.exists?
now returnstrue
, 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:
def exists?(..., skip_fallbacks = false)
def exists_in_locale?
The text was updated successfully, but these errors were encountered: