-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds command to translate a pattern of keys
- Related to #574
- Loading branch information
1 parent
6a46c61
commit d3f9274
Showing
3 changed files
with
50 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,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module I18n::Tasks | ||
module Command | ||
module Commands | ||
module Translate | ||
include Command::Collection | ||
include I18n::Tasks::KeyPatternMatching | ||
|
||
cmd :translate, | ||
pos: '[pattern]', | ||
desc: t('i18n_tasks.cmd.desc.translate'), | ||
args: [:locale, :locale_to_translate_from, arg(:data_format).from(1), :translation_backend] | ||
|
||
def translate(opts = {}) | ||
forest = i18n.tree(opts[:from]) | ||
forest.set_root_key!(opts[:locale]) | ||
|
||
if opts[:pattern] | ||
pattern_re = i18n.compile_key_pattern(opts[:pattern]) | ||
forest.select_keys! { |full_key, _node| full_key =~ pattern_re } | ||
end | ||
|
||
backend = opts[:backend]&.to_sym || i18n.translation_config[:backend] || :google | ||
|
||
puts forest | ||
|
||
print_forest i18n.translate_forest(forest, from: opts[:from], backend: backend), opts | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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