Skip to content

Commit

Permalink
Add a config variable that allows user to disable text strip
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandomm committed Dec 5, 2015
1 parent 0f96596 commit 2f64ac2
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ Tolk.config do |config|

config.primary_locale_name = 'de'
# primary locale to not be overriden by default locale in development mode.

config.strip_texts = false
# Don't strip translation texts automatically
end
```

Expand Down
2 changes: 1 addition & 1 deletion app/models/tolk/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def text=(value)
end
super unless value == text
else
value = value.strip if value.is_a?(String)
value = value.strip if value.is_a?(String) && Tolk.config.strip_texts
super unless value.to_s == text
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/tolk/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class << self
# reject files of type xxx.en.yml when syncing locales
attr_accessor :block_xxx_en_yml_locale_files

# strip translation texts automatically
attr_accessor :strip_texts

def reset
@exclude_gems_token = false

@strip_texts = true

@block_xxx_en_yml_locale_files = true # keep compat with older versions

@dump_path = Proc.new { "#{Rails.application.root}/config/locales" }
Expand Down
1 change: 1 addition & 0 deletions test/unit/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class ConfigTest < ActiveSupport::TestCase
assert_equal "#{Rails.root}/config/locales", Tolk::Export.dump_path
assert Tolk.config.mapping.keys.include?('ar')
assert_equal 'Arabic',Tolk.config.mapping['ar']
assert_equal true, Tolk.config.strip_texts
end
end
9 changes: 9 additions & 0 deletions test/unit/translation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ def setup
assert_equal(text.strip, Tolk::Translation.new(:text => text).value)
end

test "translation with string value with leading and trailing whitespace but with strip_texts config disabled" do
Tolk.config.strip_texts = false

text = "\t Hello World \r\n"
assert_equal(text, Tolk::Translation.new(:text => text).value)

Tolk.config.strip_texts = true
end

test "translation with string value with variables" do
text = "{{attribute}} {{message}}"
assert_equal(text, Tolk::Translation.new(:text => text).value)
Expand Down

0 comments on commit 2f64ac2

Please sign in to comment.