Skip to content

Commit

Permalink
Add copy command
Browse files Browse the repository at this point in the history
Closes: #288
  • Loading branch information
jclusso committed Feb 24, 2024
1 parent 1ab60ab commit f6f89ce
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ en:
check_consistent_interpolations: verify that all translations use correct interpolation variables
check_normalized: verify that all translation data is normalized
config: display i18n-tasks configuration
cp: copy the keys in locale data that match the given pattern
data: show locale data
data_merge: merge locale data with trees
data_remove: remove keys present in tree from data
Expand Down
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ ru:
интерполяционные переменные
check_normalized: проверить, что все файлы переводов нормализованы
config: показать конфигурацию
cp: скопируйте ключи в данных локали, соответствующие заданному шаблону
data: показать данные переводов
data_merge: добавить дерево к переводам
data_remove: удалить ключи, которые есть в дереве, из данных
Expand Down
14 changes: 14 additions & 0 deletions lib/i18n/tasks/command/commands/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def mv(opt = {})
terminal_report.mv_results results
end

cmd :cp,
pos: 'FROM_KEY_PATTERN TO_KEY_PATTERN',
desc: t('i18n_tasks.cmd.desc.cp')
def cp(opt = {})
fail CommandError, 'requires FROM_KEY_PATTERN and TO_KEY_PATTERN' if opt[:arguments].size < 2

from_pattern = opt[:arguments].shift
to_pattern = opt[:arguments].shift
forest = i18n.data_forest
results = forest.mv_key!(compile_key_pattern(from_pattern), to_pattern, root: false, retain: true)
i18n.data.write forest
terminal_report.cp_results results
end

cmd :rm,
pos: 'KEY_PATTERN [KEY_PATTERN...]',
desc: t('i18n_tasks.cmd.desc.rm')
Expand Down
4 changes: 2 additions & 2 deletions lib/i18n/tasks/data/tree/siblings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def rename_key(key, new_key)
# @param to_pattern [Regexp]
# @param root [Boolean]
# @return {old key => new key}
def mv_key!(from_pattern, to_pattern, root: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
def mv_key!(from_pattern, to_pattern, root: false, retain: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
moved_forest = Siblings.new
moved_nodes = []
old_key_to_new_key = {}
Expand Down Expand Up @@ -69,7 +69,7 @@ def mv_key!(from_pattern, to_pattern, root: false) # rubocop:disable Metrics/Abc
node.value = new_target.to_sym
end
end
remove_nodes_and_emptied_ancestors! moved_nodes
remove_nodes_and_emptied_ancestors!(moved_nodes) unless retain
merge! moved_forest
old_key_to_new_key
end
Expand Down
6 changes: 6 additions & 0 deletions lib/i18n/tasks/reports/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def mv_results(results)
end
end

def cp_results(results)
results.each do |(from, to)|
print_info "#{Rainbow(from).cyan} #{Rainbow('+').yellow.bright} #{Rainbow(to).green}"
end
end

def check_normalized_results(non_normalized)
if non_normalized.empty?
print_success 'All data is normalized'
Expand Down
6 changes: 6 additions & 0 deletions spec/commands/data_commands_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ def en_data2
expect(in_test_app_dir { YAML.load_file('config/locales/en.yml') })
.to(eq('en' => { 'b' => '1', 'common' => { 'hello' => 'Hello' } }))
end

it '#cp' do
run_cmd('cp', 'a', 'b')
expect(in_test_app_dir { YAML.load_file('config/locales/en.yml') })
.to(eq('en' => { 'a' => '1', 'b' => '1', 'common' => { 'hello' => 'Hello' } }))
end
end

0 comments on commit f6f89ce

Please sign in to comment.