yaml_translate 0.1

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.
@@ -0,0 +1,18 @@
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
+ *.yml
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yaml_translate.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ = Yaml Translate
2
+
3
+ A simple command line tool to translate values of an existing yaml-file into one ore more languages, utilizing Google's translation service
4
+
5
+ == Installation
6
+ $ gem install yaml_translate
7
+
8
+ == Usage
9
+ $ yaml_translate --from en --to fr,ru -f my_yaml.yml
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rdoc/task'
5
+ Rake::RDocTask.new do |rdoc|
6
+ rdoc.rdoc_dir = 'rdoc'
7
+ rdoc.rdoc_files.include('README*')
8
+ rdoc.rdoc_files.include('lib/**/*.rb')
9
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yaml_translate'
4
+ YamlTranslate.run!
@@ -0,0 +1,52 @@
1
+ require 'yaml_translate/version'
2
+ require 'optparse'
3
+
4
+ module YamlTranslate
5
+ autoload :Translator, 'yaml_translate/translator'
6
+
7
+ module_function
8
+
9
+ @@defaults = {
10
+ #:from => 'en',
11
+ #:to => 'es',
12
+ #:verbose => false,
13
+ #:file => nil
14
+ }
15
+
16
+ def parse! arguments=ARGV, defaults=@@defaults
17
+ @@defaults.dup.tap do |options|
18
+ OptionParser.new do |parser|
19
+ parser.banner = 'Usage: yaml_translate [options]'
20
+ parser.version = YamlTranslate::VERSION
21
+
22
+ parser.on('-lf', '--from LANG', 'Set the language to translate from') do |from|
23
+ options[:from] = from
24
+ end
25
+
26
+ parser.on('-lt', '--to LANG1,LANG2,...', Array, 'Set the language(s) to translate to') do |to|
27
+ options[:to] = to
28
+ end
29
+
30
+ parser.on('-f', '--file FILE', 'Specify the yaml-file to translate') do |file|
31
+ options[:file] = file
32
+ end
33
+
34
+ parser.on_tail('-h', '--help', 'Display this help information') do
35
+ puts parser
36
+ exit!
37
+ end
38
+ end.parse!
39
+ end
40
+ end
41
+
42
+ def run! options=nil
43
+ options &&= @@defaults.merge options
44
+ options ||= parse!
45
+
46
+ # TODO: bail out on missing options
47
+
48
+ puts "Starting YamlTranslate\n"
49
+ Translator.new(options[:from], options[:to], options[:file]).translate!
50
+ end
51
+
52
+ end
@@ -0,0 +1,122 @@
1
+ require 'google_translate'
2
+ require 'yaml'
3
+ require 'ya2yaml'
4
+
5
+ module Google
6
+ class Translator
7
+ alias_method :translate_original, :translate
8
+
9
+ # Calling the original method while gracefully return a string on any error despite an Interrupt
10
+ def translate from, to, from_text, options={}
11
+ begin
12
+ translate_original from, to, from_text, options
13
+ rescue Interrupt
14
+ exit 1
15
+ rescue Exception => e
16
+ ["[FAIL] #{from_text}", '']
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ #
23
+ # Class handling the translation
24
+ #
25
+ class YamlTranslate::Translator
26
+ SUBSTITUTION_PLACE_HOLDER = '%---%'
27
+ SUBSTITUTION_PLACE_HOLDER_2 = '-.-.-'
28
+
29
+ #
30
+ # Initializes a new YamlTranslate::Translator instance when provided with
31
+ # * +from+ - the source language (e,g, 'en')
32
+ # * +to+ - an Array containing the destinations languages (e.g. ['de', 'fr'])
33
+ # * +yml+ - the filename of the yaml-file to translate
34
+ #
35
+ def initialize from, to, yml
36
+ @from = from
37
+ @to = to
38
+ @yml = yml
39
+ @translator = Google::Translator.new
40
+ end
41
+
42
+ def translate!
43
+ raise "Yaml file #{@yml} not found" unless File.exist? @yml
44
+
45
+ begin
46
+ @to.each do |lang|
47
+ puts "\nTranslating to '#{lang}'"
48
+ translated_filename = File.join(File.dirname(@yml), "#{lang}.yml")
49
+ source = YAML.load_file @yml
50
+ @keys = source.keys.size
51
+ @running = 0
52
+ translate_keys(source, lang, @from)
53
+ File.open(translated_filename, 'w') { |f| f.write(source.ya2yaml) }
54
+ end
55
+ rescue => e
56
+ puts "\nTranslation process fucked up!!: #{e.backtrace}"
57
+ end
58
+
59
+ puts "\nDone, exiting"
60
+ end
61
+
62
+ def translate_keys(translate_hash, to, from)
63
+ if translate_hash.is_a?(String)
64
+ add_substitutions(translate_hash)
65
+ translate_hash = @translator.translate(from, to, translate_hash)[0]
66
+ remove_substitutions(translate_hash)
67
+ elsif translate_hash.is_a?(Array)
68
+ translate_hash.map! { |x|
69
+ if !x.nil? then
70
+ @translator.translate(from, to, x)[0]
71
+ else
72
+ ""
73
+ end }
74
+ elsif translate_hash.is_a?(Hash)
75
+ translate_hash.each_key do |key|
76
+ next if translate_hash[key].is_a?(Fixnum)
77
+ if translate_hash[key].is_a?(Hash) || translate_hash[key].is_a?(Array)
78
+ translate_keys(translate_hash[key], to, from)
79
+ else
80
+ if key == false
81
+ puts "Key #{key} was evaluated as false. Check your yml file and be sure to escape values like no with 'no'. ie 'no': 'No'"
82
+ elsif key == true
83
+ puts "Key #{key} was evaluated as true. Check your yml file and be sure to escape values like yes with 'yes'. ie 'yes': 'Yes'"
84
+ elsif !translate_hash[key].nil?
85
+ add_substitutions(translate_hash[key])
86
+ @running += 1
87
+ print "#{@running}/#{@keys}\r"
88
+ #print "[#{key}] #{translate_hash[key]} : "
89
+ res = @translator.translate(from, to, translate_hash[key])[0]
90
+ translate_hash[key] = res
91
+ #print " #{res}\n"
92
+ remove_substitutions(translate_hash[key])
93
+ else
94
+ puts "Key #{key} contains no data"
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ def add_substitutions(translate_text)
102
+ # pull out all the string substitutions so that google doesn't translate those
103
+ pattern = /\%.+?\%/ # non greedy pattern match so that we properly match strings like: "{{name}} on {{application_name}}"
104
+ @replacements = translate_text.to_s.scan(pattern)
105
+ translate_text.to_s.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER)
106
+
107
+ # Pull out all the translation blocks so Google doesn't mess with those
108
+ pattern = /%\{.+?\}/
109
+ @replacements2 = translate_text.to_s.scan(pattern)
110
+ translate_text.to_s.gsub!(pattern, SUBSTITUTION_PLACE_HOLDER_2)
111
+ end
112
+
113
+ def remove_substitutions(translate_text)
114
+ return '' if !translate_text
115
+ @replacements.each do |r|
116
+ translate_text.to_s.gsub!(SUBSTITUTION_PLACE_HOLDER, r)
117
+ end
118
+ @replacements2.each do |r|
119
+ translate_text.to_s.gsub!(SUBSTITUTION_PLACE_HOLDER_2, r)
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,3 @@
1
+ module YamlTranslate
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/yaml_translate/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Sebastian Georgi"]
6
+ gem.email = ["sgeorgi@sgeorgi.de"]
7
+ gem.description = %q{Translating YAML-files using Google}
8
+ gem.summary = %q{Translating YAML-files using Google}
9
+ gem.homepage = "http://github.com/sgeorgi/yaml_translate"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "yaml_translate"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = YamlTranslate::VERSION
17
+
18
+ gem.add_dependency 'google-translate'
19
+ gem.add_dependency 'ya2yaml'
20
+
21
+ gem.add_development_dependency 'rake'
22
+ gem.add_development_dependency 'rdoc'
23
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_translate
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sebastian Georgi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: google-translate
16
+ requirement: &9100580 !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: *9100580
25
+ - !ruby/object:Gem::Dependency
26
+ name: ya2yaml
27
+ requirement: &9099700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *9099700
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &9098820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *9098820
47
+ - !ruby/object:Gem::Dependency
48
+ name: rdoc
49
+ requirement: &9096940 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *9096940
58
+ description: Translating YAML-files using Google
59
+ email:
60
+ - sgeorgi@sgeorgi.de
61
+ executables:
62
+ - yaml_translate
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - .gitignore
67
+ - Gemfile
68
+ - README.rdoc
69
+ - Rakefile
70
+ - bin/yaml_translate
71
+ - lib/yaml_translate.rb
72
+ - lib/yaml_translate/translator.rb
73
+ - lib/yaml_translate/version.rb
74
+ - yaml_translate.gemspec
75
+ homepage: http://github.com/sgeorgi/yaml_translate
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.10
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Translating YAML-files using Google
99
+ test_files: []