yml_gtranslate 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ *.yml
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ .project
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p362@gtranslate"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.17.8 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in yml_gtranslate.gemspec
4
+ gemspec
5
+
6
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Stefan Mikula
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # yml_gtranslate
2
+
3
+ yml_gtranslate is a convenience gem for starting your rails localization quickly. It uses the Google Translate service (no API required though).
4
+ It creates missing `*.yml` localization config files or updates the locale file with missing keys and translates them.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'yml_gtranslate', :git => 'git://github.com/zenchief/yml_gtranslate.git'
12
+
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install yml_gtranslate
21
+
22
+ ## Requirements
23
+
24
+ You need [sed](www.gnu.org/software/sed) on your system and in your $PATH. It's used for handling comment tokens before and after transaltion.
25
+
26
+ ## Usage
27
+
28
+ After installation you can translate your files easily just by a command:
29
+
30
+
31
+ $ yml_gt <from_lang> <to_lang> [directory]
32
+ goes thru all `*from_lang.yml` files in the `direcotry`
33
+ the default dir is config/locales
34
+
35
+
36
+ Translates config/locales/*en.yml files to German
37
+
38
+ $ yml_gt en de
39
+
40
+
41
+ Translates all sk.yml files in the _current directory_ to English (hence the dot)
42
+
43
+ $ yml_gt sk en .
44
+
45
+
46
+
47
+ This is going to take your all your `config/locales/*en.yml` files and compare them with `config/locales/*de.yml` files.
48
+ (That is in case your locale files are divided, e.g. en.yml, devise.en.yml etc.). If the target file does not exist
49
+ it's going to create it and translate all string keys in the source file to German using Google Translate.
50
+ If the target file already exists it's gonna compare all the string keys in both source and target and translate only those missing in the target.
51
+
52
+ The translation adds a comment "#i18n-GT" after the translated key. This is to let you know, that this string was
53
+ generated by Google Translate and probably needs some fine tuning.
54
+
55
+
56
+
57
+ ## Contributing
58
+
59
+ A. Email me and we'll talk it over
60
+
61
+ **or**
62
+
63
+ B:
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
70
+
71
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/bin/yml_gt ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'yml_gtranslate'
4
+
5
+ if ARGV.size != 3 and ARGV.size != 2
6
+ puts "Usage: #{$0} <from_lang> <to_lang> [directory]"
7
+ puts " goes thru all *from_lang.yml files in the direcotry"
8
+ puts " the default dir is config/locales"
9
+ puts " eg: #{$0} en de . -> translates all *en.yml to *de.yml files in the current directory"
10
+ exit -1
11
+ end
12
+
13
+ from_lang = ARGV[0]
14
+ to_lang = ARGV[1]
15
+ directory = ARGV[2] || "config/locales"
16
+
17
+ #puts directory
18
+ #exit
19
+
20
+ gt = YmlGtranslate::Translator.new(from_lang, to_lang, directory)
21
+ gt.translate_locales
data/de.yml ADDED
@@ -0,0 +1,21 @@
1
+ ---
2
+ de:
3
+ hello: "hallo Welt" #i18n-GT
4
+ helpers:
5
+ label:
6
+ user:
7
+ email: "Email" #i18n-GT
8
+ password: "Kennwort" #i18n-GT
9
+ remember_me: "Eingeloggt bleiben" #i18n-GT
10
+ lang:
11
+ english: "Englisch" #i18n-GT
12
+ language: "Sprache" #i18n-GT
13
+ slovak: "Slovak" #i18n-GT
14
+ navbar:
15
+ home: "Zuhause" #i18n-GT
16
+ profile: "Profil" #i18n-GT
17
+ reservations: "Reservierungen" #i18n-GT
18
+ settings: "Einstellungen" #i18n-GT
19
+ sign_in: "Anmeldung" #i18n-GT
20
+ sign_out: "austragen" #i18n-GT
21
+ trips: "Reisen" #i18n-GT
data/en.yml ADDED
@@ -0,0 +1,23 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ hello: "Hello world"
6
+ lang:
7
+ language: "Language"
8
+ slovak: "Slovak"
9
+ english: "English"
10
+ navbar:
11
+ home: "Home"
12
+ trips: "Trips"
13
+ reservations: "Reservations"
14
+ sign_in: "Sign in"
15
+ sign_out: "Sign out"
16
+ profile: "Profile"
17
+ settings: "Settings"
18
+ helpers:
19
+ label:
20
+ user:
21
+ email: "Email"
22
+ password: "Password"
23
+ remember_me: "Stay signed in"
@@ -0,0 +1,75 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'ya2yaml'
4
+ require 'yaml'
5
+
6
+
7
+ module YmlGtranslate
8
+ class Translator
9
+ def initialize(from_lang, to_lang, dir)
10
+ @from_lang = from_lang
11
+ @to_lang = to_lang
12
+ @directory = dir || ""
13
+ end
14
+
15
+ COMMENT_TOKEN = "#i18n-GT"
16
+
17
+ def translate(string)
18
+ command = <<-EOF
19
+ curl -s -A "Mozilla/5.0 (X11; Linux x86_64; rv:5.0) Gecko/20100101 Firefox/5.0" "http://translate.google.com/translate_a/t?client=t&text=#{URI.escape(string)}&hl=#{@from_lang}&sl=auto&tl=#{@to_lang}&multires=1&prev=conf&psl=auto&ptl=#{@from_lang}&otf=1&it=sel.7123%2Ctgtd.3099&ssel=0&tsel=4&uptl=#{@to_lang}&sc=1"
20
+ EOF
21
+ command.strip!
22
+ res = `#{command}`.gsub!(/,+/, ",")
23
+ JSON.parse(res).first.first.first.strip
24
+ end
25
+
26
+ def comment(string)
27
+ string + COMMENT_TOKEN
28
+ end
29
+
30
+
31
+ def compare(hash1, hash2)
32
+ hash1.inject({}) do |h, pair|
33
+ key, value = pair
34
+ if hash2.key?(key)
35
+ h[key] = value.is_a?(Hash) ? compare(value, hash2[key]) : hash2[key]
36
+ else
37
+ h[key] = value.is_a?(Hash) ? compare(value, {}) : value.is_a?(String) ? comment(translate(value)) : value
38
+ end
39
+ h
40
+ end
41
+ end
42
+
43
+
44
+
45
+
46
+ def translate_locales
47
+ regexp = "*#{@from_lang}.yml"
48
+
49
+ Dir.glob(File.join(@directory, regexp)).each do |f|
50
+ prefix = f.split("#{@from_lang}.yml").first.to_s
51
+ to_file = "#{prefix}#{@to_lang}.yml"
52
+ from_hash = YAML.load_file(f)[@from_lang.to_s]
53
+
54
+ to_hash = {}
55
+ if File.exists?(to_file)
56
+ incomment = "sed s/\\\"\\ #i18n-GT/#i18n-GT\\\"/g #{to_file} > tmp.yml; rm #{to_file}; mv tmp.yml #{to_file}"
57
+ `#{incomment}`
58
+ to_hash = YAML.load_file(to_file)[@to_lang.to_s]
59
+ end
60
+
61
+ result = compare(from_hash, to_hash)
62
+
63
+ File.open("#{to_file}", 'w') do |out|
64
+ out.write({@to_lang.to_s => result}.ya2yaml)
65
+ end
66
+
67
+ comment = "sed s/#i18n-GT\\\"/\\\"\\ #i18n-GT/g #{to_file} > tmp.yml; rm #{to_file}; mv tmp.yml #{to_file}"
68
+
69
+ `#{comment}`
70
+ end
71
+ end
72
+
73
+
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ module YmlGtranslate
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,6 @@
1
+ require "yml_gtranslate/version"
2
+ require "yml_gtranslate/translator"
3
+
4
+
5
+ module YmlGtranslate
6
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe YmlGtranslate do
4
+
5
+ before { @gt = YmlGtranslate::Translator.new("en", "de", "") }
6
+
7
+ it "asserts comment token properly suffixed" do
8
+
9
+ @gt.comment("stuff").should eq("stuff" + YmlGtranslate::Translator::COMMENT_TOKEN)
10
+ end
11
+
12
+ it "checks german translation" do
13
+ @gt.translate("Good morning").should eq("guten Morgen")
14
+ end
15
+
16
+ it "checks comparing empty hashes" do
17
+ @gt.compare({},{}).should eq({})
18
+ end
19
+
20
+
21
+ end
@@ -0,0 +1,2 @@
1
+ require 'yml_gtranslate'
2
+
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'yml_gtranslate/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "yml_gtranslate"
8
+ gem.version = YmlGtranslate::VERSION
9
+ gem.authors = ["Stefan Mikula"]
10
+ gem.email = ["stef.mikula@gmail.com"]
11
+ gem.description = %q{Uses Google translate service to translate your *.yml files in your rails projects. Handy to get your localizations started fast.}
12
+ gem.summary = %q{Translates your *.yml locale files using Google Translate (not thru API).}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency 'rake'
21
+ gem.add_development_dependency 'rspec'
22
+
23
+ gem.add_development_dependency 'ya2yaml'
24
+
25
+
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yml_gtranslate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stefan Mikula
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
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: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
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: ya2yaml
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
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: Uses Google translate service to translate your *.yml files in your rails
63
+ projects. Handy to get your localizations started fast.
64
+ email:
65
+ - stef.mikula@gmail.com
66
+ executables:
67
+ - yml_gt
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - .rvmrc
73
+ - Gemfile
74
+ - LICENSE.txt
75
+ - README.md
76
+ - Rakefile
77
+ - bin/yml_gt
78
+ - de.yml
79
+ - en.yml
80
+ - lib/yml_gtranslate.rb
81
+ - lib/yml_gtranslate/translator.rb
82
+ - lib/yml_gtranslate/version.rb
83
+ - spec/lib/yml_gtranslate_spec.rb
84
+ - spec/spec_helper.rb
85
+ - yml_gtranslate.gemspec
86
+ homepage: ''
87
+ licenses: []
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ! '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.24
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Translates your *.yml locale files using Google Translate (not thru API).
110
+ test_files:
111
+ - spec/lib/yml_gtranslate_spec.rb
112
+ - spec/spec_helper.rb