-
Notifications
You must be signed in to change notification settings - Fork 1
/
ymlKeys.rb
353 lines (308 loc) · 10.3 KB
/
ymlKeys.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# -*- encoding : utf-8 -*-
require 'yaml'
require 'clipboard'
#windows
require 'ffi'
class YmlKeys
def initialize(f = nil , a = nil, l = 'fr')
@folder = f
@locale = l
@alias = a.nil? ? @folder : a
@path = "bo.facile/app/views/#{@folder}/"
end
def self.ignore(t = nil)
t = self.left[0] if t.nil?
File.open("yml_to_ignore.yml", "a+") do |f|
f.puts(" #{t}: \"#{t}\"" )
end
end
def repeated
to_delete = []
load_file
@keys.keys.each do |k|
if @keys.has_key?(k)
puts("\n#{k}")
end
end
end
def clear
to_delete = []
to_save = []
load_file
@keys.keys.each do |k|
if exist?(":#{k}", false) == true
to_save << k
else
to_delete << k
@keys.delete(k)
end
end
if to_delete.size > 1
msg = "Saved Keys : #{to_save.size}, Time : #{Time.now}"
create_file("locale.#{@locale}_saved_keys.yml", msg , to_save)
msg = "Deleted Keys : #{to_delete.size}, Time : #{Time.now}"
create_file("locale.#{@locale}_deleted_keys.yml", msg , to_delete)
create_yml
@keys.each do |k|
fill_yml(k)
end
else
puts("All the #{@keys.size} exist, the yml file is updated")
end
return true
end
def k
Clipboard.copy("K_I18N_ENGLISH_INSIDE = true")
puts('K_I18N_ENGLISH_INSIDE = true')
end
def create_file(title, msg, data)
File.open("#{@path}/#{title}", "w+") do |f|
f.puts(msg)
data.each { |element| f.puts(" #{element}") }
end
end
def exist?(query = nil, log = true)
query = Clipboard.paste if query.nil?
results = []
entries
entries.each do |file|
#names = File.readlines("#{@path}/#{file}")
#matches = names.select { |name| name[/#{query}/] }
#results << matches
unless File.directory?(File.join(@path, file))
if accepted?(file)
File.foreach("#{@path}#{file}") do |line|
results << line if line["#{query}"]
end
end
end
end
if results.size > 0
puts "Yes, #{query} has #{results.size} matches" if log
return true
else
puts "No, #{query} has no matches"
end
return false
end
def accepted?(file)
f = file.split('.').last
return false if f == 'yml'
return false if f == 'html'
return true
end
def setdir
puts Dir.getwd
end
def entries
tmpd = Dir.getwd
#"/var/www/incwo-dev/dev-bo"
Dir.chdir(@path)
rbfiles = File.join("**", "*")
entries = Dir.glob(rbfiles)
Dir.chdir(tmpd)
return entries
end
def self.get_folders
tmpd = Dir.getwd
#"/var/www/incwo-dev/dev-bo"
Dir.chdir('bo.facile/app/views/')
rbfiles = File.join("**")
entries = Dir.glob(rbfiles)
Dir.chdir(tmpd)
return entries
end
def next
self.next
end
def self.next
results = []
folders = self.get_folders
to_ignore = YAML.load_file('yml_to_ignore.yml')
folders.each do |folder|
if File.file?(File.join("bo.facile/app/views/#{folder}", 'locale.fr.yml'))
else
r = to_ignore['ignore'].key(folder)
if(r.nil?)
results << folder
else
end
end
end
return YmlKeys.new(results[0])
end
def self.left
results = []
folders = self.get_folders
folders.each do |folder|
if File.file?(File.join("bo.facile/app/views/#{folder}", 'locale.fr.yml'))
else
results << folder
end
end
return results.sort
end
def create_yml
File.open("#{@path}/locale.#{@locale}.yml", "w+") do |f|
f.puts("---\n#{@locale}:")
f.puts(" #{@alias}_key_test: \"test\"")
end
end
def fill_yml(t)
o = " #{t[0]}: \"#{t[1]}\""
File.open("#{@path}/locale.#{@locale}.yml",'a+') do |file|
file.puts(o)
puts("Added => #{o}")
end
end
def load_file
#default path for incwo yml files.
#@path to set a full path
@yml_path = "#{@path}locale.#{@locale}.yml"
if @folder.nil? || @path.nil?
return "No folder/ No path"
else
@yml = YAML.load_file(@yml_path)
@keys = @yml[@locale]
end
end
def find(t = nil, h = nil, l = nil)
#windows only
t = Clipboard.paste.encode('UTF-8') if t.nil?
# t = Clipboard.paste if t.nil?
t = t.gsub("\n", " ")
t = t.gsub("\t", " ")
r = nil
load_file
r = @keys.key(t)
#r = @keys.index{|x|===t}
if(r.nil?)
r = add_key(t)
else
puts("Already exists:")
#puts("#{@keys[r]}")
end
output(r, h, l)
end
def test
find('Test', nil, nil)
end
def findh
find(nil, true, nil)
end
def findl
find(nil, nil, true)
end
def output(k, h = nil, l = nil)
#puts "I18n.t(:#{k})"
if h.nil?
if l.nil?
Clipboard.copy("I18n.t(:#{k})")
else
Clipboard.copy("I18n.t(:#{k}, :locale => in_language_code)")
end
else
Clipboard.copy("<%= I18n.t(:#{k})%>")
end
return k
end
def i18n
puts("ready_for_i18n /var/www/incwo-dev/dev-bo/app/views/#{@folder} /var/www/incwo-dev/dev-bo/app/views/#{@folder} --locale fr --ext '.rhtml' > /var/www/incwo-dev/dev-bo/app/views/#{@folder}/locale.fr.yml")
end
def add_key(t)
key = text_to_key(t)
k = "#{@alias}_key_#{key}"
t = t.gsub('"', '\"')
o = " #{k}: \"#{t}\""
File.open(@yml_path,'a+') do |file|
file.puts(o)
#puts("Added => #{o}")
puts("Added")
end
return k
end
def text_to_key(t)
n = transliterate(t)
#my_string.delete('^a-zA-Z')
#n = n.gsub(/(\W|\d)/, "_")
n = n.gsub(/(\W)/, "_")
#n = n.gsub('"', '\"')
n = n.downcase
n = n.gsub('__','_')
n = n.gsub('____','_')
n = n.gsub('_____','_')
return n
end
def transliterate(s)
default_aproximations if @aproximations.nil?
chars = s.split(//)
chars.each_with_index do |value , index|
n = nil
n = @aproximations[value]
unless (n.nil?)
chars[index] = n
end
end
return chars.join()
end
# extracted from:
#https://github.com/svenfuchs/i18n/blob/master/lib/i18n/backend/transliterator.rb
def default_aproximations
@aproximations = {
"¹"=>"1", "²"=>"2", "³"=>"3",
"À"=>"A", "Á"=>"A", "Â"=>"A", "Ã"=>"A", "Ä"=>"A", "Å"=>"A", "Æ"=>"AE",
"Ç"=>"C", "È"=>"E", "É"=>"E", "Ê"=>"E", "Ë"=>"E", "Ì"=>"I", "Í"=>"I",
"Î"=>"I", "Ï"=>"I", "Ð"=>"D", "Ñ"=>"N", "Ò"=>"O", "Ó"=>"O", "Ô"=>"O",
"Õ"=>"O", "Ö"=>"O", "×"=>"x", "Ø"=>"O", "Ù"=>"U", "Ú"=>"U", "Û"=>"U",
"Ü"=>"U", "Ý"=>"Y", "Þ"=>"Th", "ß"=>"ss", "à"=>"a", "á"=>"a", "â"=>"a",
"ã"=>"a", "ä"=>"a", "å"=>"a", "æ"=>"ae", "ç"=>"c", "è"=>"e", "é"=>"e",
"ê"=>"e", "ë"=>"e", "ì"=>"i", "í"=>"i", "î"=>"i", "ï"=>"i", "ð"=>"d",
"ñ"=>"n", "ò"=>"o", "ó"=>"o", "ô"=>"o", "õ"=>"o", "ö"=>"o", "ø"=>"o",
"ù"=>"u", "ú"=>"u", "û"=>"u", "ü"=>"u", "ý"=>"y", "þ"=>"th", "ÿ"=>"y",
"Ā"=>"A", "ā"=>"a", "Ă"=>"A", "ă"=>"a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t", "Ŧ"=>"T", "ŧ"=>"t",
"Ũ"=>"U", "ũ"=>"u", "Ū"=>"U", "ū"=>"u", "Ŭ"=>"U", "ŭ"=>"u", "Ů"=>"U",
"ů"=>"u", "Ű"=>"U", "ű"=>"u", "Ų"=>"U", "ų"=>"u", "Ŵ"=>"W", "ŵ"=>"w",
"Ŷ"=>"Y", "ŷ"=>"y", "Ÿ"=>"Y", "Ź"=>"Z", "ź"=>"z", "Ż"=>"Z", "ż"=>"z",
"Ž"=>"Z", "ž"=>"z", "·"=>"_", "¿"=>"_", "¡"=>"_"
}
end
end
#para mejorar : keys mas cortas. buscar entre keys para generarlas
#borrar desde consola
#editar keys
#escapear comillas internas
#revisar keys repetidas
#revison de textto caracter por caracter
#unused code
#File.open("app/views/#{@folder}/locale.fr.yml", 'w') do |h|
# h.write @yml
#end
#k[key] = test
#k = {"fr" =>{ "#{key}" => test}}
#@yml[@locale]["#{@folder}_test"] = test
#@yml << k
# File.open("app/views/#{@folder}/locale.fr.yml", "r") do |f|
# f.each_line do |line|
# @yml << line
# end
# end
#string.gsub(/[^\x00-\x7f]/u) do |char|
#@aproximations[char] || replacement || '?'
#I18n.available_locales = [:fr]
#n = I18n.transliterate(t)
# in_language_code = 'fr' if in_language_code.to_s == '' K_I18N_ENGLISH_INSIDE
#