Skip to content

Commit

Permalink
Merge pull request #88 from faberNovel/feature/allow_not_stripping_va…
Browse files Browse the repository at this point in the history
…lues

Feature/allow not stripping values
  • Loading branch information
alexandre-pod authored Dec 12, 2023
2 parents dadc2e2 + 3a56f08 commit 699fe8d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Added

- new option `--skip-value-stripping` that disable the leading and trailing whitespaces removal on wording values. The value stripping was introduced with version 6.0.0.

## 6.1.0
### Added

Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ $ ad_localize --auto-escape-percent
$ ad_localize -l fr,en
```

* Keep extra spaces before and after translated values
```
$ ad_localize --skip-value-stripping
```

### In a Ruby program
There are many possibilities when using AdLocalize in a ruby program. You can add support to your own wording format, support other platforms, select which locales you want to export, generate wording file content without writing on the disk and many more.

Expand Down
1 change: 1 addition & 0 deletions lib/ad_localize/mappers/options_to_export_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def map(options:)
request.locales = options[:locales]
request.bypass_empty_values = options[:'non-empty-values']
request.auto_escape_percent = options[:'auto-escape-percent']
request.skip_value_stripping = options[:'skip-value-stripping']
request.csv_paths = options[:csv_paths]
request.merge_policy = options[:'merge-policy']
request.output_path = options[:'target-dir']
Expand Down
2 changes: 2 additions & 0 deletions lib/ad_localize/option_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class OptionHandler
locales: Requests::ExportRequest::DEFAULTS[:locales],
:'non-empty-values' => Requests::ExportRequest::DEFAULTS[:bypass_empty_values],
:'auto-escape-percent' => Requests::ExportRequest::DEFAULTS[:auto_escape_percent],
:'skip-value-stripping' => Requests::ExportRequest::DEFAULTS[:skip_value_stripping],
csv_paths: Requests::ExportRequest::DEFAULTS[:csv_paths],
:'merge-policy' => Requests::ExportRequest::DEFAULTS[:merge_policy],
:'target-dir' => Requests::ExportRequest::DEFAULTS[:output_path],
Expand Down Expand Up @@ -39,6 +40,7 @@ def self.parse!(options)
end
parser.on("-x", "--non-empty-values", TrueClass, 'Do not export keys with empty values (iOS only)')
parser.on("--auto-escape-percent", TrueClass, 'Add escaping for % symbol to support wording use with String formatting (iOS only)')
parser.on("--skip-value-stripping", TrueClass, 'Disable the removal of leading and trailing whitespaces on wording values')
end.parse!(options, into: args)

args[:csv_paths] = options
Expand Down
3 changes: 2 additions & 1 deletion lib/ad_localize/parsers/csv_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def build_wording(csv_path:, locales:, keys:, export_request:)
next if export_request.bypass_empty_values && value.blank?

comment = row["#{COMMENT_KEY_COLUMN_IDENTIFIER} #{locale}"]
wording[locale].add_wording(key: key, value: value&.strip, comment: comment)
value = value&.strip unless export_request.skip_value_stripping
wording[locale].add_wording(key: key, value: value, comment: comment)
end
added_keys[raw_key] = true
end
Expand Down
8 changes: 8 additions & 0 deletions lib/ad_localize/requests/export_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ExportRequest
locales: [],
bypass_empty_values: false,
auto_escape_percent: false,
skip_value_stripping: false,
csv_paths: [],
merge_policy: Interactors::MergeWordings::DEFAULT_POLICY,
output_path: Pathname.new('exports'),
Expand All @@ -23,6 +24,7 @@ class ExportRequest
:locales,
:bypass_empty_values,
:auto_escape_percent,
:skip_value_stripping,
:csv_paths,
:merge_policy,
:output_path,
Expand All @@ -38,6 +40,7 @@ def initialize
@locales = DEFAULTS[:locales]
@bypass_empty_values = DEFAULTS[:bypass_empty_values]
@auto_escape_percent = DEFAULTS[:auto_escape_percent]
@skip_value_stripping = DEFAULTS[:skip_value_stripping]
@csv_paths = DEFAULTS[:csv_paths]
@merge_policy = DEFAULTS[:merge_policy]
@output_path = DEFAULTS[:output_path]
Expand All @@ -63,6 +66,10 @@ def auto_escape_percent=(value)
@auto_escape_percent = [true, 'true'].include?(value)
end

def skip_value_stripping=(value)
@skip_value_stripping = [true, 'true'].include?(value)
end

def csv_paths=(value)
return unless value.is_a? Array

Expand Down Expand Up @@ -127,6 +134,7 @@ def to_s
"locales: #{locales}, " \
"bypass_empty_values: #{bypass_empty_values}, " \
"auto_escape_percent: #{auto_escape_percent}, " \
"skip_value_stripping: #{skip_value_stripping}, " \
"csv_paths: #{csv_paths}, " \
"merge_policy: #{merge_policy}, " \
"output_path: #{output_path}, " \
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/reference_whitespace_stripping.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
,key,en,
,no_whitespaces,no_whitespaces,
, before, before,
,after ,after ,
, both , both ,
48 changes: 48 additions & 0 deletions test/parsers/csv_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,54 @@
module AdLocalize
module Parsers
class CSVParserTest < TestCase
test 'should trim whitespace on keys and translations' do
# Given
csv_path = "test/fixtures/reference_whitespace_stripping.csv"
assert(File.exist?(csv_path), "File does not exists #{csv_path}")
parser = Parsers::CSVParser.new
export_request = Requests::ExportRequest.new

# When
wording = parser.call(csv_path: csv_path, export_request: export_request)

# Then
en_singular_wording = wording["en"].singulars

assert_equal en_singular_wording["no_whitespaces"].key.label, "no_whitespaces"
assert_equal en_singular_wording["before"].key.label, "before"
assert_equal en_singular_wording["after"].key.label, "after"
assert_equal en_singular_wording["both"].key.label, "both"

assert_equal en_singular_wording["no_whitespaces"].value, "no_whitespaces"
assert_equal en_singular_wording["before"].value, "before"
assert_equal en_singular_wording["after"].value, "after"
assert_equal en_singular_wording["both"].value, "both"
end

test 'should not trim whitespace on translation when strip is disabled' do
# Given
csv_path = "test/fixtures/reference_whitespace_stripping.csv"
assert(File.exist?(csv_path), "File does not exists #{csv_path}")
parser = Parsers::CSVParser.new
export_request = Requests::ExportRequest.new
export_request.skip_value_stripping = true

# When
wording = parser.call(csv_path: csv_path, export_request: export_request)

# Then
en_singular_wording = wording["en"].singulars

assert_equal en_singular_wording["no_whitespaces"].key.label, "no_whitespaces"
assert_equal en_singular_wording["before"].key.label, "before"
assert_equal en_singular_wording["after"].key.label, "after"
assert_equal en_singular_wording["both"].key.label, "both"

assert_equal en_singular_wording["no_whitespaces"].value, "no_whitespaces"
assert_equal en_singular_wording["before"].value, " before"
assert_equal en_singular_wording["after"].value, "after "
assert_equal en_singular_wording["both"].value, " both "
end
end
end
end

0 comments on commit 699fe8d

Please sign in to comment.