i18nize 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78ea8f5bd14b52b8c173e982f375e57ee78a513ac76f814c968eec974de6ece4
4
- data.tar.gz: f3b3d75ae86ea18979d156774eebae09357cc487e47dd872ed4b2ca68a3bf8be
3
+ metadata.gz: 2eb468fa514aca67d48cd8446b001d19a767c5a87f99040ae7361c833757d69f
4
+ data.tar.gz: 491459d40226d8d04a213c849609fdfde0ab1144b8a346c6e85cdb7c4a584f6a
5
5
  SHA512:
6
- metadata.gz: bb64139a19f726fda950ccd7a3148872509e2991d061e95ec741008e68152619a564041b46eda3387d762df691739e7ddaba3f55a35267c41083d23f1700364f
7
- data.tar.gz: 54c6ed5bc10f790bd0bf99cbdd4d439a6d77764d8c5ff7c9e435c1d28b8381fc29456c8e11287b7bbcbdce59cf42daddf18d6a95fac1e13b1e6cbc775dce0979
6
+ metadata.gz: e1cf515c413fa8de3b46cbb35abdcea2cdf60ac7f52e5c1cddf705eae8e2a3875d9f8802642b33e553c1405978aa78b6fc8b05d2bcfa5d7edaa725840c801457
7
+ data.tar.gz: 07cd5ad618104d352c7124fe0add8dcb0ac0f7c2bd18a037239006c0ed5cd88532c011545aeabaf0dfea263b28cc0ad83977b39221ec9eda241ce03722713b00
data/lib/i18nize/cli.rb CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  require "optparse"
4
4
 
5
+ require "i18nize/translator"
6
+ require "i18nize/inserter"
7
+ require "i18nize/comparer"
8
+
5
9
  module I18nize
6
10
  class CLI
7
11
  def self.run(argv)
@@ -10,15 +14,90 @@ module I18nize
10
14
 
11
15
  def initialize(argv)
12
16
  @argv = argv.dup
13
- @options = { from: "en" }
17
+ @options = {
18
+ from: "en",
19
+ mode: :translate,
20
+ missing_from: nil
21
+ }
14
22
  end
15
23
 
16
24
  def run
17
25
  print_help_and_exit if @argv.empty? || help_requested?
18
26
 
19
- to_locale = parse_positional_locale!(@argv.first)
27
+ first = @argv.first
20
28
  parse_flags!(@argv[1..] || [])
21
29
 
30
+ to_locale = parse_positional_locale!(first)
31
+
32
+ if @options[:mode] == :missing
33
+ from = @options[:missing_from] || @options[:from]
34
+ show_missing_translations(from:, to: to_locale)
35
+ return 0
36
+ end
37
+
38
+ run_translation(to_locale)
39
+ 0
40
+ rescue OptionParser::InvalidOption => e
41
+ warn red(e.message)
42
+ print_help_and_exit(2)
43
+ rescue SystemExit => e
44
+ raise e
45
+ rescue => e
46
+ warn red("Error: #{e.class}: #{e.message}")
47
+ e.backtrace.each { |l| warn l } if ENV["I18NIZE_DEBUG"] == "1"
48
+ exit 1
49
+ end
50
+
51
+ private
52
+
53
+ def help_requested?
54
+ (@argv & %w[-h --help help]).any?
55
+ end
56
+
57
+ def parse_positional_locale!(arg)
58
+ abort yellow("Usage: i18nize <to_locale> [--from en] [--missing [FROM]]") unless arg
59
+ arg.strip
60
+ end
61
+
62
+ def parse_flags!(rest)
63
+ OptionParser.new do |o|
64
+ o.banner = "Usage: i18nize <to_locale> [options]"
65
+ o.on("--from LANG", "Source language for comparison/translation (default: en)") { |v| @options[:from] = v }
66
+
67
+ o.on("--missing", "--missing [FROM]", "List missing keys only; optionally specify source locale") do |from|
68
+ @options[:mode] = :missing
69
+ @options[:missing_from] = from if from && !from.strip.empty?
70
+ end
71
+
72
+ o.on("-h", "--help", "Show help") { print_help_and_exit }
73
+ end.parse!(rest)
74
+ end
75
+
76
+ def show_missing_translations(from:, to:)
77
+ load_rails_if_available
78
+ puts cyan("🔍 Missing keys for #{to} (from #{from})")
79
+
80
+ missing = I18nize::Comparer.missing_translations(from_locale: from, to_locale: to)
81
+
82
+ if missing.empty?
83
+ puts green("✅ No missing translations.")
84
+ return
85
+ end
86
+
87
+ total_keys = missing.total_keys
88
+ puts yellow("⚠️ #{total_keys} missing #{total_keys == 1 ? 'key' : 'keys'}:")
89
+
90
+ missing.values_by_file.each do |file, keys_hash|
91
+ puts " #{file}:"
92
+ keys_hash.keys.sort.each do |k|
93
+ puts " - #{k.sub(/^#{from}\./, '')}"
94
+ end
95
+ end
96
+ end
97
+
98
+ def run_translation(to_locale)
99
+ load_env_files
100
+
22
101
  api_key = ENV["DEEPL_API_KEY"]
23
102
  abort red("Missing DEEPL_API_KEY env variable") if api_key.to_s.strip.empty?
24
103
 
@@ -33,53 +112,32 @@ module I18nize
33
112
  )
34
113
 
35
114
  I18nize::Inserter.insert_missing(
115
+ from_locale: @options[:from],
36
116
  to_locale: to_locale,
37
117
  translator: translator
38
118
  )
39
119
 
40
120
  puts green("✅ Translation completed.")
41
- 0
42
- end
43
-
44
- private
45
-
46
- def help_requested?
47
- (@argv & %w[-h --help help]).any?
48
121
  end
49
122
 
50
- def parse_positional_locale!(arg)
51
- abort yellow("Usage: i18nize <to_locale> [--from en]") unless arg
52
- arg.strip
53
- end
54
-
55
- def parse_flags!(rest)
56
- OptionParser.new do |o|
57
- o.banner = "Usage: i18nize <to_locale> [options]"
58
- o.on("--from LANG", "Source language (default: en)") { |v| @options[:from] = v }
59
- o.on("-h", "--help", "Show help") { print_help_and_exit }
60
- end.parse!(rest)
123
+ def load_env_files
124
+ begin
125
+ require "dotenv/load"
126
+ rescue LoadError
127
+ # Ignore
128
+ end
61
129
  end
62
130
 
63
131
  def load_rails_if_available
64
132
  return if defined?(Rails)
65
133
 
66
- require_relative "../../config/environment" if File.exist?("config/environment.rb")
134
+ if File.exist?("config/environment.rb")
135
+ require_relative "../../config/environment"
136
+ end
67
137
  rescue LoadError
68
138
  # Ignore
69
139
  end
70
140
 
71
- def print_help_and_exit
72
- puts <<~HELP
73
- Usage:
74
- i18nize <to_locale> [--from en]
75
-
76
- Examples:
77
- i18nize cs
78
- i18nize fr --from en
79
- HELP
80
- exit 0
81
- end
82
-
83
141
  def color(text, code) = "\e[#{code}m#{text}\e[0m"
84
142
 
85
143
  def red(text) = color(text, 31)
@@ -89,5 +147,24 @@ module I18nize
89
147
  def yellow(text) = color(text, 33)
90
148
 
91
149
  def cyan(text) = color(text, 36)
150
+
151
+ def print_help_and_exit(code = 0)
152
+ puts <<~HELP
153
+ Usage:
154
+ i18nize <to_locale> [options]
155
+
156
+ Examples:
157
+ i18nize cs # translate missing from en -> cs
158
+ i18nize cs --from de # translate missing from de -> cs
159
+ i18nize cs --missing # list missing keys for cs (from en)
160
+ i18nize cs --missing de # list missing keys for cs (from de)
161
+
162
+ Options:
163
+ --from LANG Source language (default: en)
164
+ --missing [FROM] Only list missing keys (optional override of source)
165
+ -h, --help Show this help
166
+ HELP
167
+ exit code
168
+ end
92
169
  end
93
170
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nize
4
- VERSION = "0.4.2"
4
+ VERSION = "0.4.4"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: i18nize
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - nikolas2145
@@ -39,7 +39,6 @@ files:
39
39
  - lib/i18nize/missing_set.rb
40
40
  - lib/i18nize/translator.rb
41
41
  - lib/i18nize/version.rb
42
- - lib/tasks/translator.rake
43
42
  - sig/i18nize.rbs
44
43
  homepage: https://github.com/nikolas2145/i18nize
45
44
  licenses:
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "i18nize/cli"
4
-
5
- namespace :i18nize do
6
- desc "Auto-translate missing I18n keys using DeepL (usage: rake 'i18nize:translate[cs]')"
7
- task :translate, [:to_locale] do |_, args|
8
- to = args[:to_locale]
9
- abort "Usage: rake 'i18nize:translate[cs]'" unless to
10
- I18nize::CLI.run([to] + env_flags_from_rake)
11
- end
12
- end
13
-
14
- def env_flags_from_rake
15
- flags = []
16
- flags += ["--from", ENV["SOURCE"]] if ENV["SOURCE"]
17
- flags
18
- end
19
-
20
- def truthy?(v) = %w[1 true yes y on].include?(v.to_s.downcase)