class Terrestrial::Cli::Pull

Public Instance Methods

run() click to toggle source
# File lib/terrestrial/cli/pull.rb, line 9
def run
  Config.load!
  MixpanelClient.track("cli-pull-command")

  fetch_translations
  languages.each do |lang, translations|
    update_translation_file(lang, translations)
  end
  print_confirmation
end

Private Instance Methods

android_translation_file_path(language) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 59
def android_translation_file_path(language)
  folder = Pathname.new(Config[:directory] + "/" + Config[:translation_files].first)
    .parent
    .parent
    .to_s + "/values-#{format_language_code(language)}"

  FileUtils.mkdir_p(folder)
  folder + "/strings.xml"
end
fetch_translations() click to toggle source
# File lib/terrestrial/cli/pull.rb, line 106
def fetch_translations
  @response = web_client.get_translations(Config[:project_id], Config[:app_id])
end
format_language_code(language) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 92
def format_language_code(language)
  lang, region = language.split("-")

  if region
    "#{lang}_#{region.upcase}"
  else
    lang
  end
end
human_readable_language_name(language_code) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 102
def human_readable_language_name(language_code)
  LanguageName.new(language_code).human_readable_name
end
ios_translation_file_path(language) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 49
def ios_translation_file_path(language)
  folder = Pathname.new(Config[:directory] + "/" + Config[:translation_files].first)
    .parent
    .parent
    .to_s + "/#{format_language_code(language)}.lproj"

  FileUtils.mkdir_p(folder)
  folder + "/Localizable.strings"
end
languages() click to toggle source
# File lib/terrestrial/cli/pull.rb, line 110
def languages
  response.body["data"]["translations"]
end
print_confirmation() click to toggle source
response() click to toggle source
# File lib/terrestrial/cli/pull.rb, line 114
def response
  @response
end
string_registry() click to toggle source
# File lib/terrestrial/cli/pull.rb, line 118
def string_registry
  @string_registry ||= StringRegistry.load
end
translation_file_path_for(language) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 39
def translation_file_path_for(language)
  if Config[:platform] == "ios"
    ios_translation_file_path(language)
  elsif Config[:platform] == "android"
    android_translation_file_path(language) 
  elsif Config[:platform] == "unity"
    unity_translation_file_path(language)
  end
end
unity_translation_file_path(language) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 69
def unity_translation_file_path(language)
  folder = Pathname.new(Config[:directory] + "/" + Config[:translation_files].first)
            .parent
            .to_s 

  folder + "/" + human_readable_language_name(language) + ".xml"
end
update_translation_file(language, translations) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 32
def update_translation_file(language, translations)
  write_translation_file(
    translation_file_path_for(language),
    ProcessesTranslations.run(translations, string_registry.entries, Config[:platform])
  )
end
web_client() click to toggle source
# File lib/terrestrial/cli/pull.rb, line 122
def web_client
  @web_client ||= Web.new
end
write_translation_file(path, translations) click to toggle source
# File lib/terrestrial/cli/pull.rb, line 77
def write_translation_file(path, translations)
  File.open(path, "w+") do |f|
    if Config[:platform] == "ios"
      f.write "// Updated by Terrestrial #{Time.now.to_s}\n\n"
      f.write DotStringsFormatter.new(translations).format_foreign_translation
    elsif Config[:platform] == "android"
      f.write "<!-- Updated by Terrestrial #{Time.now.to_s} -->\n\n"
      f.write AndroidXmlFormatter.new(translations).format_foreign_translation
    elsif Config[:platform] == "unity"
      f.write "<!-- Updated by Terrestrial #{Time.now.to_s} -->\n\n"
      f.write UnityFormatter.new(translations).format_foreign_translation
    end
  end
end