autolang 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/autolang.rb +75 -68
- data/lib/autolang/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2b977f52a75eb2ac75ad49a3eae7c7f3e62a1a90
|
4
|
+
data.tar.gz: 2decf0c1b5941fab428a3923b2b14f4e3e571522
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12a7429ac8d9c188b0f114e89c63b3b094f73e92b070d1fd7586731b1a5c1de41ce01cf5003e3bcd0051585d7ed8dae54637768de69e83635f0f006fd9c755f2
|
7
|
+
data.tar.gz: 77af9b7518bce320ef4286273f0b9d3e31f85097ea427022995407a2908606f2d600cac61611557253ccaa2c985c81880188449eca3f56ccfc40b8e48a508fe9
|
data/lib/autolang.rb
CHANGED
@@ -1,89 +1,96 @@
|
|
1
1
|
require 'easy_translate'
|
2
2
|
|
3
3
|
module Autolang
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
4
|
+
class << self
|
5
|
+
def translate(text, language)
|
6
|
+
e = TranslationEscaper.new(text)
|
7
|
+
e.unescape EasyTranslate.translate(e.escaped, :to => language, :format => 'html')
|
8
|
+
end
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
10
|
+
def translate_into_new_language(key, file, language)
|
11
|
+
EasyTranslate.api_key = key
|
12
|
+
if file.end_with?(".json")
|
13
|
+
translate_json_into_new_file(file, language)
|
14
|
+
else
|
15
|
+
translate_gettext_into_new_file(file, language)
|
16
|
+
end
|
16
17
|
end
|
17
|
-
end
|
18
18
|
|
19
|
-
|
20
|
-
po_file = File.join(File.dirname(pot_file), language, "#{language}.po")
|
19
|
+
private
|
21
20
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
Dir.mkdir(language_dir)
|
21
|
+
def extract_msgid(text)
|
22
|
+
return unless text =~ /^msgid/
|
23
|
+
return unless msgid = text.scan(/"(.+)"/)[0]
|
24
|
+
msgid.first.to_s.gsub(' | ','|')
|
27
25
|
end
|
28
26
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
27
|
+
def translate_gettext_into_new_file(pot_file, language)
|
28
|
+
po_file = File.join(File.dirname(pot_file), language, "#{language}.po")
|
29
|
+
|
30
|
+
# create directory if it does not exist
|
31
|
+
language_dir = File.dirname(po_file)
|
32
|
+
unless FileTest.exist?(language_dir)
|
33
|
+
puts "Creating new language directory: #{language_dir}"
|
34
|
+
Dir.mkdir(language_dir)
|
35
35
|
end
|
36
|
-
end
|
37
36
|
|
38
|
-
|
39
|
-
|
40
|
-
|
37
|
+
# generate po file if it does not exist
|
38
|
+
unless FileTest.exist?(po_file)
|
39
|
+
if system("which msginit")
|
40
|
+
puts "Generating new language file: #{po_file}"
|
41
|
+
`msginit -i #{pot_file} -o #{po_file} -l #{language} --no-translator`
|
42
|
+
else
|
43
|
+
warn "msginit not available creating new language by copying"
|
44
|
+
`cp #{pot_file} #{po_file}`
|
45
|
+
end
|
46
|
+
raise "Error during initialization" unless $?.success?
|
47
|
+
end
|
41
48
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
old = JSON.load(File.read(file))
|
46
|
-
new = translate_hash(old, language)
|
47
|
-
File.open(out, "w+") { |f| f.write(JSON.dump(new)) }
|
48
|
-
end
|
49
|
+
lines = translate_po_file_content(File.readlines(po_file), language)
|
50
|
+
File.open(po_file, "w+") { |f| f.write(lines*"\n") }
|
51
|
+
end
|
49
52
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
else
|
57
|
-
v
|
58
|
-
end
|
59
|
-
all
|
53
|
+
def translate_json_into_new_file(file, language)
|
54
|
+
require 'json'
|
55
|
+
out = File.join(File.dirname(file), "#{language}.json")
|
56
|
+
old = JSON.load(File.read(file))
|
57
|
+
new = translate_hash(old, language)
|
58
|
+
File.open(out, "w+") { |f| f.write(JSON.dump(new)) }
|
60
59
|
end
|
61
|
-
end
|
62
60
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
puts '-'*80
|
74
|
-
|
75
|
-
#replace translation
|
76
|
-
elsif line =~ /^msgstr/ and not msgstr.empty?
|
77
|
-
line = "msgstr \"#{msgstr}\""
|
61
|
+
def translate_hash(hash, language)
|
62
|
+
hash.inject({}) do |all, (k,v)|
|
63
|
+
all[k] = if v.is_a?(String)
|
64
|
+
translate(v, language)
|
65
|
+
elsif v.is_a?(Hash)
|
66
|
+
translate_hash(v, language)
|
67
|
+
else
|
68
|
+
v
|
69
|
+
end
|
70
|
+
all
|
78
71
|
end
|
79
|
-
|
80
|
-
line.strip
|
81
72
|
end
|
82
|
-
end
|
83
73
|
|
84
|
-
|
85
|
-
|
86
|
-
|
74
|
+
def translate_po_file_content(lines, language)
|
75
|
+
msgstr = ""
|
76
|
+
puts "Translating..."
|
77
|
+
lines.map do |line|
|
78
|
+
#read string to translate
|
79
|
+
if msgid = extract_msgid(line)
|
80
|
+
msgstr = translate(msgid, language)
|
81
|
+
|
82
|
+
puts msgid
|
83
|
+
puts msgstr
|
84
|
+
puts '-'*80
|
85
|
+
|
86
|
+
#replace translation
|
87
|
+
elsif line =~ /^msgstr/ and not msgstr.empty?
|
88
|
+
line = "msgstr \"#{msgstr}\""
|
89
|
+
end
|
90
|
+
|
91
|
+
line.strip
|
92
|
+
end
|
93
|
+
end
|
87
94
|
end
|
88
95
|
|
89
96
|
# protects text from evil translation robots
|
data/lib/autolang/version.rb
CHANGED