canvas-i18n-editor 0.0.1 → 0.0.2
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 +4 -4
- data/bin/canvas-i18n-editor +44 -27
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cce046eb29fbec10d9be6d2e2fb4e34c5e24f82c55d78182e62529c64e0759a9
|
4
|
+
data.tar.gz: 9f0a327f99ee1d5c947605d0a4012bf24ec28ffc84800d65c20de7d4b62636c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b042981a09e3528b27e5cd1dd7a177cf3637bf3f336a6db76f8915446acfe272e3f4ff85f095d9dadd360870baf8a3d153b6cec6fe02b6301eec90a17ce66ac
|
7
|
+
data.tar.gz: 72c40f2f42a805af9d317aae0d8dc8e19515b29f88b828bc818c8935ab65463d6a84abc769b8324fd70779c1a61813d9860a5c7124c950e50920d6646fb4c817
|
data/bin/canvas-i18n-editor
CHANGED
@@ -8,8 +8,6 @@ require "json"
|
|
8
8
|
|
9
9
|
Options = Struct.new(:translate_from, :translate_to, :mode, :apply_js)
|
10
10
|
|
11
|
-
ACCEPTED_MODE = %w[canvas rails reverse].freeze
|
12
|
-
|
13
11
|
LOCALE_FILE = "config/locales/zh-Hant.yml"
|
14
12
|
JS_LOCALE_FILE = "public/javascripts/translations/zh-Hant.json"
|
15
13
|
TMP_FILE = "/tmp/canvas-i18n"
|
@@ -20,26 +18,29 @@ def update_rails_i18n(args)
|
|
20
18
|
# find key in config/locales/zh-Hant.yml
|
21
19
|
data = File.readlines(LOCALE_FILE)
|
22
20
|
keywords = raw_key.split(".")
|
23
|
-
current_layer = 1
|
24
21
|
get_layer = -> { _1.match(/^\s*/)[0].size / 2 }
|
22
|
+
current_layer = 1
|
23
|
+
found_key_count = 0
|
25
24
|
updated_flag = false
|
26
25
|
File.open(TMP_FILE, "w:UTF-8") do |file|
|
27
26
|
data[0..3].each { file.write(_1) }
|
28
27
|
(4..data.size - 1).each do |line_index|
|
29
28
|
line = data[line_index]
|
30
|
-
|
31
|
-
|
29
|
+
next_line = data[line_index + 1]
|
30
|
+
current_layer = get_layer[line]
|
31
|
+
if !updated_flag && current_layer == found_key_count + 1
|
32
|
+
if line.match?(/^\s{#{current_layer * 2}}#{keywords[0]}:/)
|
32
33
|
line_number = line_index + 1
|
33
34
|
key = keywords.shift
|
34
|
-
|
35
|
+
found_key_count += 1
|
35
36
|
puts "find #{key} in line #{line_number}"
|
36
37
|
# find last key, should update content now
|
37
38
|
if keywords.empty?
|
38
39
|
line = line.sub(/#{key}:\s?/, "#{key}: ").sub(/(?<=#{key}:\s).+/, "\"#{args.translate_to}\"")
|
39
40
|
updated_flag = true
|
40
41
|
end
|
41
|
-
elsif line_index
|
42
|
-
#
|
42
|
+
elsif keywords.join(".") != raw_key && (line_index >= data.size - 1 || get_layer[next_line] < current_layer)
|
43
|
+
# there's at least one key found and reach last line in this scope
|
43
44
|
update_nested_key(file, keywords, current_layer, args)
|
44
45
|
updated_flag = true
|
45
46
|
end
|
@@ -48,7 +49,9 @@ def update_rails_i18n(args)
|
|
48
49
|
file.write(line)
|
49
50
|
end
|
50
51
|
|
51
|
-
|
52
|
+
unless updated_flag
|
53
|
+
update_nested_key(file, keywords, current_layer, args)
|
54
|
+
end
|
52
55
|
file.close
|
53
56
|
end
|
54
57
|
FileUtils.mv(TMP_FILE, LOCALE_FILE)
|
@@ -57,21 +60,32 @@ end
|
|
57
60
|
|
58
61
|
def update_nested_key(file, keywords, current_layer, args)
|
59
62
|
keywords[0..-2].each do |key|
|
60
|
-
file.write("#{
|
63
|
+
file.write("#{" " * 2 * current_layer}#{key}: \n")
|
61
64
|
current_layer += 1
|
62
65
|
end
|
63
|
-
file.write("#{
|
66
|
+
file.write("#{" " * 2 * current_layer}#{keywords[-1]}: #{args.translate_to}\n")
|
64
67
|
end
|
65
68
|
|
66
69
|
def update_js(key, translate_to, apply_js)
|
70
|
+
add_js_key unless File.exist?(JS_LOCALE_FILE)
|
67
71
|
data = File.read(JS_LOCALE_FILE)
|
68
|
-
|
69
|
-
|
72
|
+
key_part = data.match(/(?<="#{key}":)"[^"]+"/)
|
73
|
+
if key_part
|
74
|
+
data = data.sub(key_part[0], "\"#{translate_to}\"")
|
75
|
+
puts "update js translations"
|
76
|
+
File.write(JS_LOCALE_FILE, data)
|
77
|
+
else
|
78
|
+
add_js_key
|
79
|
+
end
|
70
80
|
|
71
|
-
File.write(JS_LOCALE_FILE, data)
|
72
81
|
generate_hashed_js if apply_js
|
73
82
|
end
|
74
83
|
|
84
|
+
def add_js_key
|
85
|
+
puts "regenerate js translations"
|
86
|
+
`bundle exec rails i18n:generate_js`
|
87
|
+
end
|
88
|
+
|
75
89
|
def generate_hashed_js
|
76
90
|
`rm public/dist/javascripts/translations/zh-Hant-*.json`
|
77
91
|
puts "generate scoped js translations"
|
@@ -116,22 +130,24 @@ def update_from_content(args)
|
|
116
130
|
# TODO: use key(full key) to update_js
|
117
131
|
end
|
118
132
|
|
133
|
+
def ask_translate_to(translate_from)
|
134
|
+
printf "what's the content you want to translate \"#{translate_from}\" to ? "
|
135
|
+
STDIN.gets
|
136
|
+
end
|
137
|
+
|
119
138
|
class Parser
|
120
139
|
def self.parse(user_input)
|
140
|
+
user_input.push("-h") if user_input.empty?
|
121
141
|
args = Options.new
|
122
142
|
|
123
143
|
opt_parser = OptionParser.new do |opts|
|
124
144
|
opts.banner = "Usage:
|
125
145
|
cool_i18n_edit translateFrom translateTo [options]
|
126
146
|
|
127
|
-
cool_i18n_edit
|
128
|
-
cool_i18n_edit
|
147
|
+
cool_i18n_edit 'Click Me' '點我' # edit i18n using canvas liner-style key
|
148
|
+
cool_i18n_edit 'controller_name.action_name.click_me' '點我' # edit i18n using rails i18n key
|
129
149
|
"
|
130
150
|
|
131
|
-
opts.on("-m MODE", "--mode MODE", "canvas, rails or reverse") do |mode|
|
132
|
-
args.mode = mode
|
133
|
-
end
|
134
|
-
|
135
151
|
opts.on("-a", "--apply", "apply js translations") do
|
136
152
|
args.apply_js = true
|
137
153
|
end
|
@@ -143,11 +159,14 @@ class Parser
|
|
143
159
|
end
|
144
160
|
|
145
161
|
opt_parser.parse!(user_input) # options are remove after this line
|
146
|
-
raise "-m option not set" unless args.mode
|
147
|
-
raise "invalid -m option #{args.mode}\nonly below types are allowed\n#{ACCEPTED_MODE.join(",")}" unless ACCEPTED_MODE.include?(args.mode)
|
148
162
|
|
149
163
|
args.translate_from = user_input[0].strip
|
150
|
-
args.translate_to = user_input[1].strip
|
164
|
+
args.translate_to = (user_input[1] || ask_translate_to(args.translate_from)).strip
|
165
|
+
args.mode = if args.translate_from.match?(/([a-zA-Z]+\.)+[a-zA-Z]+/)
|
166
|
+
:rails
|
167
|
+
else
|
168
|
+
:canvas
|
169
|
+
end
|
151
170
|
|
152
171
|
args
|
153
172
|
end
|
@@ -155,10 +174,8 @@ end
|
|
155
174
|
|
156
175
|
args = Parser.parse(ARGV)
|
157
176
|
case args.mode
|
158
|
-
in
|
177
|
+
in :rails
|
159
178
|
update_rails_i18n(args)
|
160
|
-
in
|
179
|
+
in :canvas
|
161
180
|
update_canvas_i18n(args)
|
162
|
-
in "reverse"
|
163
|
-
update_from_content(args)
|
164
181
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canvas-i18n-editor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-06-
|
11
|
+
date: 2023-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: i18n edit
|
14
14
|
email: 0711kps@gmail.com
|
@@ -18,7 +18,7 @@ extensions: []
|
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
20
|
- bin/canvas-i18n-editor
|
21
|
-
homepage: https://rubygems.org/gems/
|
21
|
+
homepage: https://rubygems.org/gems/canvas-i18n-editor
|
22
22
|
licenses:
|
23
23
|
- MIT
|
24
24
|
metadata: {}
|