translate_acts_as_translatable_models 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/railtie.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Railtie < Rails::Railtie
2
+ rake_tasks do
3
+ load File.join(File.dirname(__FILE__), "tasks", "acts_as_translatable_task.rake")
4
+ end
5
+ end
@@ -0,0 +1,65 @@
1
+ require 'net/http'
2
+ require 'rexml/document'
3
+ require 'bing_translator'
4
+
5
+ desc "Translate your YAML files using Bing."
6
+ task :translate_acts_as_translatable_models => :environment do
7
+ @class = ENV["class"]
8
+ raise "need to specify class=Category or class=Category,Product" unless @class
9
+
10
+ @from_locale = ENV["from"]
11
+ raise "need to specify from=<locale>" unless @from_locale
12
+
13
+ @to_locale = ENV["to"]
14
+ raise "need to specify to=<locale>" unless @to_locale
15
+
16
+ @force_translation = (ENV["force_translation"] == "true")
17
+
18
+ @app_id = ENV["app_id"]
19
+ raise "need to specify app_id=<Your Bing API key>" unless @app_id
20
+
21
+ puts "Translating..."
22
+
23
+ @class.split(",").each do |c|
24
+ model = c.constantize
25
+ model.enable_locale_fallbacks = false
26
+
27
+ model.all.each do |record|
28
+ model_translated = false
29
+ model.translatable_fields.each do |field|
30
+ source = record.send("#{field}_#{@from_locale}")
31
+ dest = record.send("#{field}_#{@to_locale}")
32
+
33
+ # only translate if not already translated
34
+ if !source.blank? && (@force_translation || dest.blank?)
35
+ dest = translate_string(source)
36
+ puts "#{source} => #{dest}"
37
+
38
+ unless dest.blank?
39
+ record.send "#{field}_#{@to_locale}=", dest
40
+ model_translated = true
41
+ end
42
+ end
43
+ end
44
+
45
+ # only save model if it has been translated
46
+ if model_translated
47
+ puts "Saving #{model}..."
48
+ record.save
49
+ end
50
+ end
51
+ end
52
+
53
+ puts "Done!"
54
+ end
55
+
56
+ def translate_string(source)
57
+ return "" unless source
58
+
59
+ # translate using Microsoft Bing
60
+ translator.translate(source, :from => @from_locale, :to => @to_locale)
61
+ end
62
+
63
+ def translator
64
+ @translator ||= BingTranslator.new @app_id
65
+ end
@@ -0,0 +1 @@
1
+ require 'railtie'
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: translate_acts_as_translatable_models
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Lasse Bunk
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-05-18 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: acts_as_translatable
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: bing_translator
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ~>
41
+ - !ruby/object:Gem::Version
42
+ hash: 27
43
+ segments:
44
+ - 0
45
+ - 0
46
+ - 2
47
+ version: 0.0.2
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ description: Ruby on Rails plugin to translate your acts_as_translatable models using Bing.
51
+ email: lassebunk@gmail.com
52
+ executables: []
53
+
54
+ extensions: []
55
+
56
+ extra_rdoc_files: []
57
+
58
+ files:
59
+ - lib/railtie.rb
60
+ - lib/tasks/acts_as_translatable_task.rake
61
+ - lib/translate_acts_as_translatable_models.rb
62
+ homepage: http://github.com/lassebunk/translate_acts_as_translatable_models
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ hash: 3
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.8.21
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Ruby on Rails plugin to translate your acts_as_translatable models using Bing.
95
+ test_files: []
96
+