translate_yaml 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ *.yml
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/README.md ADDED
@@ -0,0 +1,77 @@
1
+ ## YML Locale Generator
2
+
3
+ This utility takes a yml locale file and creates a new one for the target region specified.
4
+
5
+ For example, if you've got an English `config/locales/en.yml` strings file for you project that you'd like to have a French version of, you'd run:
6
+
7
+ translate config/locales/en.yml fr
8
+
9
+ And it would output to whatever folder the command was initiated from.
10
+
11
+ The localization data is generated using the REST API from http://mymemory.translated.net/.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'translate_yaml'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install translate_yaml
26
+
27
+ ### Usage
28
+
29
+ translate [options] source target
30
+
31
+ Options:
32
+ --log-level LEVEL Set the logging level
33
+ (debug|info|warn|error|fatal)
34
+ (Default: info)
35
+
36
+ Arguments:
37
+
38
+ source
39
+ The source filename that will be localized
40
+ ex: en.yml, config/locales/fr.yml
41
+ target
42
+ The ISO standard name for the target
43
+ ex: fr, en, zh-CN
44
+
45
+
46
+ ### Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ ### About
55
+
56
+ Copyright (c) 2012 Alex Bevilacqua
57
+
58
+ MIT License
59
+
60
+ Permission is hereby granted, free of charge, to any person obtaining
61
+ a copy of this software and associated documentation files (the
62
+ "Software"), to deal in the Software without restriction, including
63
+ without limitation the rights to use, copy, modify, merge, publish,
64
+ distribute, sublicense, and/or sell copies of the Software, and to
65
+ permit persons to whom the Software is furnished to do so, subject to
66
+ the following conditions:
67
+
68
+ The above copyright notice and this permission notice shall be
69
+ included in all copies or substantial portions of the Software.
70
+
71
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
72
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
73
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
74
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
75
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
76
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
77
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/bin/translate ADDED
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $KCODE = 'UTF8' unless RUBY_VERSION >= '1.9'
4
+
5
+ require 'methadone'
6
+
7
+ include Methadone::Main
8
+ include Methadone::CLILogging
9
+
10
+ require 'net/http'
11
+ require 'uri'
12
+ require 'json'
13
+
14
+ require 'ya2yaml'
15
+
16
+ DOMAIN = "mymemory.translated.net"
17
+ PATH = "/api/get?langpair=%s&q=%s"
18
+
19
+ def translate_line(langs, line)
20
+ # skip comment lines
21
+ stripped = line.strip
22
+ return line if stripped.length == 0 || stripped[0] == "#"
23
+
24
+ parts = line.split(":")
25
+ key = parts[0]
26
+ value = parts[1..-1].join(':').strip
27
+ result = ""
28
+
29
+ if value.length > 0
30
+ req = Net::HTTP.get(DOMAIN, URI.escape(PATH % [langs.join('|'), value]))
31
+ res = JSON.parse(req)
32
+
33
+ result = if res["responseStatus"] != 200
34
+ res["responseDetails"]
35
+ else
36
+ res["responseData"]["translatedText"]
37
+ end
38
+ end
39
+
40
+ debug "#{key}: #{value}"
41
+
42
+ # ensure that we replace the language key with the target
43
+ # language key
44
+ #
45
+ # en: => fr: (or whatever was provided)
46
+ key = langs[1] if key == langs[0]
47
+
48
+ debug "#{key}: #{result}"
49
+
50
+ "#{key}: #{result}"
51
+ end
52
+
53
+ main do |source, target|
54
+ begin
55
+ src = File.open(source, "r")
56
+
57
+ src_dir, src_file = File.dirname(source), File.basename(source)
58
+
59
+ dst = File.open("#{[src_dir, target].join("/")}.yml", "w")
60
+ rescue Exception => ex
61
+ error ex.message
62
+ end
63
+
64
+ # format the language pair
65
+ langs = [src_file.chomp(File.extname(src_file)), target]
66
+
67
+ src.each_line do |line|
68
+ dst.puts translate_line(langs, line)
69
+ end
70
+ end
71
+
72
+ description "Localization Generator from YAML"
73
+
74
+ arg :source, "The source filename that will be localized\n\tex: en.yml, config/locales/fr.yml"
75
+ arg :target, "The ISO standard name for the target\n\tex: fr, en, zh-CN"
76
+
77
+ use_log_level_option
78
+
79
+ go!
@@ -0,0 +1,5 @@
1
+ require "translate_yaml/version"
2
+
3
+ module TranslateYaml
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module TranslateYaml
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/translate_yaml/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Alex Bevilacqua"]
6
+ gem.email = ["alexbevi@gmail.com"]
7
+ gem.description = ""
8
+ gem.summary = "Utility to generate yml locale files"
9
+ gem.homepage = "https://github.com/alexbevi/translate_yaml"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "translate_yaml"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = TranslateYaml::VERSION
17
+
18
+ gem.add_dependency 'methadone'
19
+ gem.add_dependency 'ya2yaml'
20
+ gem.add_dependency 'json'
21
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: translate_yaml
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Bevilacqua
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: methadone
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: ya2yaml
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ''
63
+ email:
64
+ - alexbevi@gmail.com
65
+ executables:
66
+ - translate
67
+ extensions: []
68
+ extra_rdoc_files: []
69
+ files:
70
+ - .gitignore
71
+ - Gemfile
72
+ - README.md
73
+ - Rakefile
74
+ - bin/translate
75
+ - lib/translate_yaml.rb
76
+ - lib/translate_yaml/version.rb
77
+ - translate_yaml.gemspec
78
+ homepage: https://github.com/alexbevi/translate_yaml
79
+ licenses: []
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 1.8.24
99
+ signing_key:
100
+ specification_version: 3
101
+ summary: Utility to generate yml locale files
102
+ test_files: []
103
+ has_rdoc: