Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds command to translate a pattern of keys #585

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glebm Do you have a suggestion for creating a tree with all keys from opts[:from] locale to send into the translate_forest method? I have tried a few ways but cannot figure out the correct format.

Copy link
Owner

@glebm glebm Jun 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The forest root nodes should be the locales to translate to (the target locales).

For example, {"en": {"key": "..."}}

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
Loading