Skip to content

Commit

Permalink
Adds command to translate a pattern of keys
Browse files Browse the repository at this point in the history
- Related to #574
  • Loading branch information
davidwessman committed Jun 16, 2024
1 parent 6a46c61 commit d3f9274
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/i18n/tasks/command/commands/translate.rb
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
2 changes: 2 additions & 0 deletions lib/i18n/tasks/commands.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require 'i18n/tasks/command/commands/interpolations'
require 'i18n/tasks/command/commands/eq_base'
require 'i18n/tasks/command/commands/data'
require 'i18n/tasks/command/commands/translate'
require 'i18n/tasks/command/commands/tree'
require 'i18n/tasks/command/commands/meta'
require 'i18n/tasks/command/commander'
Expand All @@ -21,6 +22,7 @@ class Commands < Command::Commander
include Command::Commands::Interpolations
include Command::Commands::EqBase
include Command::Commands::Data
include Command::Commands::Translate
include Command::Commands::Tree
include Command::Commands::Meta

Expand Down
15 changes: 15 additions & 0 deletions spec/i18n_tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,21 @@
end
end

it 'translate pattern' do
in_test_app_dir do
expect(YAML.load_file('config/locales/en.yml')['en']['used_but_missing']).to be_nil
expect(YAML.load_file('config/locales/en.yml')['en']['default_arg']).to be_nil
end
run_cmd 'translate', '--locale=sv'
in_test_app_dir do
expect(YAML.load_file('config/locales/en.yml')['en']['used_but_missing']['key']).to eq 'Key'
expect(YAML.load_file('config/locales/en.yml')['en']['present_in_es_but_not_en']['a']).to eq 'ES_TEXT'
expect(YAML.load_file('config/locales/en.yml')['en']['default_arg']).to eq 'Default Text'
expect(YAML.load_file('config/locales/en.yml')['en']['default_plural_arg']).to eq({ 'one' => 'One Text',
'other' => 'Other Text' })
end
end

it 'default value: base_value for non-base locale' do
in_test_app_dir do
expect(YAML.load_file('config/locales/es.yml')['es']['missing_in_es']).to be_nil
Expand Down

0 comments on commit d3f9274

Please sign in to comment.