Class: Lokale::Writer

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

Instance Method Summary collapse

Instance Method Details

#append_new_strings(lstrings, file) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/lokale/agent.rb', line 225

def append_new_strings(lstrings, file)
  content = file.content

  puts "Appending #{lstrings.size} new strings to file #{file.lang}/#{file.full_name}:"
  lstrings.each { |ls| puts ls.pretty }
  puts

  append_at = find_append_point(content)
  data_to_append = "\n\n" + lstrings.map { |ls| ls.write_format }.join("\n").chomp("\n")

  content.insert(append_at, data_to_append)
  file.write(content)
end

#find_append_point(content) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/lokale/agent.rb', line 208

def find_append_point(content)
  ignore_after = (content =~ /^\s*?\/\/\s*?\n\s*?\/\/\s*?MARK/) || Float::INFINITY
  string_regexp = /^".*?"\s*=\s*".*"\s*;/

  append_at = content.match(string_regexp).end(0)
  return if append_at.nil?
  next_try = append_at
  while next_try < ignore_after
    append_at = next_try
    next_match = content.match(string_regexp, next_try)
    break if next_match.nil?
    next_try = next_match.end(0)
  end

  append_at
end

#hash_string(hash, depth) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/lokale/agent.rb', line 239

def hash_string(hash, depth)
  total_string = ""
  tab = "    " * depth
  hash.each do |k, v|
    case v
    when LString
      total_string += tab + "static let #{v.key.split(".")[-1].camelize(:lower)} = NSLocalizedString(\"#{v.key}\", comment:\"#{v.note}\")\n"
    when Hash
      total_string += tab + "\n"
      total_string += tab + "class #{k.camelize} {\n"
      total_string += hash_string(v, depth + 1)
      total_string += tab + "}\n\n"
    end
  end

  total_string
end

#write_to_project_file(lstrings, file) ⇒ Object



257
258
259
260
261
262
263
264
265
266
# File 'lib/lokale/agent.rb', line 257

def write_to_project_file(lstrings, file)
  root = Hash.new
  lstrings.each do |ls|
    root.set_by_key_path ls.key.split("."), ls
  end

  content = "\nextension String {\n\n" + hash_string(root, 1) + "}\n"
  File.write(file, content)

end