Class: Translator::Base

Inherits:
Object
  • Object
show all
Includes:
Support
Defined in:
lib/translator.rb

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

Yaml

Constant Summary

Constants included from Support

Support::LOCALES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



51
52
53
54
# File 'lib/translator.rb', line 51

def initialize(options={})
  @source = options[:source]
  @destination = options[:destination]
end

Instance Attribute Details

#destinationObject

instance methods



49
50
51
# File 'lib/translator.rb', line 49

def destination
  @destination
end

#sourceObject

instance methods



49
50
51
# File 'lib/translator.rb', line 49

def source
  @source
end

Class Method Details

.all_source_filesObject



131
132
133
# File 'lib/translator.rb', line 131

def self.all_source_files
  raise "Define in child"
end

.templateObject



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_keysObject



139
140
141
# File 'lib/translator.rb', line 139

def clear_all_keys
  @all_keys = nil
end

#comment?(line) ⇒ Boolean

Returns:

  • (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_localesObject



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, new_translation_message)
  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_lineObject



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_messageObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/translator.rb', line 94

def new_translation_message
  now = Time.now
  
  date = now.day
  month = now.month
  year = now.year
  
  timestamp = "#{month}/#{date}/#{year}"
  output = []
  output << "# "
  output << "# Keys translated automatically on #{timestamp}."
  output << "# "
  
  output.join("\n")
end

#non_english_localesObject



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_localesObject



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!("&quot;", "\"")
  value.gsub!("&#39;", "\"")
  value.gsub!("&gt; ", ">")
  
  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

#translateObject



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