autolang 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4340ee480a489883cce8353c75041bde4eb9dbfe
4
+ data.tar.gz: 1594ac6888144bc39839212367fb08162b2111c3
5
+ SHA512:
6
+ metadata.gz: 93f570f0f7915e7e9c2150921972836a9ba5d34881bf55bfb0cbdddf1053ffecdcd83b60a6f0626ae06ecd11824ff9bd9c7055062756cc4d5feb7218e88769f6
7
+ data.tar.gz: 003b1b9df9caf4af64d9fcfe7b1a1f4aa88d14238801205aa86e40cc32feb10bdcc7b58fa447d433b1b7f07f462f4dd72c6684486047d0580acf5181f0dad04e
data/bin/autolang CHANGED
@@ -1,9 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
- require 'rubygems'
2
+ require "rubygems" if RUBY_VERSION < "1.9"
3
3
  require 'optparse'
4
4
 
5
- $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
5
+ $LOAD_PATH << File.expand_path("../../lib", __FILE__)
6
6
  require 'autolang'
7
+ require 'autolang/version'
7
8
 
8
9
  OptionParser.new do |opts|
9
10
  opts.banner = <<BANNER
@@ -11,6 +12,7 @@ OptionParser.new do |opts|
11
12
  Usage:
12
13
  autolang /path/to/app.pot <language-code>
13
14
  autolang /path/to/app.pot es
15
+ autolang /path/to/app.json es
14
16
 
15
17
  language-code are 2 letter ISO 639 codes
16
18
  if you do not have a pot file, use gettext and updatepo first (google helps...)
@@ -22,7 +24,7 @@ BANNER
22
24
  opts.on('-v', '--version','Show Version'){ puts Autolang::VERSION; exit}
23
25
  end.parse!
24
26
 
25
- if ARGV.size < 2 or not File.exist?(ARGV.first)
27
+ if ARGV.size < 3 or not File.exist?(ARGV[1])
26
28
  puts "Usage instructions: autolang --help"
27
29
  exit
28
30
  end
data/lib/autolang.rb CHANGED
@@ -1,16 +1,22 @@
1
- require 'rtranslate'
2
- require 'rtranslate/language'
3
-
4
- class Autolang
5
- VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
1
+ require 'easy_translate'
6
2
 
3
+ module Autolang
7
4
  def self.extract_msgid(text)
8
5
  return unless text =~ /^msgid/
9
6
  return unless msgid = text.scan(/"(.+)"/)[0]
10
- msgid.to_s.gsub(' | ','|')
7
+ msgid.first.to_s.gsub(' | ','|')
8
+ end
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)
16
+ end
11
17
  end
12
18
 
13
- def self.translate_into_new_language(pot_file, language)
19
+ def self.translate_gettext_into_new_file(pot_file, language)
14
20
  po_file = File.join(File.dirname(pot_file), language, "#{language}.po")
15
21
 
16
22
  # create directory if it does not exist
@@ -24,10 +30,34 @@ class Autolang
24
30
  unless FileTest.exist?(po_file)
25
31
  puts "Generating new language file: #{po_file}"
26
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"
35
+ end
27
36
  end
28
37
 
29
38
  lines = translate_po_file_content(File.readlines(po_file), language)
30
- File.open(po_file, "w+"){|f| f.write(lines*"\n") }
39
+ File.open(po_file, "w+") { |f| f.write(lines*"\n") }
40
+ end
41
+
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
+
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
60
+ end
31
61
  end
32
62
 
33
63
  def self.translate_po_file_content(lines, language)
@@ -42,7 +72,7 @@ class Autolang
42
72
  puts msgstr
43
73
  puts '-'*80
44
74
 
45
- #replace translation
75
+ #replace translation
46
76
  elsif line =~ /^msgstr/ and not msgstr.empty?
47
77
  line = "msgstr \"#{msgstr}\""
48
78
  end
@@ -53,8 +83,7 @@ class Autolang
53
83
 
54
84
  def self.translate(text, language)
55
85
  e = TranslationEscaper.new(text)
56
- @translator = Translate::RTranslate.new unless @translator
57
- e.unescape @translator.translate(e.escaped, :from => 'ENGLISH', :to => language, :userip => '127.0.0.1')
86
+ e.unescape EasyTranslate.translate(e.escaped, :to => language, :format => 'html')
58
87
  end
59
88
 
60
89
  # protects text from evil translation robots
@@ -72,7 +101,7 @@ class Autolang
72
101
  remove_placeholders(translation)
73
102
  end
74
103
 
75
- protected
104
+ protected
76
105
 
77
106
  def escape_text
78
107
  @placeholders = []
@@ -0,0 +1,3 @@
1
+ module Autolang
2
+ VERSION = "0.3.0"
3
+ end
metadata CHANGED
@@ -1,103 +1,90 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: autolang
3
- version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
6
- segments:
7
- - 0
8
- - 2
9
- - 0
10
- version: 0.2.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Chris Blackburn
14
8
  - Michael Grosser
15
9
  autorequire:
16
10
  bindir: bin
17
11
  cert_chain: []
18
-
19
- date: 2011-03-19 00:00:00 +01:00
20
- default_executable: autolang
21
- dependencies:
22
- - !ruby/object:Gem::Dependency
23
- version_requirements: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
12
+ date: 2014-05-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: i18n
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
26
18
  - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: easy_translate
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
32
35
  type: :runtime
33
- requirement: *id001
34
- name: i18n
35
36
  prerelease: false
36
- - !ruby/object:Gem::Dependency
37
- version_requirements: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
- requirements:
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
40
39
  - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: json
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
46
49
  type: :runtime
47
- requirement: *id002
48
- name: sishen-rtranslate
49
50
  prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
50
56
  description:
51
57
  email: michael@grosser.it
52
- executables:
58
+ executables:
53
59
  - autolang
54
60
  extensions: []
55
-
56
61
  extra_rdoc_files: []
57
-
58
- files:
59
- - Gemfile
60
- - Gemfile.lock
61
- - Rakefile
62
- - Readme.md
63
- - VERSION
64
- - autolang.gemspec
62
+ files:
65
63
  - bin/autolang
66
64
  - lib/autolang.rb
67
- - spec/autolang_spec.rb
68
- has_rdoc: true
69
- homepage: http://github.com/grosser/autolang
70
- licenses: []
71
-
65
+ - lib/autolang/version.rb
66
+ homepage: https://github.com/grosser/autolang
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
72
70
  post_install_message:
73
71
  rdoc_options: []
74
-
75
- require_paths:
72
+ require_paths:
76
73
  - lib
77
- required_ruby_version: !ruby/object:Gem::Requirement
78
- none: false
79
- requirements:
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
80
76
  - - ">="
81
- - !ruby/object:Gem::Version
82
- hash: 3
83
- segments:
84
- - 0
85
- version: "0"
86
- required_rubygems_version: !ruby/object:Gem::Requirement
87
- none: false
88
- requirements:
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
89
81
  - - ">="
90
- - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
- version: "0"
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
95
84
  requirements: []
96
-
97
85
  rubyforge_project:
98
- rubygems_version: 1.4.2
86
+ rubygems_version: 2.2.2
99
87
  signing_key:
100
- specification_version: 3
88
+ specification_version: 4
101
89
  summary: Kick-start new translation via google translate
102
- test_files:
103
- - spec/autolang_spec.rb
90
+ test_files: []
data/Gemfile DELETED
@@ -1,11 +0,0 @@
1
- source :rubygems
2
-
3
- gem 'i18n'
4
- gem 'sishen-rtranslate'
5
-
6
- group :dev do
7
- gem 'jeweler'
8
- gem 'mocha'
9
- gem 'rspec'
10
- gem 'rake'
11
- end
data/Gemfile.lock DELETED
@@ -1,36 +0,0 @@
1
- GEM
2
- remote: http://rubygems.org/
3
- specs:
4
- activesupport (3.0.4)
5
- diff-lcs (1.1.2)
6
- git (1.2.5)
7
- i18n (0.5.0)
8
- jeweler (1.5.2)
9
- bundler (~> 1.0.0)
10
- git (>= 1.2.5)
11
- rake
12
- json (1.5.1)
13
- mocha (0.9.12)
14
- rake (0.8.7)
15
- rspec (2.5.0)
16
- rspec-core (~> 2.5.0)
17
- rspec-expectations (~> 2.5.0)
18
- rspec-mocks (~> 2.5.0)
19
- rspec-core (2.5.1)
20
- rspec-expectations (2.5.0)
21
- diff-lcs (~> 1.1.2)
22
- rspec-mocks (2.5.0)
23
- sishen-rtranslate (1.5)
24
- activesupport (>= 2.2.0)
25
- json (>= 1.1.3)
26
-
27
- PLATFORMS
28
- ruby
29
-
30
- DEPENDENCIES
31
- i18n
32
- jeweler
33
- mocha
34
- rake
35
- rspec
36
- sishen-rtranslate
data/Rakefile DELETED
@@ -1,18 +0,0 @@
1
- task :default do
2
- sh "rspec spec"
3
- end
4
-
5
- begin
6
- require 'jeweler'
7
- Jeweler::Tasks.new do |gem|
8
- gem.name = 'autolang'
9
- gem.summary = "Kick-start new translation via google translate"
10
- gem.email = "michael@grosser.it"
11
- gem.homepage = "http://github.com/grosser/#{gem.name}"
12
- gem.authors = ["Chris Blackburn", "Michael Grosser"]
13
- end
14
-
15
- Jeweler::GemcutterTasks.new
16
- rescue LoadError
17
- puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler"
18
- end
data/Readme.md DELETED
@@ -1,34 +0,0 @@
1
- Autolang
2
- ========
3
-
4
- - Kick-start your translation!
5
- - Translate all your Gettext - msgids to another language using google-translate.
6
-
7
- Usage
8
- =====
9
-
10
- gem install autolang
11
-
12
- autolang /path/to/app.pot <language-code>
13
- autolang /path/to/app.pot es
14
-
15
- language-code are 2 letter [ISO 639](http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes) codes<br/>
16
- if you have no .pot file, use gettext and updatepo first (google helps...)
17
-
18
- Translation examples
19
- ====================
20
- - Car|Engine -> Motor
21
- - hello %{name} -> hallo %{name}
22
-
23
- TODO
24
- ====
25
- - Do not convert "& to "and", use something 'smarter'.
26
-
27
- Authors
28
- =======
29
- Original by [Chris Blackburn](cbciweb.com) released under MIT license
30
-
31
- Enhanced by
32
-
33
- - [Michael Grosser](http://grosser.it)
34
- - [Hans Engel](http://engel.uk.to/)
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.2.0
data/autolang.gemspec DELETED
@@ -1,50 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{autolang}
8
- s.version = "0.2.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Chris Blackburn", "Michael Grosser"]
12
- s.date = %q{2011-03-19}
13
- s.default_executable = %q{autolang}
14
- s.email = %q{michael@grosser.it}
15
- s.executables = ["autolang"]
16
- s.files = [
17
- "Gemfile",
18
- "Gemfile.lock",
19
- "Rakefile",
20
- "Readme.md",
21
- "VERSION",
22
- "autolang.gemspec",
23
- "bin/autolang",
24
- "lib/autolang.rb",
25
- "spec/autolang_spec.rb"
26
- ]
27
- s.homepage = %q{http://github.com/grosser/autolang}
28
- s.require_paths = ["lib"]
29
- s.rubygems_version = %q{1.4.2}
30
- s.summary = %q{Kick-start new translation via google translate}
31
- s.test_files = [
32
- "spec/autolang_spec.rb"
33
- ]
34
-
35
- if s.respond_to? :specification_version then
36
- s.specification_version = 3
37
-
38
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
39
- s.add_runtime_dependency(%q<i18n>, [">= 0"])
40
- s.add_runtime_dependency(%q<sishen-rtranslate>, [">= 0"])
41
- else
42
- s.add_dependency(%q<i18n>, [">= 0"])
43
- s.add_dependency(%q<sishen-rtranslate>, [">= 0"])
44
- end
45
- else
46
- s.add_dependency(%q<i18n>, [">= 0"])
47
- s.add_dependency(%q<sishen-rtranslate>, [">= 0"])
48
- end
49
- end
50
-
@@ -1,98 +0,0 @@
1
- # coding: utf-8
2
-
3
- require 'rake'
4
- require 'mocha'
5
- $LOAD_PATH.unshift 'lib'
6
- require 'autolang'
7
-
8
- describe Autolang do
9
- it "has a VERSION" do
10
- Autolang::VERSION.should =~ /^\d+\.\d+\.\d+$/
11
- end
12
-
13
- describe :extract_msgid do
14
- it "finds message in text" do
15
- Autolang.extract_msgid('msgid "hello"').should == 'hello'
16
- end
17
-
18
- it "does not find empty message" do
19
- Autolang.extract_msgid('msgid ""').should == nil
20
- end
21
-
22
- it "does not find non-existing message" do
23
- Autolang.extract_msgid('msgstr "hello"').should == nil
24
- end
25
- end
26
-
27
- describe :translate do
28
- it "translates a word" do
29
- Autolang.translate('hello', 'es').should == 'hola'
30
- end
31
-
32
- it "converts html entities" do
33
- Autolang.translate('sales & tax', 'es').should == 'impuesto sobre ventas y'
34
- end
35
-
36
- it "converts html entities back" do
37
- Autolang.translate('"&&&"', 'es').should == '"Andandand"'
38
- end
39
-
40
- it "translates with strange signs" do
41
- Autolang.translate('production', 'es').should == 'producción'
42
- end
43
-
44
- it "translates with | " do
45
- Autolang.translate('Auto|hello', 'es').should == 'hola'
46
- end
47
-
48
- it "translates with %{}" do
49
- Autolang.translate('hello %{name}', 'es').should == 'hola %{name}'
50
- end
51
- end
52
- end
53
-
54
- describe Autolang::TranslationEscaper do
55
- t = Autolang::TranslationEscaper
56
- describe :escaped do
57
- it "replaces piped subsections" do
58
- e = t.new('Car|be gone')
59
- e.escaped.should == 'be gone'
60
- e.unescape('geh weg').should == 'geh weg'
61
- end
62
-
63
- it "does not replace regular pipes" do
64
- e = t.new('Eat my pipe | fool')
65
- e.escaped.should == 'Eat my pipe | fool'
66
- e.unescape('Iss mein Rohr | idiot').should == 'Iss mein Rohr | idiot'
67
- end
68
-
69
- it "replaces %{something}" do
70
- e = t.new('can i have a %{name}, please.')
71
- e.escaped.should == 'can i have a PH0, please.'
72
- e.unescape('kann ich bitte ein PH0 haben.').should == 'kann ich bitte ein %{name} haben.'
73
- end
74
-
75
- it "replaces at the end of sentences" do
76
- e = t.new('hello %{name}')
77
- e.unescape('hello PH0').should == 'hello %{name}'
78
- end
79
- end
80
- end
81
-
82
- describe 'translate pot file' do
83
- delete = lambda{ `rm -rf spec/fixtures && mkdir spec/fixtures` }
84
-
85
- before &delete
86
- after &delete
87
-
88
- before do
89
- @pot = 'spec/fixtures/xxx.pot'
90
- File.open(@pot, 'w'){|f| f.write(%Q{msgid "hello"\nmsgstr ""}) }
91
- @po = 'spec/fixtures/de/de.po'
92
- end
93
-
94
- it "translates all msgids" do
95
- `./bin/autolang #{@pot} de`
96
- File.read(@po).should include(%Q{msgid "hello"\nmsgstr "hallo"})
97
- end
98
- end