ruby-i18n / i18n

Internationalization (i18n) library for Ruby
MIT License
976 stars 408 forks source link

Frozen hashes in Ruby 3.1 #648

Closed 23tux closed 1 year ago

23tux commented 1 year ago

I noticed when upgrading to Ruby 3.1, that this lines causes the loaded hashes to be frozen:

https://github.com/ruby-i18n/i18n/blob/32c957e413f8493ccffa66e1ecce8e47e3c9153c/lib/i18n/backend/base.rb#L248-L250

and

https://github.com/ruby-i18n/i18n/blob/32c957e413f8493ccffa66e1ecce8e47e3c9153c/lib/i18n/backend/base.rb#L264-L266

We do some manipulations to the loaded translations like parsing markdown and writing the results back to the translation hashes. This now throws a can't modify frozen Hash error when trying to set a key on that hash.

I wonder why this is? Can someone elaborate why the files are loaded with freeze: true? Is this for performance reasons?

And what would a good way of customize this? For now I can only think of monkey patching the load_yml and load_json method, even if it's very ugly and complicates future gem updates.

Or is there some hook or custom backend or anything like this, that would be better than monkey patching the method?

radar commented 1 year ago

Here's the pull request https://github.com/ruby-i18n/i18n/pull/587

msxavi commented 1 year ago

https://github.com/ruby-i18n/i18n/pull/587 didn’t fix YML, did it?

radar commented 1 year ago

That one was just for JSON. Are you seeing issues with YML too? If so, please open a different issue.