Skip to content

Commit

Permalink
Merge pull request #196 from jrafanie/do_not_force_encoding_in_place_…
Browse files Browse the repository at this point in the history
…to_play_well_with_i18n_1_13_0

Dup frozen strings before force_encoding
  • Loading branch information
grosser authored Apr 26, 2023
2 parents f8dd9c4 + 84ea65e commit b567b46
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/gettext_i18n_rails/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.force_encoding("UTF-8") : result
if RUBY19 && result.is_a?(String)
result = result.dup if result.frozen?
result.force_encoding("UTF-8")
else
result
end
end
end
end
Expand Down
7 changes: 7 additions & 0 deletions spec/gettext_i18n_rails/backend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
subject.translate('xx', 'c', {}).should == 'd'
end

it "passes non-gettext keys to default backend without modifying frozen translation" do
subject.backend.should_receive(:translate).with('xx', 'c', {}).and_return 'd'.freeze
# TODO track down why this is called 3 times on 1.8 (only 1 time on 1.9)
FastGettext.stub(:current_repository).and_return 'a'=>'b'
subject.translate('xx', 'c', {}).should == 'd'
end

it 'temporarily sets the given locale' do
FastGettext.should_receive(:set_locale).with('xx').and_return('xy')
FastGettext.should_receive(:set_locale).with('xy').and_return('xx')
Expand Down

0 comments on commit b567b46

Please sign in to comment.