class Lokale::Writer

Public Instance Methods

append_new_strings(lstrings, file) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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