-
-
Save Ripper346/ce0431f4b636b027174e444826b991c2 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 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, runfind . -type f -name *.yml | grep -v sp…
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 | |
def sort_yml_key(a, b) | |
countable = %w(one two three four five other true false) | |
if countable.include?(a) and not countable.include?(b) | |
-1 | |
elsif not countable.include?(a) and countable.include?(b) | |
1 | |
elsif countable.include?(a) and countable.include?(b) | |
countable.index(a) <=> countable.index(b) | |
else | |
a <=> b | |
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 and File.exists?(file) | |
YAML::parse(File.read(file)).to_ruby.sort_by_key(true, &method(:sort_yml_key)).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, &method(:sort_yml_key)).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 | |
%w(it en).each do |lang| | |
puts lang | |
%w(activerecord.attributes activerecord.errors activerecord.messages activerecord.models enum).each do |file_type| | |
puts "\t" + file_type | |
ord = current_i18n_yaml("config/locales/#{file_type}.#{lang}.yml") | |
File.open("config/locales/#{file_type}.#{lang}.yml", 'w') do |file| | |
file.puts ord | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment