translate_acts_as_translatable_models 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/tasks/acts_as_translatable_task.rake +71 -0
- metadata +4 -3
@@ -0,0 +1,71 @@
|
|
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
|
+
begin
|
61
|
+
dest = translator.translate(source, :from => @from_locale, :to => @to_locale)
|
62
|
+
rescue
|
63
|
+
dest = ""
|
64
|
+
end
|
65
|
+
|
66
|
+
dest
|
67
|
+
end
|
68
|
+
|
69
|
+
def translator
|
70
|
+
@translator ||= BingTranslator.new @app_id
|
71
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: translate_acts_as_translatable_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lasse Bunk
|
@@ -43,6 +43,7 @@ extra_rdoc_files: []
|
|
43
43
|
|
44
44
|
files:
|
45
45
|
- lib/railtie.rb
|
46
|
+
- lib/tasks/acts_as_translatable_task.rake
|
46
47
|
- lib/translate_acts_as_translatable_models.rb
|
47
48
|
homepage: http://github.com/lassebunk/translate_acts_as_translatable_models
|
48
49
|
licenses: []
|