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

Already on GitHub? Sign in to your account

Add exists? to check if a certain key exists in the translations #69

Closed
wants to merge 2 commits into
from

Conversation

Projects
None yet

We just added exists? to check if a certain key exists in the translations. Also, there was a typo in test/i18n_test.rb. Both in separate commits. :)

Collaborator

yaroslav commented Nov 19, 2010

+1 on idea (much needed), but need a better method name

Sven?..

thijsc commented Nov 19, 2010

+1

ron commented Nov 19, 2010

+1

Collaborator

josevalim commented Nov 19, 2010

+1 on the idea but the implementation needs to be improved.

Although I18n::Backend::Base implements lookup(), lookup() is not part of the API required for backends to implement, so we cannot rely on it. Instead, we can implement exists?() in I18n::Backend::Base that calls lookup() as you did and I18n.exists? would simply delegate to it. This is different because someone would still be able to implement exists?() without being forced to implement lookup.

Following this implementation, we need to add specific tests to the backend as well. Here is a good candidate to put them:

https://github.com/80beans/i18n/blob/086bb2882fa40a7e51e52bff65867ce9ba3a1063/lib/i18n/tests/basics.rb

This way we will be able to ensure that all backends will pass the exists?() specification!

Thanks!

barbolo commented Sep 22, 2011

+1

+1

+1 ... I just had to write my own method to do this!

+1 - This would be very useful.
My dirty workaround: t("translation_key", :default => 0) != 0

I was just googling for this again, and found my own workaround... so I decided it was time to submit an updated pull request 😄

@josevalim, could you please take a look at #182 and let me know if I've implemented your suggestions correctly? Thanks!

bump - If you have time, please take a look at #182 and let me know if I need to make any changes

@tigrish tigrish added a commit that referenced this pull request Aug 21, 2013

@tigrish tigrish Merge pull request #182 from ndbroadbent/exists
Updated #69 - Add exists? to check if a certain key exists in the translations
3e53950

@tigrish tigrish closed this Aug 21, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment