From c8e576fd5d5bad05f659d9100b1e43c4d88d8679 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Hoste?= Date: Wed, 26 Apr 2023 17:53:46 +0200 Subject: [PATCH] Init cleanup: keep YAML files and keys from locales that are not in the configuration file. (#54) * Don't delete unmanaged locales YAML files when init new project * Add spec to check that cleanup don't remove ignored target languages --- .../init_operation/cleanup_yaml_files_step.rb | 39 +++++++++---------- lib/translation_io/yaml_entry.rb | 4 +- .../cleanup_yaml_files_step_spec.rb | 18 ++++++++- 3 files changed, 36 insertions(+), 25 deletions(-) diff --git a/lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb b/lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb index 13ec831..ef315a4 100644 --- a/lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb +++ b/lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb @@ -12,30 +12,28 @@ def initialize(source_locale, target_locales, yaml_file_paths, yaml_locales_path def run @yaml_file_paths.each do |locale_file_path| if locale_file_removable?(locale_file_path) - if File.exist?(locale_file_path) - content_hash = TranslationIO.yaml_load(File.read(locale_file_path)) || {} - source_content_hash = content_hash.select { |k| k.to_s == @source_locale.to_s } + content_hash = TranslationIO.yaml_load(File.read(locale_file_path)) || {} + source_content_hash = content_hash.reject { |k| k.to_s.in?(@target_locales.collect(&:to_s)) } - if source_content_hash.empty? - TranslationIO.info "Removing #{locale_file_path}", 2, 2 - FileUtils.rm(locale_file_path) - elsif content_hash != source_content_hash # in case of mixed languages in source YAML file - TranslationIO.info "Rewriting #{locale_file_path}", 2, 2 + if source_content_hash.empty? + TranslationIO.info "Removing #{locale_file_path}", 2, 2 + FileUtils.rm(locale_file_path) + elsif content_hash != source_content_hash # in case of mixed languages in source YAML file + TranslationIO.info "Rewriting #{locale_file_path}", 2, 2 - if TranslationIO.config.yaml_line_width - file_content = source_content_hash.to_yaml(:line_width => TranslationIO.config.yaml_line_width) - else - file_content = source_content_hash.to_yaml - end + if TranslationIO.config.yaml_line_width + file_content = source_content_hash.to_yaml(:line_width => TranslationIO.config.yaml_line_width) + else + file_content = source_content_hash.to_yaml + end - file_content = file_content.gsub(/ $/, '') # remove trailing spaces + file_content = file_content.gsub(/ $/, '') # remove trailing spaces - File.open(locale_file_path, 'wb') do |file| - file.write(file_content) - end - else - # don't touch source + File.open(locale_file_path, 'wb') do |file| + file.write(file_content) end + else + # don't touch source end end end @@ -44,6 +42,7 @@ def run private def locale_file_removable?(locale_file_path) + exists = File.exist?(locale_file_path) in_project = locale_file_path_in_project?(locale_file_path) protected_file = @target_locales.any? do |target_locale| @@ -55,7 +54,7 @@ def locale_file_removable?(locale_file_path) paths.include?(TranslationIO.normalize_path(locale_file_path)) end - in_project && !protected_file + exists && in_project && !protected_file end def locale_file_path_in_project?(locale_file_path) diff --git a/lib/translation_io/yaml_entry.rb b/lib/translation_io/yaml_entry.rb index 5c99b34..873b239 100644 --- a/lib/translation_io/yaml_entry.rb +++ b/lib/translation_io/yaml_entry.rb @@ -39,9 +39,7 @@ def localization?(key, value) end def localization_prefix?(key) - localization_key_prefixes.any? do |prefix| - key_without_locale(key).match(/^#{Regexp.escape(prefix)}\b/) != nil - end + localization_key_prefixes.any? { |prefix| key_without_locale(key).match(/^#{Regexp.escape(prefix)}\b/) != nil } end private diff --git a/spec/translation/client/init_operation/cleanup_yaml_files_step_spec.rb b/spec/translation/client/init_operation/cleanup_yaml_files_step_spec.rb index 2017c4e..b76c53e 100644 --- a/spec/translation/client/init_operation/cleanup_yaml_files_step_spec.rb +++ b/spec/translation/client/init_operation/cleanup_yaml_files_step_spec.rb @@ -2,7 +2,7 @@ describe TranslationIO::Client::InitOperation::CleanupYamlFilesStep do - it 'removes bad target files' do + it 'removes files and keys from initialized target locale codes (and ignore not-initialized target locales)' do yaml_locales_path = 'tmp/config/locales' FileUtils.mkdir_p(yaml_locales_path) @@ -24,6 +24,15 @@ EOS end + File.open("#{yaml_locales_path}/nl.yml", 'wb') do |file| + file.write <<-EOS +--- +nl: + main: + hello: Hallo wereld +EOS + end + File.open("#{yaml_locales_path}/mixed.yml", 'wb') do |file| file.write <<-EOS --- @@ -31,7 +40,9 @@ misc: yo: Yo folks fr: - home: "Accueil" + home: Accueil +nl: + home: Receptie EOS end @@ -44,6 +55,7 @@ File.exist?("#{yaml_locales_path}/en.yml" ).should be true File.exist?("#{yaml_locales_path}/fr.yml" ).should be false + File.exist?("#{yaml_locales_path}/nl.yml" ).should be true # not removed because no in target locales File.exist?("#{yaml_locales_path}/mixed.yml").should be true File.read("#{yaml_locales_path}/mixed.yml").should == <<-EOS @@ -51,6 +63,8 @@ en: misc: yo: Yo folks +nl: + home: Receptie EOS end