module PolyglotIos module Command class Pull include Helper::Terminal include Helper::General def self.init new.call end def call prompt.say("Fetching translations...") generate_translations(project_configs, programming_language) success("Translations successfully generated!") end private def generate_translations(projects, programming_language) projects.each do |project| project_id = project[:id] languages = pull_languages(project_id) translation_keys = pull_translation_keys(project_id) write_translations(translation_keys, languages, project[:path], use_old_naming) write_languages(languages, project[:sourceFilesPath], programming_language) write_translation_sources(translation_keys, project[:sourceFilesPath], programming_language) end end # Serializing data def write_translations(translation_keys, languages, translations_path, use_old_naming = false) return if translations_path.to_s.empty? languages.each do |language| PolyglotIos::Serializer::Translation .write(translation_keys, language, translations_path, use_old_naming) end end def write_languages(languages, sources_path, programming_language) return if sources_path.to_s.empty? case programming_language.downcase when "objc" serializer = PolyglotIos::Serializer::Language::ObjC when "swift" serializer = PolyglotIos::Serializer::Language::Swift end serializer.new(languages).save(sources_path) end def write_translation_sources(translation_keys, sources_path, programming_language) return if sources_path.to_s.empty? case programming_language.downcase when "swift" serializer = PolyglotIos::Serializer::Source::Swift serializer .new(translation_keys) .save(sources_path) end end # Pulling data def pull_languages(project_id) PolyglotIos::Resource::Language .token(token) .depaginate(project_id: project_id) end def pull_translation_keys(project_id) PolyglotIos::Resource::TranslationKey .token(token) .depaginate(project_id: project_id) .sort_by { |key| key.name.downcase } end end end end