[ Index ]

PHP Cross Reference of GlotPress

title

Body

[close]

/gp-includes/cli/ -> translation-set.php (source)

   1  <?php
   2  
   3  class GP_CLI_Translation_Set extends WP_CLI_Command {
   4      /**
   5       * Get a translation set for a project.
   6       *
   7       * @param string $project Project path
   8       * @param string $locale Locale slug
   9       * @param string $set Set slug
  10       * @return GP_Translation_Set|WP_Error Translation set if available, error otherwise.
  11       */
  12  	protected function get_translation_set( $project, $locale, $set = 'default' ) {
  13          $this->project = GP::$project->by_path( $project );
  14          if ( ! $this->project ) {
  15              return new WP_Error( 'gp_set_no_project', __( 'Project not found!', 'glotpress' ) );
  16          }
  17  
  18          $this->locale = GP_Locales::by_slug( $locale );
  19          if ( ! $this->locale ) {
  20              return new WP_Error( 'gp_set_no_locale', __( 'Locale not found!', 'glotpress' ) );
  21          }
  22  
  23          $this->translation_set = GP::$translation_set->by_project_id_slug_and_locale( $this->project->id, $set, $this->locale->slug );
  24          if ( ! $this->translation_set ) {
  25              return new WP_Error( 'gp_set_not_found', __( 'Translation set not found!', 'glotpress' ) );
  26          }
  27  
  28          return $this->translation_set;
  29      }
  30  
  31      /**
  32       * Export the translation set
  33       *
  34       * ## OPTIONS
  35       *
  36       * <project>
  37       * : Project path
  38       *
  39       * <locale>
  40       * : Locale to export
  41       *
  42       * [--set=<set>]
  43       * : Translation set slug; default is "default"
  44       *
  45       * [--format=<format>]
  46       * : Format for output (one of "po", "mo", "android", "resx", "strings"; default is "po")
  47       *
  48       * [--search=<search>]
  49       * : Search term
  50       *
  51       * [--status=<status>]
  52       * : Translation string status; default is "current"
  53       *
  54       * [--priority=<priorities>]
  55       * : Original priorities, comma separated. Possible values are "hidden,low,normal,high"
  56       */
  57  	public function export( $args, $assoc_args ) {
  58          $set_slug        = isset( $assoc_args['set'] ) ? $assoc_args['set'] : 'default';
  59          $translation_set = $this->get_translation_set( $args[0], $args[1], $set_slug );
  60          if ( is_wp_error( $translation_set ) ) {
  61              WP_CLI::error( $translation_set->get_error_message() );
  62          }
  63  
  64          $format = isset( $assoc_args['format'] ) ? $assoc_args['format'] : 'po';
  65          $format = gp_array_get( GP::$formats, $format, null );
  66          if ( ! $format ) {
  67              WP_CLI::error( __( 'No such format.', 'glotpress' ) );
  68          }
  69  
  70          $filters = array();
  71          if ( isset( $assoc_args['search'] ) ) {
  72              $filters['term'] = $assoc_args['search'];
  73          }
  74          $filters['status'] = isset( $assoc_args['status'] ) ? $assoc_args['status'] : 'current';
  75  
  76          if ( isset( $assoc_args['priority'] ) ) {
  77  
  78              $filters['priority'] = array();
  79  
  80              $priorities       = explode( ',', $assoc_args['priority'] );
  81              $valid_priorities = GP::$original->get_static( 'priorities' );
  82  
  83              foreach ( $priorities as $priority ) {
  84                  $key = array_search( $priority, $valid_priorities, true );
  85                  if ( false === $key ) {
  86                      WP_CLI::warning( sprintf( 'Invalid priority %s', $priority ) );
  87                  } else {
  88                      $filters['priority'][] = $key;
  89                  }
  90              }
  91          }
  92  
  93          $entries = GP::$translation->for_export( $this->project, $translation_set, $filters );
  94          WP_CLI::line( $format->print_exported_file( $this->project, $this->locale, $translation_set, $entries ) );
  95      }
  96  
  97      /**
  98       * Import a file into the translation set
  99       *
 100       * ## OPTIONS
 101       *
 102       * <project>
 103       * : Project path
 104       *
 105       * <locale>
 106       * : Locale to export
 107       *
 108       * <file>
 109       * : File to import
 110       *
 111       * [--set=<set>]
 112       * : Translation set slug; default is "default"
 113       *
 114       * [--status=<status>]
 115       * : Translation string status; default is "current"
 116       */
 117  	public function import( $args, $assoc_args ) {
 118          $set_slug        = isset( $assoc_args['set'] ) ? $assoc_args['set'] : 'default';
 119          $translation_set = $this->get_translation_set( $args[0], $args[1], $set_slug );
 120          if ( is_wp_error( $translation_set ) ) {
 121              WP_CLI::error( $translation_set->get_error_message() );
 122          }
 123  
 124          $po       = new PO();
 125          $imported = $po->import_from_file( $args[2] );
 126          if ( ! $imported ) {
 127              WP_CLI::error( __( "Couldn't load translations from file!", 'glotpress' ) );
 128          }
 129  
 130          $desired_status = isset( $assoc_args['status'] ) ? $assoc_args['status'] : 'current';
 131  
 132          $added = $translation_set->import( $po, $desired_status );
 133  
 134          /* translators: %s: Number of imported translations */
 135          WP_CLI::line( sprintf( _n( '%s translation was added', '%s translations were added', $added, 'glotpress' ), $added ) );
 136      }
 137  
 138      /**
 139       * Recheck warnings for the translation set
 140       *
 141       * ## OPTIONS
 142       *
 143       * <project>
 144       * : Project path
 145       *
 146       * <locale>
 147       * : Locale to export
 148       *
 149       * [--set=<set>]
 150       * : Translation set slug; default is "default"
 151       *
 152       * @subcommand recheck-warnings
 153       */
 154  	public function recheck_warnings( $args, $assoc_args ) {
 155          $set_slug        = isset( $assoc_args['set'] ) ? $assoc_args['set'] : 'default';
 156          $translation_set = $this->get_translation_set( $args[0], $args[1], $set_slug );
 157          if ( is_wp_error( $translation_set ) ) {
 158              WP_CLI::error( $translation_set->get_error_message() );
 159          }
 160  
 161          $project = GP::$project->get( $translation_set->project_id );
 162          $locale  = GP_Locales::by_slug( $translation_set->locale );
 163          foreach ( GP::$translation->for_translation( $project, $translation_set, 'no-limit' ) as $entry ) {
 164              $warnings = GP::$translation_warnings->check( $entry->singular, $entry->plural, $entry->translations, $locale );
 165              if ( $warnings == $entry->warnings ) {
 166                  continue;
 167              }
 168  
 169              $translation = new GP_Translation( array( 'id' => $entry->id ) );
 170              /* translators: %s: ID of a translation */
 171              WP_CLI::line( sprintf( __( 'Updating warnings for #%s', 'glotpress' ), $entry->id ) );
 172              $translation->update( array( 'warnings' => $warnings ) );
 173          }
 174      }
 175  
 176  }


Generated: Sun Dec 22 01:01:04 2024 Cross-referenced by PHPXref 0.7.1