public function import_translations_post($project_path, $locale_slug, $translation_set_slug) { $project = GP::$project->by_path($project_path); $locale = GP_Locales::by_slug($locale_slug); if (!$project || !$locale) { return $this->die_with_404(); } if ($this->invalid_nonce_and_redirect('import-translations_' . $project->id)) { return; } $translation_set = GP::$translation_set->by_project_id_slug_and_locale($project->id, $translation_set_slug, $locale_slug); if (!$translation_set) { return $this->die_with_404(); } $can_import_current = $this->can('approve', 'translation-set', $translation_set->id); $can_import_waiting = $can_import_current || $this->can('import-waiting', 'translation-set', $translation_set->id); if (!$can_import_current && !$can_import_waiting) { $this->redirect_with_error(__('You are not allowed to do that!', 'glotpress')); return; } $import_status = gp_post('status', 'waiting'); $allowed_import_status = array(); if ($can_import_current) { $allowed_import_status[] = 'current'; } if ($can_import_waiting) { $allowed_import_status[] = 'waiting'; } if (!in_array($import_status, $allowed_import_status, true)) { $this->redirect_with_error(__('Invalid translation status.', 'glotpress')); return; } if (!is_uploaded_file($_FILES['import-file']['tmp_name'])) { $this->redirect_with_error(__('Error uploading the file.', 'glotpress')); return; } $format = gp_get_import_file_format(gp_post('format', 'po'), $_FILES['import-file']['name']); if (!$format) { $this->redirect_with_error(__('No such format.', 'glotpress')); return; } $translations = $format->read_translations_from_file($_FILES['import-file']['tmp_name'], $project); if (!$translations) { $this->redirect_with_error(__('Couldn’t load translations from file!', 'glotpress')); return; } $translations_added = $translation_set->import($translations, $import_status); $this->notices[] = sprintf(__('%s translations were added', 'glotpress'), $translations_added); $this->redirect(gp_url_project($project, gp_url_join($locale->slug, $translation_set->slug))); }
function test_gp_get_import_file_format() { // Test to make sure we detect each file format extension correctly. foreach (GP::$formats as $format) { foreach ($format->get_file_extensions() as $extension) { $this->assertEquals($format, gp_get_import_file_format(null, 'filename.' . $extension)); } } // Test to make sure we don't auto detect if a known file format is passed in. $this->assertEquals(GP::$formats['po'], gp_get_import_file_format('po', 'filename.strings')); // Test to make sure we return null when no file format is found. $this->assertEquals(null, gp_get_import_file_format('thiswillneverbeafileformat', 'filename.thiswillneverbeafileformat')); $this->assertEquals(null, gp_get_import_file_format(null, 'filename.thiswillneverbeafileformat')); }
public function import_originals_post($project_path) { $project = GP::$project->by_path($project_path); if (!$project) { return $this->die_with_404(); } if ($this->invalid_nonce_and_redirect('import-originals_' . $project->id)) { return; } if ($this->cannot_and_redirect('write', 'project', $project->id)) { return; } if (!is_uploaded_file($_FILES['import-file']['tmp_name'])) { // TODO: different errors for different upload conditions $this->redirect_with_error(__('Error uploading the file.', 'glotpress')); return; } $format = gp_get_import_file_format(gp_post('format', 'po'), $_FILES['import-file']['name']); if (!$format) { $this->redirect_with_error(__('No such format.', 'glotpress')); return; } $translations = $format->read_originals_from_file($_FILES['import-file']['tmp_name']); if (!$translations) { $this->redirect_with_error(__('Couldn’t load translations from file!', 'glotpress')); return; } list($originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted, $originals_error) = GP::$original->import_for_project($project, $translations); $notice = sprintf(__('%1$s new strings added, %2$s updated, %3$s fuzzied, and %4$s obsoleted.', 'glotpress'), $originals_added, $originals_existing, $originals_fuzzied, $originals_obsoleted); if ($originals_error) { $notice .= ' ' . sprintf(_n('%s new string was not imported due to an error.', '%s new strings were not imported due to an error.', $originals_error, 'glotpress'), $originals_error); } $this->notices[] = $notice; $this->redirect(gp_url_project($project)); }