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

Fix for TranslatedString to close #136 #147

Merged
merged 2 commits into from May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions r18n-core/lib/r18n-core/translated_string.rb
Expand Up @@ -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

Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion r18n-core/lib/r18n-core/translation.rb
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions r18n-core/spec/translation_spec.rb
Expand Up @@ -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
Expand Down