Skip to content

Commit

Permalink
Merge pull request #107 from lassebunk/allow-blank-translations
Browse files Browse the repository at this point in the history
Allow blank translations. Fixes #101
  • Loading branch information
rsl committed Apr 29, 2013
2 parents 7e0456e + a329fcb commit 26883df
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/stringex/localization.rb
Expand Up @@ -37,7 +37,7 @@ def translate(scope, key, options = {})

translation = initial_translation(scope, key, locale)

return translation unless translation.to_s.empty?
return translation unless translation.nil?

if locale != default_locale
translate scope, key, options.merge(:locale => default_locale)
Expand Down
5 changes: 4 additions & 1 deletion lib/stringex/localization/backend/i18n.rb
Expand Up @@ -30,7 +30,10 @@ def store_translations(locale, scope, data)
end

def initial_translation(scope, key, locale)
::I18n.translate(key, :scope => [:stringex, scope], :locale => locale, :default => "")
# I18n can't return a nil as default as this gets interpreted as if no default
# is specified, so we use a string instead.
translated = ::I18n.translate(key, :scope => [:stringex, scope], :locale => locale, :default => "__default__")
translated == "__default__" ? nil : translated
end

def load_translations(locale = nil)
Expand Down
2 changes: 1 addition & 1 deletion locales/da.yml
Expand Up @@ -39,7 +39,7 @@ da:
frac14: en fjerdedel
frac12: halv
frac34: tre fjerdedele
gt: >
gt: ">"
lt: <
nbsp: " "
pound: " pund "
Expand Down
2 changes: 1 addition & 1 deletion locales/en.yml
Expand Up @@ -39,7 +39,7 @@ en:
frac14: one fourth
frac12: half
frac34: three fourths
gt: >
gt: ">"
lt: <
nbsp: " "
pound: " pounds "
Expand Down
11 changes: 11 additions & 0 deletions test/localization_test.rb
Expand Up @@ -109,4 +109,15 @@ def test_can_translate_using_i18n
assert_equal value, Stringex::Localization.translate(:test_i18n_translation, key)
end
end

def test_allows_blank_translations
[:internal, :i18n].each do |backend|
Stringex::Localization.backend = backend

assert_equal "Test blank", "Test&nbsp;blank".convert_miscellaneous_html_entities

Stringex::Localization.store_translations :en, :html_entities, { :nbsp => "" }
assert_equal "Testblank", "Test&nbsp;blank".convert_miscellaneous_html_entities
end
end
end

0 comments on commit 26883df

Please sign in to comment.