From d3f927486e2086f7ea47cde64e440eb7a396c50a Mon Sep 17 00:00:00 2001 From: David Wessman Date: Sun, 16 Jun 2024 18:29:22 +0200 Subject: [PATCH] Adds command to translate a pattern of keys - Related to #574 --- lib/i18n/tasks/command/commands/translate.rb | 33 ++++++++++++++++++++ lib/i18n/tasks/commands.rb | 2 ++ spec/i18n_tasks_spec.rb | 15 +++++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/i18n/tasks/command/commands/translate.rb diff --git a/lib/i18n/tasks/command/commands/translate.rb b/lib/i18n/tasks/command/commands/translate.rb new file mode 100644 index 00000000..6763745b --- /dev/null +++ b/lib/i18n/tasks/command/commands/translate.rb @@ -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 diff --git a/lib/i18n/tasks/commands.rb b/lib/i18n/tasks/commands.rb index 50e306cd..a19203db 100644 --- a/lib/i18n/tasks/commands.rb +++ b/lib/i18n/tasks/commands.rb @@ -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' @@ -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 diff --git a/spec/i18n_tasks_spec.rb b/spec/i18n_tasks_spec.rb index fbbc3a55..951b0aaf 100644 --- a/spec/i18n_tasks_spec.rb +++ b/spec/i18n_tasks_spec.rb @@ -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