1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
require "test_helper"
require 'stringex'
require 'i18n'
class LocalizationTest < Test::Unit::TestCase
def setup
I18n.locale = :en
Stringex::Localization.reset!
end
def test_stores_translations
Stringex::Localization.backend = :internal
data = { one: "number one", two: "number two" }
Stringex::Localization.store_translations :en, :test_store, data
data.each do |key, value|
assert_equal value, Stringex::Localization.translate(:test_store, key)
end
end
def test_converts_translation_keys_to_symbols
Stringex::Localization.backend = :internal
data = { "one" => "number one", "two" => "number two" }
Stringex::Localization.store_translations :en, :test_convert, data
data.each do |key, value|
assert_equal value, Stringex::Localization.translate(:test_convert, key)
assert_equal value, Stringex::Localization.translate(:test_convert, key.to_sym)
end
end
def test_can_translate
Stringex::Localization.backend = :internal
data = { one: "number one", two: "number two" }
Stringex::Localization.store_translations :en, :test_translate, data
data.each do |key, value|
assert_equal value, Stringex::Localization.translate(:test_translate, key)
end
end
def test_can_translate_when_given_string_as_key
Stringex::Localization.backend = :internal
data = { one: "number one", two: "number two" }
Stringex::Localization.store_translations :en, :test_translate, data
data.each do |key, value|
assert_equal value, Stringex::Localization.translate(:test_translate, key.to_s)
end
end
def test_returns_default_if_none_found
Stringex::Localization.backend = :internal
assert_equal "my default", Stringex::Localization.translate(:test_default, :nonexistent, default: "my default")
end
def test_returns_nil_if_no_default
Stringex::Localization.backend = :internal
assert_nil Stringex::Localization.translate(:test_no_default, :nonexistent)
end
def test_falls_back_to_default_locale
Stringex::Localization.backend = :internal
Stringex::Localization.default_locale = :es
Stringex::Localization.locale = :da
data = { "one" => "number one", "two" => "number two" }
Stringex::Localization.store_translations :es, :test_default_locale, data
data.each do |key, value|
assert_equal value, Stringex::Localization.translate(:test_default_locale, key)
end
end
def test_with_locale
Stringex::Localization.backend = :internal
Stringex::Localization.locale = :fr
assert_equal :fr, Stringex::Localization.locale
locale_set_in_block = nil
Stringex::Localization.with_locale :da do
locale_set_in_block = Stringex::Localization.locale
end
assert_equal :da, locale_set_in_block
assert_equal :fr, Stringex::Localization.locale
end
def test_stores_translations_in_i18n
Stringex::Localization.backend = :i18n
data = { one: "number one", two: "number two" }
Stringex::Localization.store_translations :en, :test_i18n_store, data
data.each do |key, value|
assert_equal value, I18n.translate("stringex.test_i18n_store.#{key}")
end
end
def test_can_translate_using_i18n
Stringex::Localization.backend = :i18n
data = { one: "number one", two: "number two" }
I18n.backend.store_translations :en, { stringex: { test_i18n_translation: data } }
data.each do |key, value|
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 blank".convert_miscellaneous_html_entities
Stringex::Localization.store_translations :en, :html_entities, { nbsp: "" }
assert_equal "Testblank", "Test blank".convert_miscellaneous_html_entities
end
end
def test_assigns_locale_in_i18n_backend
if other_locale = I18n.available_locales.find{|locale| ![:en, :de].include?(locale)}
I18n.locale = :en
Stringex::Localization.backend = :i18n
assert_equal :en, Stringex::Localization.locale
I18n.locale = other_locale
assert_equal other_locale, Stringex::Localization.locale
Stringex::Localization.locale = :de
assert_equal :de, Stringex::Localization.locale
assert_equal other_locale, I18n.locale
Stringex::Localization.locale = nil
assert_equal other_locale, Stringex::Localization.locale
assert_equal other_locale, I18n.locale
else
flunk "No I18n locales are available except :de and :en so test will not work"
end
end
def test_enforce_available_locales_default
return unless I18n.respond_to?(:enforce_available_locales)
Stringex::Localization.backend = :i18n
assert_not_nil I18n.enforce_available_locales
'Some String'.to_url
assert_not_nil I18n.enforce_available_locales
end
def test_respects_user_enforce_available_locales_setting
return unless I18n.respond_to?(:enforce_available_locales)
Stringex::Localization.backend = :i18n
I18n.enforce_available_locales = false
'Some String'.to_url
assert_equal false, I18n.enforce_available_locales
end
end
|