Skip to content

Commit

Permalink
Init cleanup: keep YAML files and keys from locales that are not in t…
Browse files Browse the repository at this point in the history
…he 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
  • Loading branch information
MichaelHoste committed Apr 26, 2023
1 parent 5b5d477 commit c8e576f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
39 changes: 19 additions & 20 deletions lib/translation_io/client/init_operation/cleanup_yaml_files_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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|
Expand All @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions lib/translation_io/yaml_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -24,14 +24,25 @@
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
---
en:
misc:
yo: Yo folks
fr:
home: "Accueil"
home: Accueil
nl:
home: Receptie
EOS
end

Expand All @@ -44,13 +55,16 @@

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
---
en:
misc:
yo: Yo folks
nl:
home: Receptie
EOS
end

Expand Down

0 comments on commit c8e576f

Please sign in to comment.