babelphish 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +2 -0
- data/lib/babelphish.rb +1 -1
- data/lib/babelphish/html_translator.rb +75 -0
- data/lib/babelphish/yml_translator.rb +123 -0
- metadata +3 -1
data/Manifest.txt
CHANGED
data/lib/babelphish.rb
CHANGED
@@ -0,0 +1,75 @@
|
|
1
|
+
module Babelphish
|
2
|
+
module HtmlTranslator
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
# Translates all files in the given path from the language
|
7
|
+
# specififed by 'translate_from' into the languages in 'translate_tos'.
|
8
|
+
# Translations that already exist will not be overwritten unless overwrite = true
|
9
|
+
def translate(path, translate_tos, translate_from = 'en', overwrite = false)
|
10
|
+
@path = path
|
11
|
+
if !Babelphish::GoogleTranslate::LANGUAGES.include?(translate_from)
|
12
|
+
STDERR.puts "#{translate_from} is not one of the available languages."
|
13
|
+
return
|
14
|
+
end
|
15
|
+
if !File.exists?(path)
|
16
|
+
STDERR.puts "#{path} does not exist."
|
17
|
+
return
|
18
|
+
end
|
19
|
+
translate_and_write_pages(path, translate_tos, translate_from, overwrite)
|
20
|
+
end
|
21
|
+
|
22
|
+
def translate_and_write_pages(path, tos, from, overwrite)
|
23
|
+
Dir.glob("#{path}/*").each do |next_file|
|
24
|
+
if File.directory?(next_file)
|
25
|
+
translate_and_write_pages(next_file, language)
|
26
|
+
else
|
27
|
+
translate_and_write_page(next_file, tos, from, overwrite)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Translate a single page from the language specified in 'from'
|
33
|
+
# into the languages specified by 'tos'
|
34
|
+
def translate_and_write_page(source_page, tos, from, overwrite)
|
35
|
+
return unless File.exist?(source_page)
|
36
|
+
return unless translate_file?(source_page)
|
37
|
+
text = IO.read(source_page)
|
38
|
+
|
39
|
+
# Pull out all the code blocks to Google doesn't mess with those
|
40
|
+
pattern = /\<\%.+\%\>/
|
41
|
+
holder = '{{---}}'
|
42
|
+
replacements = text.scan(pattern)
|
43
|
+
text.gsub!(pattern, holder)
|
44
|
+
|
45
|
+
# Send to Google for translations
|
46
|
+
translations = Babelphish::Translator.multiple_translate(text, tos, from)
|
47
|
+
|
48
|
+
# Put the code back
|
49
|
+
translations.each_key do |locale|
|
50
|
+
replacements.each do |r|
|
51
|
+
translations[locale].sub!(holder, r)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Write the new file
|
56
|
+
translations.each_key do |locale|
|
57
|
+
translated_filename = get_translated_file(source_page, locale)
|
58
|
+
if (locale != from) && (overwrite || !File.exists?(translated_filename))
|
59
|
+
File.open(translated_filename, 'w') { |f| f.write(translations[locale]) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
def get_translated_file(page, to)
|
66
|
+
page.gsub('.html', ".#{to}.html")
|
67
|
+
end
|
68
|
+
|
69
|
+
def translate_file?(page)
|
70
|
+
page.split('.').length == 3
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,123 @@
|
|
1
|
+
module Babelphish
|
2
|
+
module YmlTranslator
|
3
|
+
|
4
|
+
class << self
|
5
|
+
|
6
|
+
def translate(yml, overwrite = false, translate_to = nil)
|
7
|
+
@yml = yml
|
8
|
+
language = File.basename(yml, ".yml")
|
9
|
+
if !Babelphish::GoogleTranslate::LANGUAGES.include?(language)
|
10
|
+
STDERR.puts "#{language} is not one of the available languages. Please choose a standard localized yml file. i.e. en.yml."
|
11
|
+
return
|
12
|
+
end
|
13
|
+
if translate_to
|
14
|
+
puts "Translating #{language} to #{translate_to}"
|
15
|
+
translate_and_write_yml(yml, translate_to, language, overwrite)
|
16
|
+
puts "Finished translating #{language} to #{translate_to}"
|
17
|
+
else
|
18
|
+
tos = Babelphish::GoogleTranslate::LANGUAGES
|
19
|
+
translate_and_write_many_yml(yml, tos, language, overwrite)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def translate_and_write_yml(yml, to, from, overwrite)
|
24
|
+
return if to == from
|
25
|
+
return unless File.exist?(yml)
|
26
|
+
translated_filename = File.join(File.dirname(yml), "#{to}.yml")
|
27
|
+
return if File.exist?(translated_filename) && !overwrite
|
28
|
+
source = YAML.load_file(yml)
|
29
|
+
translate_keys(source, to, from)
|
30
|
+
# change the top level key from the source language to the destination language
|
31
|
+
source[to] = source[from]
|
32
|
+
source.delete(from)
|
33
|
+
File.open(translated_filename, 'w') { |f| f.write(source.ya2yaml) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def translate_keys(translate_hash, to, from)
|
37
|
+
translate_hash.each_key do |key|
|
38
|
+
if translate_hash[key].is_a?(Hash)
|
39
|
+
translate_keys(translate_hash[key], to, from)
|
40
|
+
else
|
41
|
+
if key == false
|
42
|
+
puts "Key #{key} was evaluated as false. Check your yml file and be sure to escape values like no with 'no'. ie 'no': 'No'"
|
43
|
+
elsif key == true
|
44
|
+
puts "Key #{key} was evaluated as true. Check your yml file and be sure to escape values like yes with 'yes'. ie 'yes': 'Yes'"
|
45
|
+
elsif !translate_hash[key].nil?
|
46
|
+
# pull out all the string substitutions so that google doesn't translate those
|
47
|
+
pattern = /\{\{.+\}\}/
|
48
|
+
holder = '{{---}}'
|
49
|
+
replacements = translate_hash[key].scan(pattern)
|
50
|
+
translate_hash[key].gsub!(pattern, holder)
|
51
|
+
translate_hash[key] = Babelphish::Translator.translate(translate_hash[key], to, from)
|
52
|
+
replacements.each do |r|
|
53
|
+
translate_hash[key].sub!(holder, r)
|
54
|
+
end
|
55
|
+
else
|
56
|
+
puts "Key #{key} contains no data"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def translate_and_write_many_yml(yml, tos, from, overwrite)
|
63
|
+
return unless File.exist?(yml)
|
64
|
+
source = YAML.load_file(yml)
|
65
|
+
translated_source = YAML.load_file(yml)
|
66
|
+
translate_many_yml_keys(translated_source, tos, from)
|
67
|
+
# At this point translated_source contains a translation for every language. Cut it apart into individual hashes
|
68
|
+
tos.each do |to|
|
69
|
+
next if to == from # don't want to overwrite the source file
|
70
|
+
extracted_translation = {}
|
71
|
+
extract_yml_translation(source, translated_source, extracted_translation, to)
|
72
|
+
# change the top level key from the source language to the destination language
|
73
|
+
translated_filename = File.join(File.dirname(yml), "#{to}.yml")
|
74
|
+
return if File.exist?(translated_filename) && !overwrite
|
75
|
+
extracted_translation[to] = extracted_translation[from]
|
76
|
+
extracted_translation.delete(from)
|
77
|
+
File.open(translated_filename, 'w') { |f| f.write(extracted_translation.ya2yaml) }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def extract_yml_translation(source, translated_source, extracted, language)
|
82
|
+
source.each_key do |key|
|
83
|
+
if source[key].is_a?(Hash)
|
84
|
+
extracted[key] = {}
|
85
|
+
extract_yml_translation(source[key], translated_source[key], extracted[key], language)
|
86
|
+
else
|
87
|
+
extracted[key] = translated_source[key][language]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def translate_many_yml_keys(translate_hash, tos, from)
|
93
|
+
translate_hash.each_key do |key|
|
94
|
+
if translate_hash[key].is_a?(Hash)
|
95
|
+
translate_many_yml_keys(translate_hash[key], tos, from)
|
96
|
+
else
|
97
|
+
if key == false
|
98
|
+
puts "Key #{key} was evaluated as false. Check your yml file and be sure it does not include values like no: No"
|
99
|
+
elsif key == true
|
100
|
+
puts "Key #{key} was evaluated as true. Check your yml file and be sure it does not include values like yes: Yes"
|
101
|
+
elsif !translate_hash[key].nil?
|
102
|
+
# pull out all the string substitutions so that google doesn't translate those
|
103
|
+
pattern = /\{\{.+\}\}/
|
104
|
+
holder = '{{---}}'
|
105
|
+
replacements = translate_hash[key].scan(pattern)
|
106
|
+
translate_hash[key].gsub!(pattern, holder)
|
107
|
+
translations = Babelphish::Translator.multiple_translate(translate_hash[key], tos, from)
|
108
|
+
translations.each_key do |locale|
|
109
|
+
replacements.each do |r|
|
110
|
+
translations[locale].sub!(holder, r)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
translate_hash[key] = translations
|
114
|
+
else
|
115
|
+
puts "Key #{key} contains no data"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: babelphish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Ball
|
@@ -66,6 +66,8 @@ files:
|
|
66
66
|
- lib/babelphish/languages.rb
|
67
67
|
- lib/babelphish/translator.rb
|
68
68
|
- lib/babelphish/exceptions.rb
|
69
|
+
- lib/babelphish/html_translator.rb
|
70
|
+
- lib/babelphish/yml_translator.rb
|
69
71
|
- lib/tasks/babelphish.rake
|
70
72
|
- script/console
|
71
73
|
- script/destroy
|