From bbdc3ac652fddeb1d360c92b832df8ed5a80c9e1 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Fri, 6 Jan 2023 12:56:20 +0100 Subject: [PATCH] tasks/locale: define substituted characters in a hash reducing code by adding moar! --- lib/tasks/locale.rake | 60 +++++++++++++++++++++---------------------- 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/lib/tasks/locale.rake b/lib/tasks/locale.rake index fb924b91..049305e0 100644 --- a/lib/tasks/locale.rake +++ b/lib/tasks/locale.rake @@ -1,48 +1,46 @@ # frozen_string_literal: true +module TestLocaleTransformer + SUBSTITUTIONS = { + "a" => "åä", + "e" => "éê", + "i" => "ïí", + "o" => "öø", + "u" => "üǔ" + }.freeze + + refine String do + def test_locale_destroy + SUBSTITUTIONS.inject(self) do |val, (from, to)| + val.gsub(from, to).gsub(from.upcase, to.upcase) + end + end + + def test_locale_repair + SUBSTITUTIONS.inject(self) do |val, (from, to)| + val.gsub(to, from).gsub(to.upcase, from.upcase) + end + end + end +end + +using TestLocaleTransformer + namespace :locale do desc "Generate en-xx locale" task generate: :environment do - def destroy(val) - val - .gsub("A", "ÅÄ") - .gsub("E", "ÉÊ") - .gsub("I", "ÏÍ") - .gsub("O", "ÖØ") - .gsub("U", "ÜǓ") - .gsub("a", "åä") - .gsub("e", "éê") - .gsub("i", "ïí") - .gsub("o", "öø") - .gsub("u", "üǔ") - end - - def repair(val) - val - .gsub("ÅÄ", "A") - .gsub("ÉÊ", "E") - .gsub("ÏÍ", "I") - .gsub("ÖØ", "O") - .gsub("ÜǓ", "U") - .gsub("åä", "a") - .gsub("éê", "e") - .gsub("ïí", "i") - .gsub("öø", "o") - .gsub("üǔ", "u") - end - def transform_locale(hash) hash.transform_values do |val| next transform_locale(val) if val.is_a? Hash next val if val.is_a? Symbol - val = destroy(val) + val = val.test_locale_destroy # undo damage in %{variables} - val = val.gsub(/%{([^}]+)}/) { repair(_1) } + val = val.gsub(/%{([^}]+)}/, &:test_locale_repair) # undo damage in - val = val.gsub(/<([^>]+)>/) { repair(_1) } + val = val.gsub(/<([^>]+)>/, &:test_locale_repair) "[#{val}]" end