i18nize 0.4.2.1 → 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: 1aea67d497c76c4a811f9e48e89deaab3f98d78e4d487a76a07bf476faf4af70
4
- data.tar.gz: 93f20538910ecc8f654534b0ab3bea080a7c726e5e5f74f1fc29cb36fbdff16f
3
+ metadata.gz: 2eb468fa514aca67d48cd8446b001d19a767c5a87f99040ae7361c833757d69f
4
+ data.tar.gz: 491459d40226d8d04a213c849609fdfde0ab1144b8a346c6e85cdb7c4a584f6a
5
5
  SHA512:
6
- metadata.gz: d68e7db77fc25438611dfac7cfe4eccdfedec1937fc71e4243221fb8ef7defd5a121633dbee59a0dc04936ee42f84cddcbd0c8f0136260e475261eee21bc9079
7
- data.tar.gz: a6bce323a49584b9e978ccbd9170c41f8c95a8b7df131928a8da1a5e200459939b8f0316ea83de2990184eb7e95c87e4db050264dfa6b0286b7ad6dc24508fe4
6
+ metadata.gz: e1cf515c413fa8de3b46cbb35abdcea2cdf60ac7f52e5c1cddf705eae8e2a3875d9f8802642b33e553c1405978aa78b6fc8b05d2bcfa5d7edaa725840c801457
7
+ data.tar.gz: 07cd5ad618104d352c7124fe0add8dcb0ac0f7c2bd18a037239006c0ed5cd88532c011545aeabaf0dfea263b28cc0ad83977b39221ec9eda241ce03722713b00
data/lib/i18nize/cli.rb CHANGED
@@ -1,8 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "optparse"
4
+
4
5
  require "i18nize/translator"
5
6
  require "i18nize/inserter"
7
+ require "i18nize/comparer"
6
8
 
7
9
  module I18nize
8
10
  class CLI
@@ -12,15 +14,90 @@ module I18nize
12
14
 
13
15
  def initialize(argv)
14
16
  @argv = argv.dup
15
- @options = { from: "en" }
17
+ @options = {
18
+ from: "en",
19
+ mode: :translate,
20
+ missing_from: nil
21
+ }
16
22
  end
17
23
 
18
24
  def run
19
25
  print_help_and_exit if @argv.empty? || help_requested?
20
26
 
21
- to_locale = parse_positional_locale!(@argv.first)
27
+ first = @argv.first
22
28
  parse_flags!(@argv[1..] || [])
23
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
+
24
101
  api_key = ENV["DEEPL_API_KEY"]
25
102
  abort red("Missing DEEPL_API_KEY env variable") if api_key.to_s.strip.empty?
26
103
 
@@ -35,53 +112,32 @@ module I18nize
35
112
  )
36
113
 
37
114
  I18nize::Inserter.insert_missing(
115
+ from_locale: @options[:from],
38
116
  to_locale: to_locale,
39
117
  translator: translator
40
118
  )
41
119
 
42
120
  puts green("✅ Translation completed.")
43
- 0
44
- end
45
-
46
- private
47
-
48
- def help_requested?
49
- (@argv & %w[-h --help help]).any?
50
121
  end
51
122
 
52
- def parse_positional_locale!(arg)
53
- abort yellow("Usage: i18nize <to_locale> [--from en]") unless arg
54
- arg.strip
55
- end
56
-
57
- def parse_flags!(rest)
58
- OptionParser.new do |o|
59
- o.banner = "Usage: i18nize <to_locale> [options]"
60
- o.on("--from LANG", "Source language (default: en)") { |v| @options[:from] = v }
61
- o.on("-h", "--help", "Show help") { print_help_and_exit }
62
- end.parse!(rest)
123
+ def load_env_files
124
+ begin
125
+ require "dotenv/load"
126
+ rescue LoadError
127
+ # Ignore
128
+ end
63
129
  end
64
130
 
65
131
  def load_rails_if_available
66
132
  return if defined?(Rails)
67
133
 
68
- require_relative "../../config/environment" if File.exist?("config/environment.rb")
134
+ if File.exist?("config/environment.rb")
135
+ require_relative "../../config/environment"
136
+ end
69
137
  rescue LoadError
70
138
  # Ignore
71
139
  end
72
140
 
73
- def print_help_and_exit
74
- puts <<~HELP
75
- Usage:
76
- i18nize <to_locale> [--from en]
77
-
78
- Examples:
79
- i18nize cs
80
- i18nize fr --from en
81
- HELP
82
- exit 0
83
- end
84
-
85
141
  def color(text, code) = "\e[#{code}m#{text}\e[0m"
86
142
 
87
143
  def red(text) = color(text, 31)
@@ -91,5 +147,24 @@ module I18nize
91
147
  def yellow(text) = color(text, 33)
92
148
 
93
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
94
169
  end
95
170
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module I18nize
4
- VERSION = "0.4.2.1"
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.1
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)