From 7cf9f36e228323432798054dcbb768cec47cdf2b Mon Sep 17 00:00:00 2001 From: Alexandre Podlewski Date: Wed, 18 Oct 2023 16:32:08 +0200 Subject: [PATCH 1/4] Make wording value stripping controlled by a flag in export_request --- lib/ad_localize/parsers/csv_parser.rb | 3 ++- lib/ad_localize/requests/export_request.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/ad_localize/parsers/csv_parser.rb b/lib/ad_localize/parsers/csv_parser.rb index b45883a..284bdcd 100644 --- a/lib/ad_localize/parsers/csv_parser.rb +++ b/lib/ad_localize/parsers/csv_parser.rb @@ -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 diff --git a/lib/ad_localize/requests/export_request.rb b/lib/ad_localize/requests/export_request.rb index a7176b5..cebff07 100644 --- a/lib/ad_localize/requests/export_request.rb +++ b/lib/ad_localize/requests/export_request.rb @@ -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'), @@ -23,6 +24,7 @@ class ExportRequest :locales, :bypass_empty_values, :auto_escape_percent, + :skip_value_stripping, :csv_paths, :merge_policy, :output_path, @@ -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] @@ -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 @@ -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}, " \ From f368b8132e56f0e4f334b4e17ff400366a290a6c Mon Sep 17 00:00:00 2001 From: Alexandre Podlewski Date: Wed, 18 Oct 2023 16:38:27 +0200 Subject: [PATCH 2/4] Add parameter to cli to disable striping of translated strings --- CHANGELOG.md | 4 ++++ lib/ad_localize/mappers/options_to_export_request.rb | 1 + lib/ad_localize/option_handler.rb | 2 ++ 3 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f068ad5..177dfbc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/ad_localize/mappers/options_to_export_request.rb b/lib/ad_localize/mappers/options_to_export_request.rb index 3cc13a6..53644ef 100644 --- a/lib/ad_localize/mappers/options_to_export_request.rb +++ b/lib/ad_localize/mappers/options_to_export_request.rb @@ -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'] diff --git a/lib/ad_localize/option_handler.rb b/lib/ad_localize/option_handler.rb index 37c9c6e..24a69da 100644 --- a/lib/ad_localize/option_handler.rb +++ b/lib/ad_localize/option_handler.rb @@ -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], @@ -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 From 89be12a89fee8e235659d63c49c07340f753fa34 Mon Sep 17 00:00:00 2001 From: Alexandre Podlewski Date: Wed, 18 Oct 2023 16:40:25 +0200 Subject: [PATCH 3/4] Update README --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index cd66af9..b45816a 100644 --- a/README.md +++ b/README.md @@ -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. From 3a56f08778e93071b537aee2896fe2b7e0d921a9 Mon Sep 17 00:00:00 2001 From: Alexandre Podlewski Date: Wed, 18 Oct 2023 17:17:05 +0200 Subject: [PATCH 4/4] Create test for checking csv parser stripping --- .../reference_whitespace_stripping.csv | 5 ++ test/parsers/csv_parser_test.rb | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 test/fixtures/reference_whitespace_stripping.csv diff --git a/test/fixtures/reference_whitespace_stripping.csv b/test/fixtures/reference_whitespace_stripping.csv new file mode 100644 index 0000000..53d0b78 --- /dev/null +++ b/test/fixtures/reference_whitespace_stripping.csv @@ -0,0 +1,5 @@ +,key,en, +,no_whitespaces,no_whitespaces, +, before, before, +,after ,after , +, both , both , diff --git a/test/parsers/csv_parser_test.rb b/test/parsers/csv_parser_test.rb index c360e40..864591c 100644 --- a/test/parsers/csv_parser_test.rb +++ b/test/parsers/csv_parser_test.rb @@ -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