Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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
View
@@ -3,20 +3,20 @@ Defaults
-------------------------------------------------*/
acronym, abbr {
- font-variant: small-caps;
- text-transform: lowercase;
+ font-variant: small-caps;
+ text-transform: lowercase;
font-weight: bold;
}
-.center {text-align: center;}
-.large {font-size: larger;}
+.center {text-align: center;}
+.large {font-size: larger;}
.small {font-size: smaller;}
strong {font-weight: bold;}
em {font-style: italic;}
-.clear {clear: both;}
-.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
-.clearfix { display: inline-block; }
+.clear {clear: both;}
+.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
+.clearfix { display: inline-block; }
.clearfix{ display: block; }
a {color: #888;}
@@ -28,8 +28,8 @@ Layout
body {
font-family: "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;
- background: #e5e5e5;
- color: #333;
+ background: #e5e5e5;
+ color: #333;
margin: 0;
padding: 0;
font-size: 14px;
@@ -288,7 +288,7 @@ table.translations .highlight {
background-color: yellow;
}
-table.translations .phrase .carriage_return {
+table.translations .phrase .carriage_return, table.translations .phrase .boolean {
color: #2fadcf;
font-weight: bold;
}
8 app/helpers/tolk/application_helper.rb
View
@@ -10,14 +10,18 @@ def format_i18n_text_area_value(value)
def yaml_value(value)
if value.present?
- unless value.is_a?(String)
+ unless value.is_a?(String) || value.is_a?(TrueClass) || value.is_a?(FalseClass)
value = value.respond_to?(:ya2yaml) ? value.ya2yaml(:syck_compatible => true) : value.to_yaml
end
end
-
+
value
end
+ def boolean_warning
+ '<span class="boolean">(Do not translate -- Enter true or false)</span>'.html_safe
+ end
+
def tolk_locale_selection
existing_locale_names = Tolk::Locale.all.map(&:name)
37 app/models/tolk/translation.rb
View
@@ -5,7 +5,8 @@ class Translation < ActiveRecord::Base
scope :containing_text, lambda {|query| where("tolk_translations.text LIKE ?", "%#{query}%") }
serialize :text
- validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil }
+ serialize :previous_text
+ validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil && !r.boolean?}
validate :check_matching_variables, :if => proc { |tr| tr.primary_translation.present? }
validates_uniqueness_of :phrase_id, :scope => :locale_id
@@ -27,6 +28,10 @@ class Translation < ActiveRecord::Base
attr_accessor :explicit_nil
before_validation :set_explicit_nil
+ def boolean?
+ text.is_a?(TrueClass) || text.is_a?(FalseClass) || text == 't' || text == 'f'
+ end
+
def up_to_date?
not out_of_date?
end
@@ -45,12 +50,27 @@ def primary_translation
def text=(value)
value = value.to_s if value.kind_of?(Fixnum)
- super unless value.to_s == text
+ if primary_translation && primary_translation.boolean?
+ value = case value.to_s.downcase.strip
+ when 'true', 't'
+ true
+ when 'false', 'f'
+ false
+ else
+ self.explicit_nil = true
+ nil
+ end
+ super unless value == text
+ else
+ super unless value.to_s == text
+ end
end
def value
if text.is_a?(String) && /^\d+$/.match(text)
text.to_i
+ elsif (primary_translation || self).boolean?
+ %w[true t].member?(text.to_s.downcase.strip)
else
text
end
@@ -100,7 +120,18 @@ def fix_text_type
end
end
- self.text = nil if primary_translation.text.class != self.text.class
+ if primary_translation.boolean?
+ self.text = case self.text.to_s.strip
+ when 'true'
+ true
+ when 'false'
+ false
+ else
+ nil
+ end
+ elsif primary_translation.text.class != self.text.class
+ self.text = nil
+ end
end
true
2  app/views/tolk/locales/all.html.erb
View
@@ -31,6 +31,7 @@
<div class="updated">
<span class="key">Updated</span>
<%= format_i18n_value(phrase.translations.primary.text) -%>
+ <%= boolean_warning if phrase.translations.primary.boolean? -%>
</div>
<div class="original">
<span class="key">Original</span>
@@ -38,6 +39,7 @@
</div>
<% else %>
<%= format_i18n_value(phrase.translations.primary.text) -%>
+ <%= boolean_warning if phrase.translations.primary.boolean? -%>
<% end %>
<span class="key"><%= phrase.key %></span>
1  app/views/tolk/locales/show.html.erb
View
@@ -35,6 +35,7 @@
<% else -%>
<%= format_i18n_value(phrase.translations.primary.text) -%>
<% end -%>
+ <%= boolean_warning if phrase.translations.primary.boolean? -%>
<span class="key" title="<%= phrase.key %>"><%= truncate(phrase.key, :length => 100) %></span>
</td>
</tr>
1  app/views/tolk/searches/show.html.erb
View
@@ -32,6 +32,7 @@
<% else -%>
<%= format_i18n_value(phrase.translations.primary.text) -%>
<% end -%>
+ <%= boolean_warning if phrase.translations.primary.boolean? -%>
<span class="key" title="<%= phrase.key %>"><%= params[:k].present? ?
highlight(h(truncate(phrase.key, :length => 100)), params[:k]) :
h(truncate(phrase.key, :length => 100)) %></span>
6 test/fixtures/tolk_phrases.yml
View
@@ -16,4 +16,8 @@ cozy:
human_format_precision:
id: 5
- key: number.human.format.precision
+ key: number.human.format.precision
+
+number_currency_format_significant:
+ id: 6
+ key: number.currency.format.significant
24 test/fixtures/tolk_translations.yml
View
@@ -1,24 +1,24 @@
hello_world_en:
id: 1
- phrase_id: 1
+ phrase_id: 1
locale_id: 1
text: Hello World
hello_world_da:
id: 2
- phrase_id: 1
+ phrase_id: 1
locale_id: 2
text: Hej Verden
hello_world_se:
id: 3
- phrase_id: 1
+ phrase_id: 1
locale_id: 3
text: Hejsan Verdon
nested_hello_world_en:
id: 4
- phrase_id: 2
+ phrase_id: 2
locale_id: 1
text: Nested Hello World
@@ -30,7 +30,7 @@ nested_hello_country_en:
nested_hello_world_da:
id: 6
- phrase_id: 2
+ phrase_id: 2
locale_id: 2
text: Nedarvet Hej Verden
@@ -39,9 +39,21 @@ cozy_da:
phrase_id: 4
locale_id: 2
text: Hyggeligt
-
+
human_format_precision_en:
id: 8
phrase_id: 5
locale_id: 1
text: "1"
+
+number_currency_format_significant_en:
+ id: 9
+ phrase_id: 6
+ locale_id: 1
+ text: false
+
+number_currency_format_significant_da:
+ id: 10
+ phrase_id: 6
+ locale_id: 2
+ text: true
4 test/locales/basic/da.yml
View
@@ -4,3 +4,7 @@ da:
hello_world: "Hej Verden"
nested:
hello_world: "Nedarvet Hej Verden"
+ number:
+ currency:
+ format:
+ significant: true
19 test/unit/locale_test.rb
View
@@ -9,12 +9,12 @@ 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",
"hello_country" => "Nested Hello Country"
- }
+ },
+ "number"=>{"human"=>{"format"=>{"precision"=>1}},"currency"=>{"format"=>{"significant"=>false}}}
}}, tolk_locales(:en).to_hash)
end
@@ -34,18 +34,21 @@ class LocaleTest < ActiveSupport::TestCase
assert_equal [4, 3], page1.map(&:id)
page2 = locale.phrases_without_translation(2)
- assert_equal [2, 5], page2.map(&:id)
+ assert_equal [2, 6], page2.map(&:id)
page3 = locale.phrases_without_translation(3)
- assert page3.blank?
+ assert_equal [5], page3.map(&:id)
+
+ page4 = locale.phrases_without_translation(4)
+ assert page4.blank?
end
test "paginating phrases with translations" do
- Tolk::Phrase.per_page = 4
+ Tolk::Phrase.per_page = 5
locale = tolk_locales(:en)
page1 = locale.phrases_with_translation
- assert_equal [1, 3, 2, 5], page1.map(&:id)
+ assert_equal [1, 3, 2, 6, 5], page1.map(&:id)
page2 = locale.phrases_with_translation(2)
assert page2.blank?
@@ -53,7 +56,7 @@ class LocaleTest < ActiveSupport::TestCase
test "counting missing translations" do
assert_equal 2, tolk_locales(:da).count_phrases_without_translation
- assert_equal 4, tolk_locales(:se).count_phrases_without_translation
+ assert_equal 5, tolk_locales(:se).count_phrases_without_translation
end
test "dumping all locales to yml" do
@@ -81,5 +84,5 @@ class LocaleTest < ActiveSupport::TestCase
assert_equal 'English', tolk_locales(:en).language_name
assert_equal 'pirate', Tolk::Locale.new(:name => 'pirate').language_name
end
-
+
end
8 test/unit/translation_test.rb
View
@@ -37,6 +37,14 @@ def setup
assert_equal(1, tolk_translations(:human_format_precision_en).value)
end
+ test "translation with true value" do
+ assert_equal(true, tolk_translations(:number_currency_format_significant_da).value)
+ end
+
+ test "translation with false value" do
+ assert_equal(false, tolk_translations(:number_currency_format_significant_en).value)
+ end
+
test "translation with hash value" do
hash = {:foo => "bar"}
assert_equal(hash, Tolk::Translation.new(:text => hash).value)
Please sign in to comment.
Something went wrong with that request. Please try again.