Last active
April 18, 2019 12:34
-
-
Save wteuber/5885260 to your computer and use it in GitHub Desktop.
Sort your rails locale yamls alphabetically and adds an empty line when indent lowers 2 or more levelsPrints everything to stdout. Watch your yaml anchors and aliases (fix manually, if needed).usage: chmod +x ./locale_yaml_formatter.rb ./locale_yaml_formatter.rb <path/to/locale.yml>In a rails project, runfind . -type f -name *.yml | grep -v spec…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Sort your rails locale yamls alphabetically and adds an empty line when indent lowers 2 or more levels | |
# Prints everything to stdout. Watch your yaml anchors and aliases (fix manually, if needed). | |
# usage: | |
# chmod +x ./locale_yaml_formatter.rb | |
# ./locale_yaml_formatter.rb <path/to/locale.yml> | |
# | |
# In a rails project, run | |
# find . -type f -name *.yml | grep -v spec | grep locales | _ | |
# awk '{system("cp "$0" "$0".tmp && locale_yaml_formatter.rb "$0".tmp > "$0" && rm "$0".tmp")}' | |
require 'yaml' | |
class Hash | |
def to_hash_recursive | |
result = self.to_hash | |
result.each do |key, value| | |
case value | |
when Hash | |
result[key] = value.to_hash_recursive | |
when Array | |
result[key] = value.to_hash_recursive | |
end | |
end | |
result | |
end | |
def sort_by_key(recursive=false, &block) | |
self.keys.sort(&block).reduce({}) do |seed, key| | |
seed[key] = self[key] | |
if recursive && seed[key].is_a?(Hash) | |
seed[key] = seed[key].sort_by_key(true, &block) | |
end | |
seed | |
end | |
end | |
end | |
class Array | |
def to_hash_recursive | |
result = self | |
result.each_with_index do |value,i| | |
case value | |
when Hash | |
result[i] = value.to_hash_recursive | |
when Array | |
result[i] = value.to_hash_recursive | |
end | |
end | |
result | |
end | |
end | |
def current_i18n_yaml(file = nil) | |
i18n_yaml = if file && File.exists?(file) | |
YAML::parse(File.read(file)).to_ruby.sort_by_key(true).to_yaml | |
else | |
{I18n.default_locale.to_s => I18n.backend.send(:translations)[I18n.default_locale].with_indifferent_access.to_hash_recursive}.sort_by_key(true).to_yaml | |
end | |
process = i18n_yaml.split(/\n/).reject{|e| e == ''}[1..-1] # remove "---" from first line in yaml | |
# add an empty line if yaml tree level changes by 2 or more | |
# exmple: | |
# 1 this: | |
# 2 is: | |
# 3 a: | |
# 4 deep: branch in the tree | |
# 5 | |
# 6 that: | |
# 7 one: not | |
# 8 two: ... | |
# Line 5 will be inserted for better readability | |
tmp_ary = [] | |
process.each_with_index do |line, idx| | |
tmp_ary << line | |
unless process[idx+1].nil? | |
this_line_spcs = line.match(/\A\s*/)[0].length | |
next_line_spcs = process[idx+1].match(/\A\s*/)[0].length | |
tmp_ary << '' if next_line_spcs - this_line_spcs < -2 | |
end | |
end | |
tmp_ary * "\n" | |
end | |
# File.open(I18n.default_locale.to_s + '.yml', 'w') { |file| file.puts current_i18n_yaml} | |
puts current_i18n_yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment