bing_translate_yaml 0.0.20 → 0.0.21

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.
Files changed (2) hide show
  1. metadata +5 -5
  2. data/tasks/translate_task.rake +0 -90
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bing_translate_yaml
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 53
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 20
10
- version: 0.0.20
9
+ - 21
10
+ version: 0.0.21
11
11
  platform: ruby
12
12
  authors:
13
13
  - Lasse Bunk
@@ -26,8 +26,8 @@ extensions: []
26
26
 
27
27
  extra_rdoc_files: []
28
28
 
29
- files:
30
- - tasks/translate_task.rake
29
+ files: []
30
+
31
31
  homepage: http://github.com/lassebunk/bing_translate_yaml
32
32
  licenses: []
33
33
 
@@ -1,90 +0,0 @@
1
- require 'net/http'
2
- require 'rexml/document'
3
-
4
- desc "Translate your YAML files using Bing."
5
- task :translate => :environment do
6
- @source_locale = ENV["source"]
7
- @dest_locale = ENV["destination"]
8
- @app_id = ENV["app_id"] || "TWwfmo09qPdsxVfnXUMCRHAyk2dGOaodKYVOvbyu3m5Y*"
9
-
10
- puts "Translating..."
11
-
12
- source_locale = ENV["source"]
13
- dest_locale = ENV["destination"]
14
-
15
- source_path = "#{Rails.root}/config/locales/#{@source_locale}.yml"
16
- dest_path = "#{Rails.root}/config/locales/#{@dest_locale}.yml"
17
-
18
- if File.exists?(source_path)
19
- source_yaml = YAML::load(File.open(source_path))
20
- source = source_yaml ? source_yaml[source_locale] || {} : {}
21
- else
22
- source = {}
23
- end
24
-
25
- if File.exists?(dest_path)
26
- dest_yaml = YAML::load(File.open(dest_path))
27
- dest = dest_yaml ? dest_yaml[dest_locale] || {} : {}
28
- else
29
- dest = {}
30
- end
31
-
32
- source = source_yaml ? source_yaml[source_locale] || {} : {}
33
- dest = dest_yaml ? dest_yaml[dest_locale] || {} : {}
34
-
35
- translated = translate_hash(source)
36
-
37
- out = { dest_locale => translated.deep_merge(dest) }
38
-
39
- File.open(dest_path, 'w') {|f| YAML.dump(out, f) }
40
-
41
- puts "Done!"
42
- end
43
-
44
- def translate_hash(yaml)
45
- dest = yaml.dup
46
-
47
- yaml.keys.each do |key|
48
- source = yaml[key]
49
-
50
- if source.is_a?(String)
51
- translated = translate_string(source)
52
- elsif source.is_a?(Hash)
53
- translated = translate_hash(source)
54
- elsif source.is_a?(Array)
55
- translated = translate_array(source)
56
- else
57
- translated = ""
58
- end
59
-
60
- dest[key] = translated
61
- end
62
-
63
- dest
64
- end
65
-
66
- def translate_array(array)
67
- out = []
68
- array.each do |source|
69
- if source.is_a?(Symbol)
70
- out << source
71
- else
72
- out << translate_string(source)
73
- end
74
- end
75
- out
76
- end
77
-
78
- def translate_string(source)
79
- return "" unless source
80
-
81
- url = "http://api.microsofttranslator.com/v2/Http.svc/Translate?appId=#{@app_id}&text=#{CGI::escape(source)}&from=#{@source_locale}&to=#{@dest_locale}"
82
- xml = Net::HTTP.get_response(URI.parse(url)).body
83
-
84
- doc = REXML::Document.new(xml)
85
- dest = doc.elements["string"].text.html_safe.gsub("% {", "%{")
86
-
87
- puts "#{source} => #{dest}"
88
-
89
- dest
90
- end