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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4340ee480a489883cce8353c75041bde4eb9dbfe
4
- data.tar.gz: 1594ac6888144bc39839212367fb08162b2111c3
3
+ metadata.gz: 2b977f52a75eb2ac75ad49a3eae7c7f3e62a1a90
4
+ data.tar.gz: 2decf0c1b5941fab428a3923b2b14f4e3e571522
5
5
  SHA512:
6
- metadata.gz: 93f570f0f7915e7e9c2150921972836a9ba5d34881bf55bfb0cbdddf1053ffecdcd83b60a6f0626ae06ecd11824ff9bd9c7055062756cc4d5feb7218e88769f6
7
- data.tar.gz: 003b1b9df9caf4af64d9fcfe7b1a1f4aa88d14238801205aa86e40cc32feb10bdcc7b58fa447d433b1b7f07f462f4dd72c6684486047d0580acf5181f0dad04e
6
+ metadata.gz: 12a7429ac8d9c188b0f114e89c63b3b094f73e92b070d1fd7586731b1a5c1de41ce01cf5003e3bcd0051585d7ed8dae54637768de69e83635f0f006fd9c755f2
7
+ data.tar.gz: 77af9b7518bce320ef4286273f0b9d3e31f85097ea427022995407a2908606f2d600cac61611557253ccaa2c985c81880188449eca3f56ccfc40b8e48a508fe9
@@ -1,89 +1,96 @@
1
1
  require 'easy_translate'
2
2
 
3
3
  module Autolang
4
- def self.extract_msgid(text)
5
- return unless text =~ /^msgid/
6
- return unless msgid = text.scan(/"(.+)"/)[0]
7
- msgid.first.to_s.gsub(' | ','|')
8
- end
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
- def self.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)
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
- def self.translate_gettext_into_new_file(pot_file, language)
20
- po_file = File.join(File.dirname(pot_file), language, "#{language}.po")
19
+ private
21
20
 
22
- # create directory if it does not exist
23
- language_dir = File.dirname(po_file)
24
- unless FileTest.exist?(language_dir)
25
- puts "Creating new language directory: #{language_dir}"
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
- # generate po file if it does not exist
30
- unless FileTest.exist?(po_file)
31
- puts "Generating new language file: #{po_file}"
32
- `msginit -i #{pot_file} -o #{po_file} -l #{language} --no-translator`
33
- unless $?.success?
34
- raise "Error during initialization, make sure gettext is installed"
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
- lines = translate_po_file_content(File.readlines(po_file), language)
39
- File.open(po_file, "w+") { |f| f.write(lines*"\n") }
40
- end
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
- def self.translate_json_into_new_file(file, language)
43
- require 'json'
44
- out = File.join(File.dirname(file), "#{language}.json")
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
- def self.translate_hash(hash, language)
51
- hash.inject({}) do |all, (k,v)|
52
- all[k] = if v.is_a?(String)
53
- translate(v, language)
54
- elsif v.is_a?(Hash)
55
- translate_hash(v, language)
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
- def self.translate_po_file_content(lines, language)
64
- msgstr = ""
65
- puts "Translating..."
66
- lines.map do |line|
67
- #read string to translate
68
- if msgid = extract_msgid(line)
69
- msgstr = translate(msgid, language)
70
-
71
- puts msgid
72
- puts msgstr
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
- def self.translate(text, language)
85
- e = TranslationEscaper.new(text)
86
- e.unescape EasyTranslate.translate(e.escaped, :to => language, :format => 'html')
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
@@ -1,3 +1,3 @@
1
1
  module Autolang
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autolang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Blackburn