From 68b59b9e0fecc2516e9a40b3fec80cfe984ed98e Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sun, 17 Jun 2018 16:17:22 +0200 Subject: [PATCH 1/2] Fixes #255. While fixing #250 094ba47 introduced a regression that the language was no longer deducted from the given filename. --- lib/twine/formatters/abstract.rb | 1 + test/test_formatters.rb | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/twine/formatters/abstract.rb b/lib/twine/formatters/abstract.rb index 00b44e4..fd2114c 100644 --- a/lib/twine/formatters/abstract.rb +++ b/lib/twine/formatters/abstract.rb @@ -81,6 +81,7 @@ module Twine only_language_and_region = /^#{LANGUAGE_CODE_WITH_OPTIONAL_REGION_CODE}$/i basename = File.basename(path, File.extname(path)) return basename if basename =~ only_language_and_region + return basename if @twine_file.language_codes.include? basename path.split(File::SEPARATOR).reverse.find { |segment| segment =~ only_language_and_region } end diff --git a/test/test_formatters.rb b/test/test_formatters.rb index 0868211..f46d797 100644 --- a/test/test_formatters.rb +++ b/test/test_formatters.rb @@ -200,6 +200,17 @@ class TestAndroidFormatter < FormatterTest assert_equal language, @formatter.determine_language_given_path("#{language}.xml") end + def test_recognize_every_twine_language_from_filename + twine_file = build_twine_file "not-a-lang-code" do + add_section "Section" do + add_definition key: "value" + end + end + + @formatter.twine_file = twine_file + assert_equal "not-a-lang-code", @formatter.determine_language_given_path("not-a-lang-code.xml") + end + def test_deducts_language_from_resource_folder language = KNOWN_LANGUAGES.sample assert_equal language, @formatter.determine_language_given_path("res/values-#{language}") @@ -238,6 +249,17 @@ class TestAppleFormatter < FormatterTest assert_equal language, @formatter.determine_language_given_path("#{language}.strings") end + def test_recognize_every_twine_language_from_filename + twine_file = build_twine_file "not-a-lang-code" do + add_section "Section" do + add_definition key: "value" + end + end + + @formatter.twine_file = twine_file + assert_equal "not-a-lang-code", @formatter.determine_language_given_path("not-a-lang-code.strings") + end + def test_deducts_language_from_resource_folder language = %w(en de fr).sample assert_equal language, @formatter.determine_language_given_path("#{language}.lproj/Localizable.strings") From 079065da31969913f65b3a03fab428f81ce96367 Mon Sep 17 00:00:00 2001 From: Sebastian Ludwig Date: Sun, 17 Jun 2018 16:23:25 +0200 Subject: [PATCH 2/2] Add hints to error messages that might help to solve the error. --- lib/twine/runner.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/twine/runner.rb b/lib/twine/runner.rb index 3639a0e..bfba1fa 100644 --- a/lib/twine/runner.rb +++ b/lib/twine/runner.rb @@ -87,7 +87,7 @@ module Twine end unless formatter - raise Twine::Error.new "Could not determine format given the contents of #{@options[:output_path]}" + raise Twine::Error.new "Could not determine format given the contents of #{@options[:output_path]}. Try using `--format`." end file_name = @options[:file_name] || formatter.default_file_name @@ -303,7 +303,7 @@ module Twine if formatters.empty? return nil elsif formatters.size > 1 - raise Twine::Error.new("Unable to determine format. Candidates are: #{formatters.map(&:format_name).join(', ')}. Please specify the format you want using '--format'") + raise Twine::Error.new("Unable to determine format. Candidates are: #{formatters.map(&:format_name).join(', ')}. Please specify the format you want using `--format`") end formatter = formatters.first formatter.twine_file = @twine_file @@ -334,12 +334,12 @@ module Twine end unless formatter - raise Twine::Error.new "Unable to determine format of #{path}" + raise Twine::Error.new "Unable to determine format of #{path}. Try using `--format`." end lang = lang || formatter.determine_language_given_path(path) unless lang - raise Twine::Error.new "Unable to determine language for #{path}" + raise Twine::Error.new "Unable to determine language for #{path}. Try using `--lang`." end @twine_file.language_codes << lang unless @twine_file.language_codes.include? lang