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

Dup frozen strings before force_encoding #196

Conversation

jrafanie
Copy link
Contributor

I18n 1.13.0 changed the behavior of interpolate to retain the original object when no interpolation occurs:

https://github.com/ruby-i18n/i18n/pull/649/files

With that change, a frozen string is no longer duplicated by i18n and retains the frozen state. This causes an error when we try to force_encoding as it's a modification in place. This worked previously because i18n interpolate would return a new unfrozen string from a frozen string input.

irb(main):003:0> I18n::VERSION
=> "1.13.0"
irb(main):004:0> I18n.interpolate(("get".freeze), {}).frozen?
=> true

vs.

irb(main):002:0> I18n::VERSION
=> "1.12.0"
irb(main):003:0> I18n.interpolate(("get".freeze), {}).frozen?
=> false

@@ -23,7 +23,12 @@ def translate(locale, key, options)
interpolate(translation, options)
else
result = backend.translate(locale, key, options)
(RUBY19 and result.is_a?(String)) ? result.dup.force_encoding("UTF-8") : result
if RUBY19 && result.is_a?(String)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can delete all the if RUBY19

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RUBY19 cleanup can be it's own PR

On it's way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I18n 1.13.0 changed the behavior of interpolate to retain the original
object when no interpolation occurs:

https://github.com/ruby-i18n/i18n/pull/649/files

With that change, a frozen string is no longer duplicated by i18n and
retains the frozen state.  This causes an error when we try to force_encoding as
it's a modification in place.  This worked previously because i18n interpolate
would return a new unfrozen string from a frozen string input.

```ruby
irb(main):003:0> I18n::VERSION
=> "1.13.0"
irb(main):004:0> I18n.interpolate(("get".freeze), {}).frozen?
=> true
```

vs.

```ruby
irb(main):002:0> I18n::VERSION
=> "1.12.0"
irb(main):003:0> I18n.interpolate(("get".freeze), {}).frozen?
=> false
```
@jrafanie jrafanie force-pushed the do_not_force_encoding_in_place_to_play_well_with_i18n_1_13_0 branch from beb8cd2 to 84ea65e Compare April 26, 2023 18:26
@jrafanie jrafanie changed the title Dup a possibly frozen string before force_encoding Dup frozen strings before force_encoding Apr 26, 2023
Copy link
Owner

@grosser grosser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks right!

@grosser grosser merged commit b567b46 into grosser:master Apr 26, 2023
@grosser
Copy link
Owner

grosser commented Apr 26, 2023

RUBY19 cleanup can be it's own PR

@grosser
Copy link
Owner

grosser commented Apr 26, 2023

1.10.1 thanks!

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

Successfully merging this pull request may close these issues.

2 participants