Class: Er18Ern::Translator

Inherits:
Object
  • Object
show all
Defined in:
lib/er18ern.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#google_translator_hax_locale_dirsObject

Returns the value of attribute google_translator_hax_locale_dirs.



93
94
95
# File 'lib/er18ern.rb', line 93

def google_translator_hax_locale_dirs
  @google_translator_hax_locale_dirs
end

#locales_dirObject

Returns the value of attribute locales_dir.



93
94
95
# File 'lib/er18ern.rb', line 93

def locales_dir
  @locales_dir
end

Instance Method Details

#copy_missing_into!(place) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/er18ern.rb', line 177

def copy_missing_into!(place)
  en_source_yaml["en"]
  output = YAML::load_file(File.expand_path("#{place}.yml", self.locales_dir))
  traverse = Proc.new do |x, y|
    if x.is_a?(Hash)
      x.each do |k, v|
        y[k] ||= v
        traverse.call(v, y[k])
      end
    end
  end
  traverse.call(en_source_yaml["en"], output[place])
  File.open(File.expand_path("#{place}.yml", self.locales_dir), "w") do |fp|
    fp.write(output.to_yaml)
  end
end

#en_source_yamlObject



95
96
97
# File 'lib/er18ern.rb', line 95

def en_source_yaml
  @en_source_yaml ||= YAML::load_file(File.expand_path('en.yml', self.locales_dir))
end

#found_keysObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/er18ern.rb', line 128

def found_keys
  @found_keys ||= begin
    found_keys = {}
    key_collector = Proc.new do |x, key_prefix|
      if x.is_a?(Hash)
        result = {}
        x.each do |k, v|
          result[k] = key_collector.call(v, "#{key_prefix}.#{k}")
        end
        result
      else
        found_keys[key_prefix] = x
      end
    end
    key_collector.call(en_source_yaml["en"], "")
    found_keys
  end
end

#generate_ermahgerd!Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/er18ern.rb', line 99

def generate_ermahgerd!
  traverse = Proc.new do |x, translator|
    if x.is_a?(Hash)
      result = {}
      x.each do |k, v|
        result[k] = traverse.call(v, translator)
      end
      result
    else
      translator.call(x)
    end
  end

  errm = {}
  errm['ermahgerd'] = en_source_yaml["en"]

  #save ermahgerd
  File.open(File.expand_path('ermahgerd.yml', self.locales_dir), "w") do |fp|
    fp.write(traverse.call(errm, lambda{|x| ImprovedErmahgerd.translate(x)}).to_yaml)
  end
end

#generate_google_translations!Object



147
148
149
150
151
152
153
154
155
# File 'lib/er18ern.rb', line 147

def generate_google_translations!
  vals = found_keys.values.uniq.map do |val|
    strip_string(val)
  end

  File.open(File.expand_path('GOOGLE_TRANSLATE_INPUT', self.google_translator_hax_locale_dirs), "w") do |fp|
    fp.write(vals.join("\n---\n"))
  end
end

#resave_en!Object



121
122
123
124
125
126
# File 'lib/er18ern.rb', line 121

def resave_en!
  #re-save EN
  File.open(File.expand_path('en.yml', self.locales_dir), "w") do |fp|
    fp.write(en_source_yaml.to_yaml)
  end
end

#save_engrish!Object



157
158
159
160
161
162
163
164
165
# File 'lib/er18ern.rb', line 157

def save_engrish!
  enstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_INPUT', self.google_translator_hax_locale_dirs)).split("\n---\n")
  engrishstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_AGAIN', self.google_translator_hax_locale_dirs)).split("\n---\n")
  engrish = doitstuff(enstuff, found_keys, "engrish", engrishstuff)

  File.open(File.expand_path('engrish.yml', self.locales_dir), "w") do |fp|
    fp.write(engrish.to_yaml)
  end
end

#save_jp!Object



167
168
169
170
171
172
173
174
175
# File 'lib/er18ern.rb', line 167

def save_jp!
  enstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_INPUT', self.google_translator_hax_locale_dirs)).split("\n---\n")
  jpstuff = File.read(File.expand_path('GOOGLE_TRANSLATE_OUTPUT', self.google_translator_hax_locale_dirs)).split("\n---\n")
  jp = doitstuff(enstuff, found_keys, "jp", jpstuff)

  File.open(File.expand_path('jp.yml', self.locales_dir), "w") do |fp|
    fp.write(jp.to_yaml)
  end
end