babel_i18n 0.0.3 → 0.0.4

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: 3174ae33ffc4a431c6163a7138938bc70d0b15b7
4
- data.tar.gz: 358276607c09f1185af39c85405d3e6f2be42ca4
3
+ metadata.gz: 2eedc791d3a24620f07894313fd46690ff796e5a
4
+ data.tar.gz: c269346b7a89bdb2023b7f6fae69a937957a4cc2
5
5
  SHA512:
6
- metadata.gz: 6167562a8fb9c89e14018c795a8c0b1a180c57665a06c8a451e5b551848e17a18f2218243b204e2dba6ebf7d98691a8dee69b181bd403c93a9da8d25a6211f21
7
- data.tar.gz: efc2933b3ef032383e23000391870f1d08cedbc8026579a2b1b3f01a9f9a32c0fe8037b5f21e0dd815752b7bae6ca7137a1a5db7c5dbc7155a04c1773498dca6
6
+ metadata.gz: 73fbe20876e9d431704628974a0b567846a7b35ba09c505558b4c99fd07b2c9964d1cbfa011cc81e7b18198e969b9eaca182be6e844abb485a84d6d6ba6f0258
7
+ data.tar.gz: 944e938e598c7b116821bc868a0c99f16bafaa1f9d6ebfeec6213d1818d78c34dd17a4b86d5cbc0377071417eb86aa489f9d7636e614a651949f483425e7fa4a
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # BabelI18n
2
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
3
+ This Ruby library allows to translate files locale dictionaries and provides methods to translate words and phrases using the Google Translate API
6
4
 
7
5
  ## Installation
8
6
 
@@ -20,9 +18,54 @@ Or install it yourself as:
20
18
 
21
19
  $ gem install babel_i18n
22
20
 
21
+ ## Google API Key
22
+
23
+ This GEM requires a valid Google API key for all requests
24
+
25
+ To find your application's API key, do the following:
26
+
27
+ 1. Go to the [Google Developers Console](https://console.developers.google.com/).
28
+ 2. Select a project, or create a new one.
29
+ 3. In the sidebar on the left, expand **APIs & auth**. Next, click **APIs**. Select the **Enabled APIs** link in the API section to see a list of all your enabled APIs. Make sure that the Google Translate API is on the list of enabled APIs. If you have not enabled it, select the API from the list of APIs, then select the **Enable API** button for the API.
30
+ 4. In the sidebar on the left, select **Credentials**.
31
+
23
32
  ## Usage
24
33
 
25
- TODO: Write usage instructions here
34
+ ### Translate file locale dictionary
35
+
36
+ #### Command line
37
+
38
+ $ babel_i18n translate_file en.yml --to pt-BR --key YOUR_GOOGLE_API_KEY
39
+
40
+ #### Or set GOOGLE_API_KEY Environment Variable
41
+
42
+ $ export GOOGLE_API_KEY=YOUR_GOOGLE_API_KEY
43
+ $ babel_i18n translate_file en.yml --to pt-BR
44
+
45
+ ### Translate words and phrases
46
+
47
+ #### Command line
48
+
49
+ $ babel_i18n translate_text Hi --from en --to pt-BR --key YOUR_GOOGLE_API_KEY
50
+
51
+ #### Or set GOOGLE_API_KEY Environment Variable
52
+
53
+ $ export GOOGLE_API_KEY=YOUR_GOOGLE_API_KEY
54
+ $ babel_i18n translate_text Hi --to pt-BR
55
+
56
+ #### In your ruby application
57
+
58
+ ```ruby
59
+ BabelI18n::Base.new('Hi', ENV['GOOGLE_API_KEY']).from('en').to('pt-BR').translate
60
+ => "Oi"
61
+ ```
62
+
63
+ #### Detecting language, omitting parameter 'from'
64
+
65
+ ```ruby
66
+ BabelI18n::Base.new('Hi', ENV['GOOGLE_API_KEY']).to('pt-BR').translate
67
+ => "Oi"
68
+ ```
26
69
 
27
70
  ## Development
28
71
 
@@ -32,7 +75,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
32
75
 
33
76
  ## Contributing
34
77
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/babel_i18n.
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabrielpedepera/babel_i18n.
36
79
 
37
80
 
38
81
  ## License
@@ -6,20 +6,34 @@ module BabelI18n
6
6
  desc 'translate_text', 'translate text --from some_languague --to other_language --key xpto'
7
7
  option :from
8
8
  option :to, required: true
9
- option :key, required: true
9
+ option :key
10
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
11
+ begin
12
+ key = options[:key] || ENV['GOOGLE_API_KEY']
13
+ raise KeyRequiredError if key.nil?
14
+ base = BabelI18n::Base.new(text, key)
15
+ base.from(options[:from]) if options[:from]
16
+ base.to(options[:to])
17
+ puts base.translate
18
+ rescue ArgumentError => e
19
+ puts e.message
20
+ end
15
21
  end
16
22
 
17
23
  desc 'translate_file', 'translate file --to other_language --key xpto'
18
24
  option :to, required: true
19
- option :key, required: true
25
+ option :key
20
26
  def translate_file(file)
21
- adapter = BabelI18n::Adapter.new(file, options[:to], options[:key])
22
- adapter.write_file
27
+ begin
28
+ key = options[:key] || ENV['GOOGLE_API_KEY']
29
+ raise KeyRequiredError if key.nil?
30
+ raise FileParserError if !File.exist?(file)
31
+ adapter = BabelI18n::Adapter.new(file, options[:to], key)
32
+ adapter.write_file
33
+ puts "File '#{adapter.from}.yml' was translated to '#{adapter.file_name}'"
34
+ rescue ArgumentError => e
35
+ puts e.message
36
+ end
23
37
  end
24
38
 
25
39
  end
@@ -0,0 +1,6 @@
1
+ def new_exception(klass, message)
2
+ eval "class #{klass}; def initialize; super('#{message}'); end; end"
3
+ end
4
+
5
+ new_exception('FileParserError < ArgumentError', 'File not found !')
6
+ new_exception('KeyRequiredError < ArgumentError', 'This method requires a valid Google API key.')
@@ -29,13 +29,11 @@ module BabelI18n
29
29
  end
30
30
 
31
31
  def translate
32
- google_translate if valid?
32
+ valid? ? google_translate : 'Error when translating'
33
33
  end
34
34
 
35
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
36
+ (!@text.nil? && !@text.empty?) && !@to.nil?
39
37
  end
40
38
 
41
39
  end
@@ -1,3 +1,3 @@
1
1
  module BabelI18n
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/babel_i18n.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "babel_i18n/version"
2
+ require "babel_i18n/exceptions"
2
3
  require "babel_i18n/base"
3
4
  require "babel_i18n/translate"
4
5
  require "babel_i18n/cli"
data/pt-BR.yml ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ pt-BR:
3
+ hello: ''
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: babel_i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Pereira
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-26 00:00:00.000000000 Z
11
+ date: 2015-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -119,9 +119,11 @@ files:
119
119
  - lib/babel_i18n/adapter.rb
120
120
  - lib/babel_i18n/base.rb
121
121
  - lib/babel_i18n/cli.rb
122
+ - lib/babel_i18n/exceptions.rb
122
123
  - lib/babel_i18n/translate.rb
123
124
  - lib/babel_i18n/version.rb
124
125
  - lib/google/api.rb
126
+ - pt-BR.yml
125
127
  homepage: https://github.com/gabrielgibson/babel_i18n
126
128
  licenses:
127
129
  - MIT