i18n-tasks 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -0
  3. data/Gemfile +2 -1
  4. data/README.md +80 -78
  5. data/bin/i18n-tasks +24 -30
  6. data/config/i18n-tasks.yml +87 -0
  7. data/config/locales/en.yml +95 -0
  8. data/i18n-tasks.gemspec +1 -0
  9. data/lib/i18n/tasks.rb +10 -0
  10. data/lib/i18n/tasks/base_task.rb +6 -2
  11. data/lib/i18n/tasks/command/collection.rb +18 -0
  12. data/lib/i18n/tasks/command/commander.rb +72 -0
  13. data/lib/i18n/tasks/command/commands/data.rb +73 -0
  14. data/lib/i18n/tasks/command/commands/eq_base.rb +20 -0
  15. data/lib/i18n/tasks/command/commands/health.rb +26 -0
  16. data/lib/i18n/tasks/command/commands/meta.rb +35 -0
  17. data/lib/i18n/tasks/command/commands/missing.rb +73 -0
  18. data/lib/i18n/tasks/command/commands/tree.rb +92 -0
  19. data/lib/i18n/tasks/command/commands/usages.rb +70 -0
  20. data/lib/i18n/tasks/command/commands/xlsx.rb +27 -0
  21. data/lib/i18n/tasks/command/dsl.rb +27 -0
  22. data/lib/i18n/tasks/command/dsl/cmd.rb +19 -0
  23. data/lib/i18n/tasks/command/dsl/cmd_opt.rb +19 -0
  24. data/lib/i18n/tasks/command/dsl/enum_opt.rb +26 -0
  25. data/lib/i18n/tasks/command/options/common.rb +48 -0
  26. data/lib/i18n/tasks/command/options/enum_opt.rb +44 -0
  27. data/lib/i18n/tasks/command/options/list_opt.rb +11 -0
  28. data/lib/i18n/tasks/command/options/locales.rb +47 -0
  29. data/lib/i18n/tasks/command/options/trees.rb +101 -0
  30. data/lib/i18n/tasks/command_error.rb +3 -0
  31. data/lib/i18n/tasks/commands.rb +22 -169
  32. data/lib/i18n/tasks/configuration.rb +1 -16
  33. data/lib/i18n/tasks/console_context.rb +1 -1
  34. data/lib/i18n/tasks/data.rb +13 -8
  35. data/lib/i18n/tasks/data/file_formats.rb +29 -18
  36. data/lib/i18n/tasks/data/file_system_base.rb +35 -4
  37. data/lib/i18n/tasks/data/router/conservative_router.rb +18 -11
  38. data/lib/i18n/tasks/data/tree/node.rb +5 -15
  39. data/lib/i18n/tasks/data/tree/nodes.rb +0 -3
  40. data/lib/i18n/tasks/data/tree/siblings.rb +32 -2
  41. data/lib/i18n/tasks/data/tree/traversal.rb +117 -96
  42. data/lib/i18n/tasks/google_translation.rb +25 -25
  43. data/lib/i18n/tasks/html_keys.rb +10 -0
  44. data/lib/i18n/tasks/key_pattern_matching.rb +1 -0
  45. data/lib/i18n/tasks/locale_list.rb +19 -0
  46. data/lib/i18n/tasks/missing_keys.rb +32 -33
  47. data/lib/i18n/tasks/plural_keys.rb +1 -1
  48. data/lib/i18n/tasks/reports/base.rb +4 -9
  49. data/lib/i18n/tasks/reports/spreadsheet.rb +5 -5
  50. data/lib/i18n/tasks/reports/terminal.rb +62 -38
  51. data/lib/i18n/tasks/scanners/base_scanner.rb +5 -4
  52. data/lib/i18n/tasks/slop_command.rb +27 -0
  53. data/lib/i18n/tasks/stats.rb +20 -0
  54. data/lib/i18n/tasks/string_interpolation.rb +14 -0
  55. data/lib/i18n/tasks/unused_keys.rb +0 -10
  56. data/lib/i18n/tasks/version.rb +1 -1
  57. data/spec/commands/data_commands_spec.rb +38 -0
  58. data/spec/commands/tree_commands_spec.rb +68 -0
  59. data/spec/fixtures/app/views/index.html.slim +1 -0
  60. data/spec/google_translate_spec.rb +5 -3
  61. data/spec/i18n_spec.rb +18 -0
  62. data/spec/i18n_tasks_spec.rb +8 -8
  63. data/spec/spec_helper.rb +3 -3
  64. data/spec/support/test_codebase.rb +4 -1
  65. data/spec/used_keys_spec.rb +7 -7
  66. data/templates/config/i18n-tasks.yml +2 -2
  67. metadata +48 -4
  68. data/lib/i18n/tasks/commands_base.rb +0 -107
  69. data/lib/i18n/tasks/fill_tasks.rb +0 -40
@@ -1,107 +0,0 @@
1
- # coding: utf-8
2
- require 'ostruct'
3
- require 'set'
4
- module I18n::Tasks
5
- class CommandsBase
6
- include ::I18n::Tasks::Logging
7
-
8
- def initialize(i18n = nil)
9
- @i18n = i18n
10
- end
11
-
12
- def locales_opt(locales)
13
- return i18n.locales if locales == ['all'] || locales == 'all'
14
- if locales.present?
15
- locales = Array(locales).map { |v| v.strip.split(/\s*[\+,:]\s*/).compact.presence if v.is_a?(String) }.flatten
16
- locales = locales.map(&:presence).compact.map { |v| v == 'base' ? base_locale : v }
17
- locales
18
- else
19
- i18n.locales
20
- end
21
- end
22
-
23
- VALID_LOCALE_RE = /\A\w[\w\-_\.]*\z/i
24
- def parse_locales!(opt)
25
- opt[:locales] = locales_opt(opt[:arguments].presence || opt[:locales]).tap do |locales|
26
- locales.each do |locale|
27
- raise CommandError.new("Invalid locale: #{locale}") if VALID_LOCALE_RE !~ locale
28
- end
29
- log_verbose "locales for the command are #{locales.inspect}"
30
- end
31
- end
32
-
33
-
34
- VALID_TREE_FORMATS = %w(terminal-table yaml json keys inspect)
35
-
36
- def print_locale_tree(tree, opt, version = :show_tree)
37
- format = opt[:format] || VALID_TREE_FORMATS.first
38
- raise CommandError.new("unknown format: #{format}. Valid formats are: #{VALID_TREE_FORMATS * ', '}.") unless VALID_TREE_FORMATS.include?(format)
39
- case format
40
- when 'terminal-table'
41
- terminal_report.send(version, tree)
42
- when 'inspect'
43
- puts tree.inspect
44
- when 'keys'
45
- puts tree.key_names(root: true)
46
- when *i18n.data.adapter_names.map(&:to_s)
47
- puts i18n.data.adapter_dump tree, i18n.data.adapter_by_name(format)
48
- end
49
- end
50
-
51
- protected
52
-
53
- def terminal_report
54
- @terminal_report ||= I18n::Tasks::Reports::Terminal.new(i18n)
55
- end
56
-
57
- def spreadsheet_report
58
- @spreadsheet_report ||= I18n::Tasks::Reports::Spreadsheet.new(i18n)
59
- end
60
-
61
- class << self
62
- def cmds
63
- @cmds ||= {}.with_indifferent_access
64
- end
65
-
66
- def cmd(name, &block)
67
- cmds[name] = OpenStruct.new(@next_def)
68
- @next_def = {}
69
- define_method name do |*args|
70
- begin
71
- coloring_was = Term::ANSIColor.coloring?
72
- Term::ANSIColor.coloring = ENV['I18N_TASKS_COLOR'] || STDOUT.isatty
73
- instance_exec *args, &block
74
- rescue CommandError => e
75
- log_error e.message
76
- exit 78
77
- ensure
78
- Term::ANSIColor.coloring = coloring_was
79
- end
80
- end
81
- end
82
-
83
- def desc(text)
84
- next_def[:desc] = text
85
- end
86
-
87
- def opts(&block)
88
- next_def[:opts] = block
89
- end
90
-
91
- private
92
- def next_def
93
- @next_def ||= {}
94
- end
95
- end
96
-
97
- def desc(name)
98
- self.class.cmds.try(:[], name).try(:desc)
99
- end
100
-
101
- def i18n
102
- @i18n ||= I18n::Tasks::BaseTask.new
103
- end
104
-
105
- delegate :base_locale, :t, to: :i18n
106
- end
107
- end
@@ -1,40 +0,0 @@
1
- # coding: utf-8
2
- module I18n::Tasks
3
- module FillTasks
4
- def fill_missing_value(opts = {})
5
- value = opts[:value] || ''
6
- base = opts[:base_locale] || base_locale
7
- locales_for_update(opts).each do |locale|
8
- m = missing_keys(locales: [locale], base_locale: base).keys { |key, node|
9
- node.value = value.respond_to?(:call) ? value.call(key, locale, node) : value
10
- if node.data.key?(:path)
11
- # set path hint for the router
12
- node.data.update path: LocalePathname.replace_locale(node.data[:path], node.data[:locale], locale), locale: locale
13
- end
14
- }
15
- data[locale] = data[locale].merge! m
16
- end
17
- end
18
-
19
- def fill_missing_google_translate(opts = {})
20
- from = opts[:from] || base_locale
21
- locales = (Array(opts[:locales]).presence || self.locales) - [from]
22
- locales.each do |locale|
23
- keys = missing_tree(locale, from, false).key_names.map(&:to_s)
24
- values = google_translate(keys.zip(keys.map(&t_proc(from))), to: locale, from: from).map(&:last)
25
-
26
- data[locale] = data[locale].merge! Data::Tree::Node.new(
27
- key: locale,
28
- children: Data::Tree::Siblings.from_flat_pairs(keys.zip(values))
29
- ).to_siblings
30
- end
31
- end
32
-
33
- def locales_for_update(opts)
34
- locales = (Array(opts[:locales] || opts[:locale]).presence || self.locales).map(&:to_s)
35
- # make sure base_locale always comes first if present
36
- locales = [base_locale] + (locales - [base_locale]) if locales.include?(base_locale)
37
- locales
38
- end
39
- end
40
- end