2023-01-05 06:19:41 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-01-06 03:56:20 -08:00
|
|
|
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
|
2023-01-05 06:19:41 -08:00
|
|
|
end
|
|
|
|
|
2023-01-06 03:56:20 -08:00
|
|
|
def test_locale_repair
|
|
|
|
SUBSTITUTIONS.inject(self) do |val, (from, to)|
|
|
|
|
val.gsub(to, from).gsub(to.upcase, from.upcase)
|
|
|
|
end
|
2023-01-05 06:19:41 -08:00
|
|
|
end
|
2023-01-06 03:56:20 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
using TestLocaleTransformer
|
2023-01-05 06:19:41 -08:00
|
|
|
|
2023-01-06 03:56:20 -08:00
|
|
|
namespace :locale do
|
|
|
|
desc "Generate en-xx locale"
|
|
|
|
task generate: :environment do
|
2023-01-05 06:19:41 -08:00
|
|
|
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
|
|
|
|
|
2023-01-06 03:56:20 -08:00
|
|
|
val = val.test_locale_destroy
|
2023-01-05 06:19:41 -08:00
|
|
|
|
|
|
|
# undo damage in %{variables}
|
2023-01-06 03:56:20 -08:00
|
|
|
val = val.gsub(/%{([^}]+)}/, &:test_locale_repair)
|
2023-01-05 06:19:41 -08:00
|
|
|
|
|
|
|
# undo damage in <html tags>
|
2023-01-06 03:56:20 -08:00
|
|
|
val = val.gsub(/<([^>]+)>/, &:test_locale_repair)
|
2023-01-05 06:19:41 -08:00
|
|
|
|
|
|
|
"[#{val}]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
en_locales = Dir[Rails.root.join("config/locales/*.en.yml")]
|
|
|
|
|
|
|
|
en_locales.each do |locale_path|
|
|
|
|
destination = locale_path.sub(/\.en\.yml$/, ".en-xx.yml")
|
|
|
|
puts "* generating #{File.basename(destination)}"
|
|
|
|
|
|
|
|
locale = YAML.load_file(locale_path)["en"]
|
|
|
|
new_locale = { "en-xx" => transform_locale(locale) }
|
|
|
|
File.open(destination, "w") do |f|
|
|
|
|
f.puts new_locale.to_yaml
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|