autolang 0.1.0 → 0.1.1

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/autolang.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{autolang}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris Blackburn", "Michael Grosser"]
data/lib/autolang.rb CHANGED
@@ -5,10 +5,50 @@ class Autolang
5
5
  VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
6
6
 
7
7
  def self.extract_msgid(text)
8
- return nil if text.match(/^msgid/).nil?
9
- msgid = text.scan(/"(.+)"/)[0]
10
- return nil if msgid.nil?
11
- msgid[0].to_s.gsub(' | ','|')
8
+ return unless text =~ /^msgid/
9
+ return unless msgid = text.scan(/"(.+)"/)[0]
10
+ msgid.to_s.gsub(' | ','|')
11
+ end
12
+
13
+ def self.translate_into_new_language(pot_file, language)
14
+ po_file = File.join(File.dirname(pot_file), language, "#{language}.po")
15
+
16
+ # create directory if it does not exist
17
+ language_dir = File.dirname(po_file)
18
+ unless FileTest.exist?(language_dir)
19
+ puts "Creating new language directory: #{language_dir}"
20
+ Dir.mkdir(language_dir)
21
+ end
22
+
23
+ # generate po file if it does not exist
24
+ unless FileTest.exist?(po_file)
25
+ puts "Generating new language file: #{po_file}"
26
+ `msginit -i #{pot_file} -o #{po_file} -l #{language} --no-translator`
27
+ end
28
+
29
+ lines = translate_po_file_content(File.readlines(po_file))
30
+ File.open(po_file, "w+"){|f| f.write(lines*"\n") }
31
+ end
32
+
33
+ def self.translate_po_file_content(lines)
34
+ msgstr = ""
35
+ puts "Translating..."
36
+ lines.map do |line|
37
+ #read string to translate
38
+ if msgid = extract_msgid(line)
39
+ msgstr = translate(msgid)
40
+
41
+ puts msgid
42
+ puts msgstr
43
+ puts '-'*80
44
+
45
+ #replace translation
46
+ elsif line =~ /^msgstr/ and not msgstr.empty?
47
+ line = "msgstr \"#{msgstr}\""
48
+ end
49
+
50
+ line.strip
51
+ end
12
52
  end
13
53
 
14
54
  def self.translate(text)
@@ -28,48 +28,6 @@ namespace :autolang do
28
28
  exit
29
29
  end
30
30
 
31
- pot_file = ENV['POT_FILE']
32
- po_file = File.join(File.dirname(pot_file),ENV['L'],"#{ENV['L']}.po")
33
-
34
- # If the directory doesn't exist created it
35
- lang_dir = File.dirname(po_file)
36
- if !FileTest.exist?(lang_dir)
37
- puts "Creating new language directory: #{lang_dir}"
38
- Dir.mkdir(lang_dir)
39
- end
40
-
41
- # copy the main po file if it doesn't exist
42
- if !FileTest.exist?(po_file)
43
- puts "Generating new language file: #{po_file}"
44
- `msginit -i #{pot_file} -o #{po_file} -l #{ENV['L']}`
45
- end
46
-
47
- # translate existing po file
48
- lines = []
49
- msgid = ""
50
- msgstr = ""
51
- puts "Translating..."
52
- File.foreach(po_file) do |line|
53
- #read string to translate
54
- if msgid = Autolang.extract_msgid(line)
55
- puts msgid
56
- puts msgstr = Autolang.translate(msgid)
57
- puts '-'*80
58
-
59
- #replace translation
60
- elsif line =~ /^msgstr/
61
- unless msgstr.empty?
62
- line = "msgstr \"#{msgstr}\""
63
- end
64
- end
65
-
66
- #output to po file
67
- lines << line.strip
68
- end
69
-
70
- #write new translation file
71
- File.open(po_file, "w+") do |file|
72
- file.write(lines*"\n")
73
- end
31
+ Autolang.translate_into_new_language(ENV['POT_FILE'], ENV['L'])
74
32
  end
75
33
  end
@@ -5,6 +5,8 @@ require 'mocha'
5
5
  $LOAD_PATH.unshift 'lib'
6
6
  require 'autolang'
7
7
 
8
+ load 'lib/tasks/autolang.rake'
9
+
8
10
  describe Autolang do
9
11
  it "has a VERSION" do
10
12
  Autolang::VERSION.should =~ /^\d+\.\d+\.\d+$/
@@ -82,3 +84,23 @@ describe Autolang::TranslationEscaper do
82
84
  end
83
85
  end
84
86
  end
87
+
88
+ describe 'translate pot file' do
89
+ delete = lambda{ `rm -rf spec/fixtures && mkdir spec/fixtures` }
90
+
91
+ before &delete
92
+ after &delete
93
+
94
+ before do
95
+ pot = 'spec/fixtures/xxx.pot'
96
+ File.open(pot, 'w'){|f| f.write(%Q{msgid "hello"\nmsgstr ""}) }
97
+ ENV['POT_FILE'] = pot
98
+ ENV['L'] = 'de'
99
+ @po = 'spec/fixtures/de/de.po'
100
+ end
101
+
102
+ it "translates all msgids" do
103
+ Rake::Task['autolang:translate'].execute
104
+ File.read(@po).should include(%Q{msgid "hello"\nmsgstr "hallo"})
105
+ end
106
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autolang
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Chris Blackburn