From 832762e92d9a1408b83bf8c6248512b7bfe94958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Hoste?= Date: Fri, 9 Feb 2024 15:55:08 +0100 Subject: [PATCH] Better bugfix for empty YAML files (only the language prefix) --- .../client/init_operation/create_yaml_po_files_step.rb | 2 +- .../client/sync_operation/create_yaml_pot_file_step.rb | 2 +- lib/translation_io/yaml_conversion.rb | 2 +- .../init_operation/create_yaml_po_files_step_spec.rb | 9 ++++++++- .../sync_operation/create_yaml_pot_file_step_spec.rb | 7 +++++++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/translation_io/client/init_operation/create_yaml_po_files_step.rb b/lib/translation_io/client/init_operation/create_yaml_po_files_step.rb index e2ac079..7a4abe3 100644 --- a/lib/translation_io/client/init_operation/create_yaml_po_files_step.rb +++ b/lib/translation_io/client/init_operation/create_yaml_po_files_step.rb @@ -20,7 +20,7 @@ def run(params) TranslationIO.info file_path, 2, 2 file_translations = TranslationIO.yaml_load(File.read(file_path)) - unless file_translations.blank? + if file_translations.present? && file_translations.values.all? { |value| value.present? } all_translations = all_translations.deep_merge(file_translations) end end diff --git a/lib/translation_io/client/sync_operation/create_yaml_pot_file_step.rb b/lib/translation_io/client/sync_operation/create_yaml_pot_file_step.rb index 4463858..7d28304 100644 --- a/lib/translation_io/client/sync_operation/create_yaml_pot_file_step.rb +++ b/lib/translation_io/client/sync_operation/create_yaml_pot_file_step.rb @@ -16,7 +16,7 @@ def run(params) TranslationIO.info file_path, 2, 2 file_translations = TranslationIO.yaml_load(File.read(file_path)) - unless file_translations.blank? + if file_translations.present? && file_translations.values.all? { |value| value.present? } all_translations = all_translations.deep_merge(file_translations) end end diff --git a/lib/translation_io/yaml_conversion.rb b/lib/translation_io/yaml_conversion.rb index 9c2d775..378f19b 100644 --- a/lib/translation_io/yaml_conversion.rb +++ b/lib/translation_io/yaml_conversion.rb @@ -28,7 +28,7 @@ def get_flat_translations_for_yaml_file(file_path) def get_flat_translations_for_yaml_data(yaml_data) translations = TranslationIO.yaml_load(yaml_data) - if translations && translations.values.all? { |value| value.present? } + if translations.present? && translations.values.all? { |value| value.present? } return FlatHash.to_flat_hash(translations) else # loading an empty file, or file with only the language, returns false return {} diff --git a/spec/translation/client/init_operation/create_yaml_po_files_step_spec.rb b/spec/translation/client/init_operation/create_yaml_po_files_step_spec.rb index cfc0132..9e36e9b 100644 --- a/spec/translation/client/init_operation/create_yaml_po_files_step_spec.rb +++ b/spec/translation/client/init_operation/create_yaml_po_files_step_spec.rb @@ -29,7 +29,14 @@ EOS end - File.open('tmp/config/locales/fr.yml', 'wb') do |file| + File.open("tmp/config/locales/empty.en.yml", 'wb') do |file| + file.write <<-EOS +--- +en: +EOS + end + + File.open('tmp/config/locales/fr.yml', 'wb') do |file| file.write <<-EOS --- fr: diff --git a/spec/translation/client/sync_operation/create_yaml_pot_file_step_spec.rb b/spec/translation/client/sync_operation/create_yaml_pot_file_step_spec.rb index 606a5e8..ef32c4c 100644 --- a/spec/translation/client/sync_operation/create_yaml_pot_file_step_spec.rb +++ b/spec/translation/client/sync_operation/create_yaml_pot_file_step_spec.rb @@ -24,6 +24,13 @@ EOS end + File.open("#{yaml_locales_path}/empty.en.yml", 'wb') do |file| + file.write <<-EOS +--- +en: +EOS + end + source_locale = 'en' yaml_file_paths = Dir["#{yaml_locales_path}/*.yml"]