From 7a412b7016b140e4733b8bd5c8cc56688d3efb64 Mon Sep 17 00:00:00 2001 From: Patrik Rak Date: Wed, 17 May 2017 13:20:57 +0200 Subject: [PATCH 1/2] Set filters correctly when merging translations. --- r18n-core/lib/r18n-core/translation.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r18n-core/lib/r18n-core/translation.rb b/r18n-core/lib/r18n-core/translation.rb index 5106cef0..ff439155 100644 --- a/r18n-core/lib/r18n-core/translation.rb +++ b/r18n-core/lib/r18n-core/translation.rb @@ -93,7 +93,7 @@ def merge!(translations, locale) when String c = { locale: locale, path: path } v = @filters.process_string(:passive, value, c, []) - value = TranslatedString.new(v, locale, path, @filter) + value = TranslatedString.new(v, locale, path, @filters) when Typed value.locale = locale value.path = path From cba4210af0fa5e112e0be8354daa2946116f9b88 Mon Sep 17 00:00:00 2001 From: Patrik Rak Date: Wed, 17 May 2017 14:35:11 +0200 Subject: [PATCH 2/2] Make TranslatedString work as normal strings whose #to_s method always returns String. Before this fix, calling for example TranslatedString#split would create TranslatedStrings whose #to_s method would return nil. --- r18n-core/lib/r18n-core/translated_string.rb | 8 +++----- r18n-core/spec/translation_spec.rb | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/r18n-core/lib/r18n-core/translated_string.rb b/r18n-core/lib/r18n-core/translated_string.rb index 1145ff8d..426989d4 100644 --- a/r18n-core/lib/r18n-core/translated_string.rb +++ b/r18n-core/lib/r18n-core/translated_string.rb @@ -29,11 +29,9 @@ class TranslatedString < String # Returns a new string object containing a copy of +str+, which translated # for +path+ to +locale+ def initialize(value, locale, path, filters = nil) - value = value.to_s if value.is_a? TranslatedString - super(value) + super(value.to_s) @filters = filters @locale = locale - @value = value @path = path end @@ -55,9 +53,9 @@ def html_safe? # Override to_s to make string html safe if `html_safe` method is defined. def to_s if respond_to? :html_safe - @value.html_safe + super.html_safe else - @value + String.new(super) end end diff --git a/r18n-core/spec/translation_spec.rb b/r18n-core/spec/translation_spec.rb index 0bee9318..22144fc8 100644 --- a/r18n-core/spec/translation_spec.rb +++ b/r18n-core/spec/translation_spec.rb @@ -18,6 +18,21 @@ expect(i18n.one | 'default').to eq('One') end + it "returns strings which can be used as normal strings" do + i18n = R18n::I18n.new('en', DIR) + expect(i18n.not.exists).not_to be_translated + expect(i18n.not.exists.to_s).to be_kind_of(String) + expect(i18n.not.exists.to_s.split.first).to be_kind_of(String) + expect(i18n.not.exists.to_s.split.first.to_s).to be_kind_of(String) + + expect(i18n.one).to be_translated + expect(i18n.one.to_s).to be_kind_of(String) + expect(i18n.one.to_s.split.first).to be_kind_of(String) + expect(i18n.one.to_s.split.first.to_s).to be_kind_of(String) + expect(i18n.one.split.first).to be_kind_of(String) + expect(i18n.one.split.first.to_s).to be_kind_of(String) + end + it "returns html escaped string" do klass = Class.new(R18n::TranslatedString) do def html_safe