babel_i18n 0.0.3 → 0.0.4
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.
- checksums.yaml +4 -4
- data/README.md +48 -5
- data/lib/babel_i18n/cli.rb +22 -8
- data/lib/babel_i18n/exceptions.rb +6 -0
- data/lib/babel_i18n/translate.rb +2 -4
- data/lib/babel_i18n/version.rb +1 -1
- data/lib/babel_i18n.rb +1 -0
- data/pt-BR.yml +3 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2eedc791d3a24620f07894313fd46690ff796e5a
|
4
|
+
data.tar.gz: c269346b7a89bdb2023b7f6fae69a937957a4cc2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 73fbe20876e9d431704628974a0b567846a7b35ba09c505558b4c99fd07b2c9964d1cbfa011cc81e7b18198e969b9eaca182be6e844abb485a84d6d6ba6f0258
|
7
|
+
data.tar.gz: 944e938e598c7b116821bc868a0c99f16bafaa1f9d6ebfeec6213d1818d78c34dd17a4b86d5cbc0377071417eb86aa489f9d7636e614a651949f483425e7fa4a
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# BabelI18n
|
2
2
|
|
3
|
-
|
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
|
-
|
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/
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/gabrielpedepera/babel_i18n.
|
36
79
|
|
37
80
|
|
38
81
|
## License
|
data/lib/babel_i18n/cli.rb
CHANGED
@@ -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
|
9
|
+
option :key
|
10
10
|
def translate_text(text)
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
25
|
+
option :key
|
20
26
|
def translate_file(file)
|
21
|
-
|
22
|
-
|
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.')
|
data/lib/babel_i18n/translate.rb
CHANGED
@@ -29,13 +29,11 @@ module BabelI18n
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def translate
|
32
|
-
google_translate
|
32
|
+
valid? ? google_translate : 'Error when translating'
|
33
33
|
end
|
34
34
|
|
35
35
|
def valid?
|
36
|
-
|
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
|
data/lib/babel_i18n/version.rb
CHANGED
data/lib/babel_i18n.rb
CHANGED
data/pt-BR.yml
ADDED
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.
|
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-
|
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
|