public function bulk_translate($project_path) { $project_path = urldecode($project_path); $url = gp_url_project($project_path); // If we don't have rights, just redirect back to the project. if (!GP::$user->current()->can('write', 'project')) { gp_redirect($url); } // Create a project class to use to get the project object. $project_class = new GP_Project(); // Get the project object from the project path that was passed in. $project_obj = $project_class->by_path($project_path); // Get the translations sets from the project ID. $translation_sets = GP::$translation_set->by_project_id($project_obj->id); // Loop through all the sets. foreach ($translation_sets as $set) { //Array ( [action] => gtranslate [priority] => 0 [redirect_to] => http://localhost/wp40/gp/projects/sample/bg/my [row-ids] => Array ( [0] => 1 [1] => 2 ) ) $bulk = array('action' => 'gtranslate', 'priority' => 0, 'row-ids' => array()); $translation = new GP_Translation(); $strings = $translation->for_translation($project_obj, $set, null, array('status' => 'untranslated')); foreach ($strings as $string) { $bulk['row-ids'][] .= $string->row_id; } $locale = GP_Locales::by_slug($set->locale); $this->gp_translation_set_bulk_action_post($project_obj, $locale, $set, $bulk); } $url = gp_url_project($project_path); gp_redirect($url); }
function action_on_translation_set($translation_set) { $project = GP::$project->get($translation_set->project_id); $locale = GP_Locales::by_slug($translation_set->locale); foreach (GP::$translation->for_translation($project, $translation_set, 'no-limit') as $entry) { $warnings = GP::$translation_warnings->check($entry->singular, $entry->plural, $entry->translations, $locale); if ($warnings != $entry->warnings) { $translation = new GP_Translation(array('id' => $entry->id)); echo sprintf(__("Updating warnings for %s"), $entry->id) . "\n"; $translation->update(array('warnings' => $warnings)); } } }
/** * Recheck warnings for the translation set * * ## OPTIONS * * <project> * : Project path * * <locale> * : Locale to export * * [--set=<set>] * : Translation set slug; default is "default" * * @subcommand recheck-warnings */ public function recheck_warnings($args, $assoc_args) { $set_slug = isset($assoc_args['set']) ? $assoc_args['set'] : 'default'; $translation_set = $this->get_translation_set($args[0], $args[1], $set_slug); if (is_wp_error($translation_set)) { WP_CLI::error($translation_set->get_error_message()); } $project = GP::$project->get($translation_set->project_id); $locale = GP_Locales::by_slug($translation_set->locale); foreach (GP::$translation->for_translation($project, $translation_set, 'no-limit') as $entry) { $warnings = GP::$translation_warnings->check($entry->singular, $entry->plural, $entry->translations, $locale); if ($warnings == $entry->warnings) { continue; } $translation = new GP_Translation(array('id' => $entry->id)); /* translators: %s: ID of a translation */ WP_CLI::line(sprintf(__('Updating warnings for #%s', 'glotpress'), $entry->id)); $translation->update(array('warnings' => $warnings)); } }
/** * Discard a warning. * * @since 1.0.0 * * @param GP_Project $project The project. * @param GP_Locale $locale The GlotPress locale. * @param GP_Translation_Set $translation_set The translation set. * @param GP_Translation $translation The translation object. */ private function discard_warning_edit_function($project, $locale, $translation_set, $translation) { if (!isset($translation->warnings[gp_post('index')][gp_post('key')])) { return $this->die_with_error('The warning doesn’exist!'); } $warning = array('project_id' => $project->id, 'translation_set' => $translation_set->id, 'translation' => $translation->id, 'warning' => gp_post('key'), 'user' => get_current_user_id()); /** * Fires before a warning gets discarded. * * @since 1.0.0 * * @param array $warning { * @type string $project_id ID of the project. * @type string $translation_set ID of the translation set. * @type string $translation ID of the translation. * @type string $warning The warning key. * @type int $user Current user's ID. * } */ do_action_ref_array('gp_warning_discarded', $warning); unset($translation->warnings[gp_post('index')][gp_post('key')]); if (empty($translation->warnings[gp_post('index')])) { unset($translation->warnings[gp_post('index')]); } $res = $translation->save(); if (false === $res || null === $res) { return $this->die_with_error('Error in saving the translation!'); } }