Last active
October 15, 2015 09:47
-
-
Save estum/4846459aeafd27036ea7 to your computer and use it in GitHub Desktop.
pry: Command to show the I18n translations tree for the given path
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
Pry::Commands.create_command "show-translation" do | |
group "Rails" | |
description "Show translations for the given path" | |
def options(opt) | |
opt.banner unindent <<-BANNER | |
Usage: show-translation [ -y | -r ] <lang.translated.path> | |
Show translations for the given path. | |
BANNER | |
opt.on :y, :yaml, "Format output as yaml", argument: false | |
opt.on :r, :reload, "Reload translations before show", argument: false | |
end | |
def process | |
I18n.reload! if opts.reload? | |
if args.empty? | |
output.puts opts | |
return | |
end | |
result_dict = splitted_args.reduce(translations) do |dict, key| | |
dict.fetch(key.to_sym) { {} } | |
end | |
if opts.yaml? || !defined?(AwesomePrint) | |
output.puts(result_dict.deep_stringify_keys.to_yaml) | |
else | |
output.puts(result_dict.ai) | |
end | |
end | |
private | |
def translations | |
I18n.backend.instance_variable_get(:@translations) | |
end | |
def splitted_args | |
args.flat_map { |val| val.split('.') } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment