From 31faa1398cb3af146655592ffd9f47848e12b9d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Hoste?= Date: Fri, 9 Feb 2024 14:50:57 +0100 Subject: [PATCH] Fix bug when YAML is commented and only the language key is present --- lib/translation_io/yaml_conversion.rb | 4 ++-- spec/support/data/commented.en.yml | 4 ++++ spec/support/data/empty.yml | 0 spec/translation/yaml_conversion_spec.rb | 7 +++++++ 4 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 spec/support/data/commented.en.yml delete mode 100644 spec/support/data/empty.yml diff --git a/lib/translation_io/yaml_conversion.rb b/lib/translation_io/yaml_conversion.rb index 34c9c4f..9c2d775 100644 --- a/lib/translation_io/yaml_conversion.rb +++ b/lib/translation_io/yaml_conversion.rb @@ -28,9 +28,9 @@ 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 + if translations && translations.values.all? { |value| value.present? } return FlatHash.to_flat_hash(translations) - else # loading an empty file returns false + else # loading an empty file, or file with only the language, returns false return {} end end diff --git a/spec/support/data/commented.en.yml b/spec/support/data/commented.en.yml new file mode 100644 index 0000000..b020394 --- /dev/null +++ b/spec/support/data/commented.en.yml @@ -0,0 +1,4 @@ +en: +# devise: +# confirmations: +# confirmed: "Your email address has been successfully confirmed." diff --git a/spec/support/data/empty.yml b/spec/support/data/empty.yml deleted file mode 100644 index e69de29..0000000 diff --git a/spec/translation/yaml_conversion_spec.rb b/spec/translation/yaml_conversion_spec.rb index ab24e5d..fe2976a 100644 --- a/spec/translation/yaml_conversion_spec.rb +++ b/spec/translation/yaml_conversion_spec.rb @@ -117,6 +117,13 @@ result.should == {} end + + it 'returns an empty Hash if the YAML file is commented (bug fix)' do + yaml_path = 'spec/support/data/commented.en.yml' + result = subject.get_flat_translations_for_yaml_file(yaml_path) + + result.should == {} + end end describe '#get_yaml_data_from_flat_translations' do