support translation hashes with numeric keys in Simple backend#422
support translation hashes with numeric keys in Simple backend#422radar merged 1 commit intoruby-i18n:masterfrom
Conversation
|
Hi @wjordan. Thank you for the pull request. Could you please give some more context around how you're using this in your application? Most often, people will use numbers and pair them up with some "context". A price, a label... something else: I18n.t(:price, amount: 10) => $10 / £10
I18n.t(:unread_messages, count: 2) => 2 unread messages / 2 mensajes no leídosCould you show me the context of how you're using this? |
Sure- in my case, numeric keys are being used in a YAML i18n source file for localizing collections of 'slides' in our application, which are indexed by slide number. See Without this PR, using the Simple backend We've considered converting our source YAML to explicitly cast all integer keys as strings ( This is different from the use of interpolation or pluralization features to inject numbers into the localized-string value, as in your examples. |
|
Heads up: This PR caused a regression -- see #443. It will be good to see if we can mitigate this regression by making both the new / old behaviours work. If we cannot, I will be reverting this PR. |
This will ensure compatibility with old I18n (1.1.0) (see also issue #443) and what is now expected as of 1.2.0 (see #422) I have switched this Hash class method monkey patching to use refinements, because it will keep the method overriding contained to the select places of the I18n gem where it is used.
Currently, loading a translation hash with a Numeric key (e.g.,
store_translations(:en, 1 => 'foo'); I18n.t(1)) does not work using theSimplebackend, since the implementation's call todeep_symbolize_keys(to normalize all translation keys as Symbols) does not affect Numeric types (Numeric#to_symis not implemented). This PR additionally callsdeep_stringify_keysto help normalize key types implementingto_s.This also brings the behavior of KeyValue and Simple backends slightly closer together, since numeric keys are already supported by the
KeyValuebackend.I don't expect much of a performance hit from the added
deep_stringify_keysoperation. Surprisingly in tests with my use-case, I saw load times with this PR change actually improve by 25% (from 60 seconds to 45 seconds), possibly since the call todeep_symbolize_keyswas no longer hitting theNoMethodErrorexception/rescue nilcode path for Numeric keys in my translation data.