Class: Translator::Base
Overview
Finds English language translation keys which have not been translated and translates them through Google Translate.
Usage:
Translator::Yaml.new(:source => 'en.yml', :destination => 'output.yml).translate
Direct Known Subclasses
Constant Summary
Constants included from Support
Instance Attribute Summary collapse
-
#destination ⇒ Object
instance methods.
-
#source ⇒ Object
instance methods.
Class Method Summary collapse
Instance Method Summary collapse
- #all_keys(lang) ⇒ Object
- #clear_all_keys ⇒ Object
- #comment?(line) ⇒ Boolean
- #copy_and_translate_line(line, lang) ⇒ Object
- #copy_lines_to_all_locales ⇒ Object
- #each_line ⇒ Object
- #format(key, value) ⇒ Object
-
#initialize(options = {}) ⇒ Base
constructor
A new instance of Base.
- #key_and_value_from_line(line) ⇒ Object
- #new_translation_message ⇒ Object
- #non_english_locales ⇒ Object
- #non_us_locales ⇒ Object
- #parse_template(p) ⇒ Object
- #post_process(value, lang) ⇒ Object
- #pre_process(value, lang) ⇒ Object
- #translate ⇒ Object
- #translate_key(value, lang) ⇒ Object
- #translate_new_key(line, lang) ⇒ Object
- #write_content(destination, content) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new instance of Base.
51 52 53 54 |
# File 'lib/translator.rb', line 51 def initialize(={}) @source = [:source] @destination = [:destination] end |
Instance Attribute Details
#destination ⇒ Object
instance methods
49 50 51 |
# File 'lib/translator.rb', line 49 def destination @destination end |
#source ⇒ Object
instance methods
49 50 51 |
# File 'lib/translator.rb', line 49 def source @source end |
Class Method Details
.all_source_files ⇒ Object
131 132 133 |
# File 'lib/translator.rb', line 131 def self.all_source_files raise "Define in child" end |
.template ⇒ Object
43 44 45 |
# File 'lib/translator.rb', line 43 def self.template raise "Define in child" end |
Instance Method Details
#all_keys(lang) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/translator.rb', line 121 def all_keys(lang) unless @all_keys @all_keys = {} Dir["#{language_path(lang)}/#{all_source_files}"].each do |p| @all_keys = @all_keys.merge(parse_template(p)) end end @all_keys end |
#clear_all_keys ⇒ Object
139 140 141 |
# File 'lib/translator.rb', line 139 def clear_all_keys @all_keys = nil end |
#comment?(line) ⇒ Boolean
222 223 224 |
# File 'lib/translator.rb', line 222 def comment?(line) raise "Define in child" end |
#copy_and_translate_line(line, lang) ⇒ Object
143 144 145 146 147 148 149 150 |
# File 'lib/translator.rb', line 143 def copy_and_translate_line(line, lang) line = line.split("\n").first if comment?(line) || line.strip == "" nil else translate_new_key(line, lang) end end |
#copy_lines_to_all_locales ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/translator.rb', line 72 def copy_lines_to_all_locales write_content(destination, ) non_us_locales.each do |lang, code| new_content = each_line do |line| copy_and_translate_line(line, lang) end write_content(destination, new_content) clear_all_keys end end |
#each_line ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/translator.rb', line 110 def each_line output = [] File.open(source, "r") do |f| f.readlines.each do |line| new_line = yield line output << new_line end end output.flatten.join("\n") end |
#format(key, value) ⇒ Object
214 215 216 |
# File 'lib/translator.rb', line 214 def format(key, value) raise "Define in child" end |
#key_and_value_from_line(line) ⇒ Object
218 219 220 |
# File 'lib/translator.rb', line 218 def key_and_value_from_line(line) raise "Define in child" end |
#new_translation_message ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/translator.rb', line 94 def now = Time.now date = now.day month = now.month year = now.year = "#{month}/#{date}/#{year}" output = [] output << "# " output << "# Keys translated automatically on #{}." output << "# " output.join("\n") end |
#non_english_locales ⇒ Object
60 61 62 63 64 |
# File 'lib/translator.rb', line 60 def non_english_locales @non_english_locales ||= LOCALES.select do |lang, code| lang !~ /^en/ end end |
#non_us_locales ⇒ Object
66 67 68 69 70 |
# File 'lib/translator.rb', line 66 def non_us_locales @non_us_locales ||= LOCALES.select do |lang, code| lang != "en-US" end end |
#parse_template(p) ⇒ Object
135 136 137 |
# File 'lib/translator.rb', line 135 def parse_template(p) raise "Define in child" end |
#post_process(value, lang) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/translator.rb', line 180 def post_process(value, lang) if lang =~ /zh/ value.gsub!("<strong>", "") value.gsub!("</strong>", "") end value.gsub!(/^#{194.chr}#{160.chr}/, "") value.gsub!(" ]", "]") value.gsub!("«", "\"") value.gsub!("»", "\"") value.gsub!(/\"\.$/, ".\"") value.gsub!(/\\ \"/, "\\\"") value.gsub!(/<\/ /, "<\/") value.gsub!(/(“|”)/, "\"") value.gsub!("<strong> ", "<strong>") value.gsub!(" </strong>", "</strong>") value.gsub!(""", "\"") value.gsub!("'", "\"") value.gsub!("> ", ">") value.gsub!("\"", "'") value.gsub!(/^\'/, "\"") value.gsub!(/\'$/, "\"") value.gsub!(" \"O", " \\\"O") value.gsub!(/\((0)\)/, "{0}") value.gsub!(/\((1)\)/, "{1}") value.gsub!(/\((2)\)/, "{2}") value.gsub!("(0)", "{0}") value.strip end |
#pre_process(value, lang) ⇒ Object
176 177 178 |
# File 'lib/translator.rb', line 176 def pre_process(value, lang) value end |
#translate ⇒ Object
56 57 58 |
# File 'lib/translator.rb', line 56 def translate copy_lines_to_all_locales end |
#translate_key(value, lang) ⇒ Object
169 170 171 172 173 174 |
# File 'lib/translator.rb', line 169 def translate_key(value, lang) code = LOCALES[lang] value = pre_process(value, lang) translation = Translate.t(value, "ENGLISH", code) post_process(translation, lang) end |
#translate_new_key(line, lang) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/translator.rb', line 152 def translate_new_key(line, lang) k, v = key_and_value_from_line(line) if k if k == "en" format(lang, "") else if v.strip == "" format(k, "") else format(k, translate_key(v, lang)) end end else nil end end |
#write_content(destination, content) ⇒ Object
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/translator.rb', line 83 def write_content(destination, content) if content && content.strip != "" puts content puts File.open(destination, "a") do |f| f.puts f.puts content end end end |