Ruby on Rails locales-find script for fuzzy searching through locales yaml files using full dot notation part
Hacked together a quick ruby script for fuzzy searching for locale keys with their full dot notation part. It loads all yaml files in config/locales, flattenes all the key paths like sv.foo.bar.baz and prints the value and filename after. The result is passed through fzf for fuzzy filtering.
bin/locales-find | fzf --ansi -e
or maybe: docker-compose run --rm web bin/locales-find 2>/dev/null | fzf --ansi -e
bin/locales-find
#!/usr/bin/env ruby
require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)
require "rubygems"
require "bundler/setup"
require "colorize"
require "yaml"
def main
flattened_locales.each do |(key, val, file)|
puts "#{key.red}: #{val} - #{file.bold}"
end
end
def flattened_locales
Dir["config/locales/*.yml"].flat_map do |path|
hash = YAML.safe_load(File.read(path), aliases: true)
file = path.split("/")[2..].join("/")
flatten_hash(hash).map { |arr| arr << file }
end.sort_by { |(key)| key[3..] }
end
def flatten_hash(obj, key_path = nil)
case obj
when Array
obj.flat_map.with_index { |o, i| flatten_hash(o, [key_path, i].compact.join(".")) }
when Hash
obj.flat_map { |k, o| flatten_hash(o, [key_path, k].compact.join(".")) }
else
[[key_path, obj]]
end
end
main
See also this other script I wrote 2 years earlier and almost forgot about:
Ruby on Rails compare yaml locales by key for each language
bin/locales-compare
https://gist.github.com/scmx/a1b68c7e9206fb62c5e2387c308d04f3