babel_i18n 0.0.2

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: c77daf5b74409c592f0f16b9263e5c0c96b726d3
4
+ data.tar.gz: a2c067a45f61ffc270332ab06764a7d162686690
5
+ SHA512:
6
+ metadata.gz: 4cb194bebca13da6375b2e2321b9fa398f629e294ddc94a9b8f3765a9b4feb32ceb25c3e99dca1dc5abda50a42e91aedda667a3260a8f9963eb078ce90aa8c08
7
+ data.tar.gz: 0f1f6780407ddc045764a68d1c41c41aa69f36e77d886d76aef68a95c889827b4bb36e21d3bb3c776eb71d062b75f6ee239ba360207feb7551240afd97a1ee97
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.env
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in babel_i18n.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Gabriel Pereira
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # BabelI18n
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/babel_i18n`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'babel_i18n'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install babel_i18n
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/babel_i18n.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'babel_i18n/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "babel_i18n"
8
+ spec.version = BabelI18n::VERSION
9
+ spec.authors = ["Gabriel Pereira"]
10
+ spec.email = ["gabrielgibson@gmail.com"]
11
+
12
+ spec.summary = %q{Translate locale dictionaries to other languages using Google Translate API}
13
+ spec.homepage = "https://github.com/gabrielgibson/babel_i18n"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.10"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "thor"
23
+ spec.add_development_dependency "oj"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "dotenv"
26
+ end
data/bin/babel_i18n ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby -U
2
+
3
+ require "babel_i18n"
4
+
5
+ BabelI18n::Cli.start(ARGV)
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "babel_i18n"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ require 'yaml'
2
+ module BabelI18n
3
+ class Adapter
4
+
5
+ attr_accessor :response, :from, :to, :key
6
+
7
+ def initialize(path, to, key)
8
+ data = YAML::load_file(path)
9
+ @from = BabelI18n::Adapter.get_language_by(data)
10
+ @response = BabelI18n::Adapter.get_response(data)
11
+ @to = to
12
+ @key = key
13
+ end
14
+
15
+ def convert
16
+ new_response = {}
17
+ @response.each do |key, value|
18
+ convert(value) if value.is_a?(Hash)
19
+ new_response[key] = BabelI18n::Base.new(value, @key).from(@from).to(@to).translate
20
+ end
21
+ new_response
22
+ end
23
+
24
+ def converted
25
+ { @to => convert }
26
+ end
27
+
28
+ def write_file
29
+ File.open("#{@to}.yml", "w") { |f| f.write converted.to_yaml }
30
+ end
31
+
32
+ protected
33
+
34
+ def self.get_language_by(data)
35
+ data.keys[0]
36
+ end
37
+
38
+ def self.get_response(data)
39
+ data.delete(get_language_by(data))
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,27 @@
1
+ module BabelI18n
2
+ class Base
3
+
4
+ attr_accessor :translation
5
+
6
+ def initialize(text, key, &block)
7
+ @translation = BabelI18n::Translate.new(text)
8
+ @translation.instance_eval(&block) if block_given?
9
+ @translation.key = key
10
+ end
11
+
12
+ def translate
13
+ @translation.translate
14
+ end
15
+
16
+ def to(to)
17
+ @translation.to(to)
18
+ self
19
+ end
20
+
21
+ def from(from)
22
+ @translation.from(from)
23
+ self
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,26 @@
1
+ require 'thor'
2
+
3
+ module BabelI18n
4
+ class Cli < Thor
5
+
6
+ desc 'translate_text', 'translate text --from some_languague --to other_language --key xpto'
7
+ option :from
8
+ option :to, required: true
9
+ option :key, required: true
10
+ def translate_text(text)
11
+ base = BabelI18n::Base.new(text, options[:key])
12
+ base.from(options[:from]) if options[:from]
13
+ base.to(options[:to])
14
+ puts base.translate
15
+ end
16
+
17
+ desc 'translate_file', 'translate file --to other_language --key xpto'
18
+ option :to, required: true
19
+ option :key, required: true
20
+ def translate_file(file)
21
+ adapter = BabelI18n::Adapter.new(file, options[:to], options[:key])
22
+ adapter.write_file
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,42 @@
1
+ require 'google/api'
2
+ module BabelI18n
3
+
4
+ class Translate
5
+
6
+ include Google::API
7
+
8
+ attr_accessor :text, :key
9
+
10
+ def initialize(text)
11
+ @text = text
12
+ @from, @to = nil, nil
13
+ end
14
+
15
+ def from(from)
16
+ @from = from
17
+ end
18
+
19
+ def to(to)
20
+ @to = to
21
+ end
22
+
23
+ def target
24
+ @to
25
+ end
26
+
27
+ def source
28
+ @from
29
+ end
30
+
31
+ def translate
32
+ google_translate if valid?
33
+ end
34
+
35
+ def valid?
36
+ raise "parameter 'text' is necessary to translate" if @text.nil? || @text.empty?
37
+ raise "parameter 'to' is necessary to translate" if @to.nil? || @to.empty?
38
+ true
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,3 @@
1
+ module BabelI18n
2
+ VERSION = "0.0.2"
3
+ end
data/lib/babel_i18n.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "babel_i18n/version"
2
+ require "babel_i18n/base"
3
+ require "babel_i18n/translate"
4
+ require "babel_i18n/cli"
5
+ require "babel_i18n/adapter"
6
+ require "google/api"
7
+
8
+ module BabelI18n
9
+ end
data/lib/google/api.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+ require 'oj'
4
+ module Google
5
+ module API
6
+
7
+ URL = 'https://www.googleapis.com/language/translate/v2'
8
+
9
+ module ClassMethods
10
+ end
11
+
12
+ module InstanceMethods
13
+
14
+ def google_translate
15
+ uri = get_uri
16
+ http = Net::HTTP.new(uri.host, uri.port)
17
+ http.use_ssl = true
18
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
19
+
20
+ request = Net::HTTP::Get.new(uri.request_uri)
21
+ response = http.request(request)
22
+
23
+ translated_date_text(Oj.load(response.body))
24
+ end
25
+
26
+ def translated_date_text(body)
27
+ body['data']['translations'].first['translatedText']
28
+ rescue
29
+ ''
30
+ end
31
+
32
+ def get_uri
33
+ url = "#{URL}?q=#{text}"
34
+ url += "&target=#{target}" if target
35
+ url += "&source=#{source}" if source
36
+ url += "&key=#{key}"
37
+ URI(url)
38
+ end
39
+
40
+ end
41
+
42
+ def self.included(receiver)
43
+ receiver.extend ClassMethods
44
+ receiver.send :include, InstanceMethods
45
+ end
46
+ end
47
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: babel_i18n
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Pereira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: thor
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: oj
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: dotenv
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - gabrielgibson@gmail.com
100
+ executables:
101
+ - babel_i18n
102
+ - console
103
+ - setup
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - ".travis.yml"
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - babel_i18n.gemspec
115
+ - bin/babel_i18n
116
+ - bin/console
117
+ - bin/setup
118
+ - lib/babel_i18n.rb
119
+ - lib/babel_i18n/adapter.rb
120
+ - lib/babel_i18n/base.rb
121
+ - lib/babel_i18n/cli.rb
122
+ - lib/babel_i18n/translate.rb
123
+ - lib/babel_i18n/version.rb
124
+ - lib/google/api.rb
125
+ homepage: https://github.com/gabrielgibson/babel_i18n
126
+ licenses:
127
+ - MIT
128
+ metadata: {}
129
+ post_install_message:
130
+ rdoc_options: []
131
+ require_paths:
132
+ - lib
133
+ required_ruby_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ requirements: []
144
+ rubyforge_project:
145
+ rubygems_version: 2.4.7
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Translate locale dictionaries to other languages using Google Translate API
149
+ test_files: []