-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #649 from codeforjapan/add-task-i18n_yaml-dump_all
- Loading branch information
Showing
1 changed file
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
def deep_keys(hash, prefix = "") | ||
hash.flat_map do |key, value| | ||
full_key = prefix.empty? ? key.to_s : "#{prefix}.#{key}" | ||
value.is_a?(Hash) ? deep_keys(value, full_key) : full_key | ||
end | ||
end | ||
|
||
namespace :i18n_yaml do | ||
desc "dump all locale data for some locale" | ||
task dump_all: :environment do | ||
locale = :ja | ||
all_translations = I18n.t(".", locale:, default: {}).deep_merge({}) | ||
keys = deep_keys(all_translations) | ||
buf = [] | ||
I18n.with_locale(locale) do | ||
keys.each do |key| | ||
# skip faker.* | ||
next if key.start_with?("faker.") | ||
|
||
buf << "#{key}: #{I18n.t(key)}" | ||
rescue StandardError => e | ||
warn "ERROR: #{key}, #{e.message}" | ||
end | ||
end | ||
|
||
puts buf | ||
end | ||
end |