Browse files

Introduced Tolk::Translation#value to accommodate numeric translation…

…s like number.human.format.precision
  • Loading branch information...
1 parent abbf171 commit c6dba7602a6db1e8a715563b5c2965d21080405e Craig Davey committed Apr 22, 2010
View
4 app/models/tolk/locale.rb
@@ -137,9 +137,9 @@ def search_phrases_without_translation(query, page = nil, options = {})
def to_hash
{ name => translations.each_with_object({}) do |translation, locale|
if translation.phrase.key.include?(".")
- locale.deep_merge!(unsquish(translation.phrase.key, translation.text))
+ locale.deep_merge!(unsquish(translation.phrase.key, translation.value))
else
- locale[translation.phrase.key] = translation.text
+ locale[translation.phrase.key] = translation.value
end
end }
end
View
8 app/models/tolk/translation.rb
@@ -25,6 +25,14 @@ def primary_translation
end
end
end
+
+ def text=(value)
+ super unless value.to_s == text
+ end
+
+ def value
+ YAML.load text
+ end
private
View
4 test/fixtures/tolk_phrases.yml
@@ -13,3 +13,7 @@ nested_hello_country:
cozy:
id: 4
key: cozy
+
+human_format_precision:
+ id: 5
+ key: number.human.format.precision
View
8 test/fixtures/tolk_translations.yml
@@ -38,4 +38,10 @@ cozy_da:
id: 7
phrase_id: 4
locale_id: 2
- text: Hyggeligt
+ text: Hyggeligt
+
+human_format_precision_en:
+ id: 8
+ phrase_id: 5
+ locale_id: 1
+ text: "1"
View
2 test/test_helper.rb
@@ -18,5 +18,5 @@ class ActiveSupport::TestCase
fixtures :all
- self.fixture_class_names = {:tolk_locales => 'Tolk::Locale', :tolk_phrases => 'Tolk::Phrase', :tolk_translation => 'Tolk::Translation'}
+ self.fixture_class_names = {:tolk_locales => 'Tolk::Locale', :tolk_phrases => 'Tolk::Phrase', :tolk_translations => 'Tolk::Translation'}
end
View
11 test/unit/locale_test.rb
@@ -7,6 +7,7 @@ class LocaleTest < ActiveSupport::TestCase
test "turning locale with nested phrases into a hash" do
assert_equal({ "en" => {
+ "number"=>{"human"=>{"format"=>{"precision"=>1}}},
"hello_world" => "Hello World",
"nested" => {
"hello_world" => "Nested Hello World",
@@ -32,26 +33,26 @@ class LocaleTest < ActiveSupport::TestCase
assert_equal [4, 3], page1.map(&:id)
page2 = locale.phrases_without_translation(2)
- assert_equal [2], page2.map(&:id)
+ assert_equal [2, 5], page2.map(&:id)
page3 = locale.phrases_without_translation(3)
assert page3.blank?
end
test "paginating phrases with translations" do
- Tolk::Phrase.per_page = 3
+ Tolk::Phrase.per_page = 4
locale = tolk_locales(:en)
page1 = locale.phrases_with_translation
- assert_equal [1, 3, 2], page1.map(&:id)
+ assert_equal [1, 3, 2, 5], page1.map(&:id)
page2 = locale.phrases_with_translation(2)
assert page2.blank?
end
test "counting missing translations" do
- assert_equal 1, tolk_locales(:da).count_phrases_without_translation
- assert_equal 3, tolk_locales(:se).count_phrases_without_translation
+ assert_equal 2, tolk_locales(:da).count_phrases_without_translation
+ assert_equal 4, tolk_locales(:se).count_phrases_without_translation
end
test "dumping all locales to yml" do
View
33 test/unit/translation_test.rb
@@ -0,0 +1,33 @@
+require 'test_helper'
+require 'fileutils'
+
+class TranslationTest < ActiveSupport::TestCase
+
+ def setup
+ Tolk::Locale.primary_locale(true)
+ end
+
+ test "translation is inavlid when a duplicate exists" do
+ translation = Tolk::Translation.new :phrase => tolk_translations(:hello_world_da).phrase, :locale => tolk_translations(:hello_world_da).locale
+ translation.text = "Revised Hello World"
+ assert translation.invalid?
+ assert translation.errors.on(:phrase_id)
+ end
+
+ test "translation is not changed when text is assigned an equal value in numberic form" do
+ translation = tolk_translations(:human_format_precision_en)
+ assert_equal "1", translation.text
+ translation.text = "1"
+ assert_equal false, translation.changed?
+ translation.text = 1
+ assert_equal false, translation.changed?
+ end
+
+ test "translation with string value" do
+ assert_equal "Hello World", tolk_translations(:hello_world_en).value
+ end
+
+ test "translation with numeric value" do
+ assert_equal 1, tolk_translations(:human_format_precision_en).value
+ end
+end

0 comments on commit c6dba76

Please sign in to comment.