Skip to content
This repository
Browse code

Merge pull request #21 from elDub/boolean_values

Fix issue with entering boolean values
  • Loading branch information...
commit b0aa355d1381b8be0c8294541b744e7c7d0abb3e 2 parents 925fcb6 + 095d435
Thomas Darde thomasdarde authored
20 app/assets/stylesheets/tolk/screen.css
@@ -3,20 +3,20 @@ Defaults
3 3 -------------------------------------------------*/
4 4
5 5 acronym, abbr {
6   - font-variant: small-caps;
7   - text-transform: lowercase;
  6 + font-variant: small-caps;
  7 + text-transform: lowercase;
8 8 font-weight: bold;
9 9 }
10 10
11   -.center {text-align: center;}
12   -.large {font-size: larger;}
  11 +.center {text-align: center;}
  12 +.large {font-size: larger;}
13 13 .small {font-size: smaller;}
14 14 strong {font-weight: bold;}
15 15 em {font-style: italic;}
16 16
17   -.clear {clear: both;}
18   -.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
19   -.clearfix { display: inline-block; }
  17 +.clear {clear: both;}
  18 +.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
  19 +.clearfix { display: inline-block; }
20 20 .clearfix{ display: block; }
21 21
22 22 a {color: #888;}
@@ -28,8 +28,8 @@ Layout
28 28
29 29 body {
30 30 font-family: "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;
31   - background: #e5e5e5;
32   - color: #333;
  31 + background: #e5e5e5;
  32 + color: #333;
33 33 margin: 0;
34 34 padding: 0;
35 35 font-size: 14px;
@@ -288,7 +288,7 @@ table.translations .highlight {
288 288 background-color: yellow;
289 289 }
290 290
291   -table.translations .phrase .carriage_return {
  291 +table.translations .phrase .carriage_return, table.translations .phrase .boolean {
292 292 color: #2fadcf;
293 293 font-weight: bold;
294 294 }
8 app/helpers/tolk/application_helper.rb
@@ -10,14 +10,18 @@ def format_i18n_text_area_value(value)
10 10
11 11 def yaml_value(value)
12 12 if value.present?
13   - unless value.is_a?(String)
  13 + unless value.is_a?(String) || value.is_a?(TrueClass) || value.is_a?(FalseClass)
14 14 value = value.respond_to?(:ya2yaml) ? value.ya2yaml(:syck_compatible => true) : value.to_yaml
15 15 end
16 16 end
17   -
  17 +
18 18 value
19 19 end
20 20
  21 + def boolean_warning
  22 + '<span class="boolean">(Do not translate -- Enter true or false)</span>'.html_safe
  23 + end
  24 +
21 25 def tolk_locale_selection
22 26 existing_locale_names = Tolk::Locale.all.map(&:name)
23 27
37 app/models/tolk/translation.rb
@@ -5,7 +5,8 @@ class Translation < ActiveRecord::Base
5 5 scope :containing_text, lambda {|query| where("tolk_translations.text LIKE ?", "%#{query}%") }
6 6
7 7 serialize :text
8   - validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil }
  8 + serialize :previous_text
  9 + validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil && !r.boolean?}
9 10 validate :check_matching_variables, :if => proc { |tr| tr.primary_translation.present? }
10 11
11 12 validates_uniqueness_of :phrase_id, :scope => :locale_id
@@ -27,6 +28,10 @@ class Translation < ActiveRecord::Base
27 28 attr_accessor :explicit_nil
28 29 before_validation :set_explicit_nil
29 30
  31 + def boolean?
  32 + text.is_a?(TrueClass) || text.is_a?(FalseClass) || text == 't' || text == 'f'
  33 + end
  34 +
30 35 def up_to_date?
31 36 not out_of_date?
32 37 end
@@ -45,12 +50,27 @@ def primary_translation
45 50
46 51 def text=(value)
47 52 value = value.to_s if value.kind_of?(Fixnum)
48   - super unless value.to_s == text
  53 + if primary_translation && primary_translation.boolean?
  54 + value = case value.to_s.downcase.strip
  55 + when 'true', 't'
  56 + true
  57 + when 'false', 'f'
  58 + false
  59 + else
  60 + self.explicit_nil = true
  61 + nil
  62 + end
  63 + super unless value == text
  64 + else
  65 + super unless value.to_s == text
  66 + end
49 67 end
50 68
51 69 def value
52 70 if text.is_a?(String) && /^\d+$/.match(text)
53 71 text.to_i
  72 + elsif (primary_translation || self).boolean?
  73 + %w[true t].member?(text.to_s.downcase.strip)
54 74 else
55 75 text
56 76 end
@@ -100,7 +120,18 @@ def fix_text_type
100 120 end
101 121 end
102 122
103   - self.text = nil if primary_translation.text.class != self.text.class
  123 + if primary_translation.boolean?
  124 + self.text = case self.text.to_s.strip
  125 + when 'true'
  126 + true
  127 + when 'false'
  128 + false
  129 + else
  130 + nil
  131 + end
  132 + elsif primary_translation.text.class != self.text.class
  133 + self.text = nil
  134 + end
104 135 end
105 136
106 137 true
2  app/views/tolk/locales/all.html.erb
@@ -31,6 +31,7 @@
31 31 <div class="updated">
32 32 <span class="key">Updated</span>
33 33 <%= format_i18n_value(phrase.translations.primary.text) -%>
  34 + <%= boolean_warning if phrase.translations.primary.boolean? -%>
34 35 </div>
35 36 <div class="original">
36 37 <span class="key">Original</span>
@@ -38,6 +39,7 @@
38 39 </div>
39 40 <% else %>
40 41 <%= format_i18n_value(phrase.translations.primary.text) -%>
  42 + <%= boolean_warning if phrase.translations.primary.boolean? -%>
41 43 <% end %>
42 44
43 45 <span class="key"><%= phrase.key %></span>
1  app/views/tolk/locales/show.html.erb
@@ -35,6 +35,7 @@
35 35 <% else -%>
36 36 <%= format_i18n_value(phrase.translations.primary.text) -%>
37 37 <% end -%>
  38 + <%= boolean_warning if phrase.translations.primary.boolean? -%>
38 39 <span class="key" title="<%= phrase.key %>"><%= truncate(phrase.key, :length => 100) %></span>
39 40 </td>
40 41 </tr>
1  app/views/tolk/searches/show.html.erb
@@ -32,6 +32,7 @@
32 32 <% else -%>
33 33 <%= format_i18n_value(phrase.translations.primary.text) -%>
34 34 <% end -%>
  35 + <%= boolean_warning if phrase.translations.primary.boolean? -%>
35 36 <span class="key" title="<%= phrase.key %>"><%= params[:k].present? ?
36 37 highlight(h(truncate(phrase.key, :length => 100)), params[:k]) :
37 38 h(truncate(phrase.key, :length => 100)) %></span>
6 test/fixtures/tolk_phrases.yml
@@ -16,4 +16,8 @@ cozy:
16 16
17 17 human_format_precision:
18 18 id: 5
19   - key: number.human.format.precision
  19 + key: number.human.format.precision
  20 +
  21 +number_currency_format_significant:
  22 + id: 6
  23 + key: number.currency.format.significant
24 test/fixtures/tolk_translations.yml
... ... @@ -1,24 +1,24 @@
1 1 hello_world_en:
2 2 id: 1
3   - phrase_id: 1
  3 + phrase_id: 1
4 4 locale_id: 1
5 5 text: Hello World
6 6
7 7 hello_world_da:
8 8 id: 2
9   - phrase_id: 1
  9 + phrase_id: 1
10 10 locale_id: 2
11 11 text: Hej Verden
12 12
13 13 hello_world_se:
14 14 id: 3
15   - phrase_id: 1
  15 + phrase_id: 1
16 16 locale_id: 3
17 17 text: Hejsan Verdon
18 18
19 19 nested_hello_world_en:
20 20 id: 4
21   - phrase_id: 2
  21 + phrase_id: 2
22 22 locale_id: 1
23 23 text: Nested Hello World
24 24
@@ -30,7 +30,7 @@ nested_hello_country_en:
30 30
31 31 nested_hello_world_da:
32 32 id: 6
33   - phrase_id: 2
  33 + phrase_id: 2
34 34 locale_id: 2
35 35 text: Nedarvet Hej Verden
36 36
@@ -39,9 +39,21 @@ cozy_da:
39 39 phrase_id: 4
40 40 locale_id: 2
41 41 text: Hyggeligt
42   -
  42 +
43 43 human_format_precision_en:
44 44 id: 8
45 45 phrase_id: 5
46 46 locale_id: 1
47 47 text: "1"
  48 +
  49 +number_currency_format_significant_en:
  50 + id: 9
  51 + phrase_id: 6
  52 + locale_id: 1
  53 + text: false
  54 +
  55 +number_currency_format_significant_da:
  56 + id: 10
  57 + phrase_id: 6
  58 + locale_id: 2
  59 + text: true
4 test/locales/basic/da.yml
@@ -4,3 +4,7 @@ da:
4 4 hello_world: "Hej Verden"
5 5 nested:
6 6 hello_world: "Nedarvet Hej Verden"
  7 + number:
  8 + currency:
  9 + format:
  10 + significant: true
19 test/unit/locale_test.rb
@@ -9,12 +9,12 @@ class LocaleTest < ActiveSupport::TestCase
9 9
10 10 test "turning locale with nested phrases into a hash" do
11 11 assert_equal({ "en" => {
12   - "number"=>{"human"=>{"format"=>{"precision"=>1}}},
13 12 "hello_world" => "Hello World",
14 13 "nested" => {
15 14 "hello_world" => "Nested Hello World",
16 15 "hello_country" => "Nested Hello Country"
17   - }
  16 + },
  17 + "number"=>{"human"=>{"format"=>{"precision"=>1}},"currency"=>{"format"=>{"significant"=>false}}}
18 18 }}, tolk_locales(:en).to_hash)
19 19 end
20 20
@@ -34,18 +34,21 @@ class LocaleTest < ActiveSupport::TestCase
34 34 assert_equal [4, 3], page1.map(&:id)
35 35
36 36 page2 = locale.phrases_without_translation(2)
37   - assert_equal [2, 5], page2.map(&:id)
  37 + assert_equal [2, 6], page2.map(&:id)
38 38
39 39 page3 = locale.phrases_without_translation(3)
40   - assert page3.blank?
  40 + assert_equal [5], page3.map(&:id)
  41 +
  42 + page4 = locale.phrases_without_translation(4)
  43 + assert page4.blank?
41 44 end
42 45
43 46 test "paginating phrases with translations" do
44   - Tolk::Phrase.per_page = 4
  47 + Tolk::Phrase.per_page = 5
45 48 locale = tolk_locales(:en)
46 49
47 50 page1 = locale.phrases_with_translation
48   - assert_equal [1, 3, 2, 5], page1.map(&:id)
  51 + assert_equal [1, 3, 2, 6, 5], page1.map(&:id)
49 52
50 53 page2 = locale.phrases_with_translation(2)
51 54 assert page2.blank?
@@ -53,7 +56,7 @@ class LocaleTest < ActiveSupport::TestCase
53 56
54 57 test "counting missing translations" do
55 58 assert_equal 2, tolk_locales(:da).count_phrases_without_translation
56   - assert_equal 4, tolk_locales(:se).count_phrases_without_translation
  59 + assert_equal 5, tolk_locales(:se).count_phrases_without_translation
57 60 end
58 61
59 62 test "dumping all locales to yml" do
@@ -81,5 +84,5 @@ class LocaleTest < ActiveSupport::TestCase
81 84 assert_equal 'English', tolk_locales(:en).language_name
82 85 assert_equal 'pirate', Tolk::Locale.new(:name => 'pirate').language_name
83 86 end
84   -
  87 +
85 88 end
8 test/unit/translation_test.rb
@@ -37,6 +37,14 @@ def setup
37 37 assert_equal(1, tolk_translations(:human_format_precision_en).value)
38 38 end
39 39
  40 + test "translation with true value" do
  41 + assert_equal(true, tolk_translations(:number_currency_format_significant_da).value)
  42 + end
  43 +
  44 + test "translation with false value" do
  45 + assert_equal(false, tolk_translations(:number_currency_format_significant_en).value)
  46 + end
  47 +
40 48 test "translation with hash value" do
41 49 hash = {:foo => "bar"}
42 50 assert_equal(hash, Tolk::Translation.new(:text => hash).value)

0 comments on commit b0aa355

Please sign in to comment.
Something went wrong with that request. Please try again.