Unverified Commit 4256b393 authored by Stefan Auditor's avatar Stefan Auditor Committed by Stefan Auditor
Browse files

Issue #3282696 by sanduhrs: Port of l10n_community: Fix po-file import

parent d5a44f88
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -41,6 +41,51 @@ define('L10N_STATUS_IS_TRANSLATION', 16);
 */
define('L10N_STATUS_IS_SUGGESTION', 32);

/**
 * Used to mark counting duplicates.
 */
define('L10N_COUNT_DUPLICATE', 'duplicate');

/**
 * Used to mark counting unchanged strings.
 */
define('L10N_COUNT_UNCHANGED', 'unchanged');

/**
 * Used to mark counting ignored strings.
 */
define('L10N_COUNT_IGNORED', 'ignored');

/**
 * Used to mark counting suggested strings.
 */
define('L10N_COUNT_SUGGESTED', 'suggested');

/**
 * Used to mark counting added strings.
 */
define('L10N_COUNT_ADDED', 'added');

/**
 * Used to mark counting updated strings.
 */
define('L10N_COUNT_UPDATED', 'updated');

/**
 * Used to mark counting declined strings.
 */
define('L10N_COUNT_DECLINED', 'declined');

/**
 * Used to mark counting declined suggestions.
 */
define('L10N_COUNT_SUGGESTION_DECLINED', 'suggestion_declined');

/**
 * Used to mark counting approved suggestions.
 */
define('L10N_COUNT_APPROVED', 'approved');

/**
 * Implements hook_theme().
 */
+44 −2
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ namespace Drupal\l10n_community\Form;
use Drupal\Component\Utility\Environment;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\l10n_server\L10nPo;

/**
 * Translation export form.
@@ -146,8 +147,7 @@ class ImportForm extends FormBase {
        $form_state->getValue('import_uid'),
      ]);
      if ($parsed) {
        // @todo Inform the user of the updates.
        // l10n_community_update_message();
         $this->updateMessage();
        // @todo Clear cache where stats are shown.
        // phpcs:ignore
        // cache_clear_all('l10n:stats:' . $form_state['values']['langcode'], 'cache');
@@ -161,4 +161,46 @@ class ImportForm extends FormBase {

  }

  /**
   * Set a message based on the number of translations changed.
   *
   * Used by both the save and import process.
   */
  public function updateMessage() {
    $counters = L10nPo::counter();
    $messages = [];

    if (!empty($counters[L10N_COUNT_DECLINED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_DECLINED], '1 translation declined', '@count translations declined');
    }
    if (!empty($counters[L10N_COUNT_SUGGESTION_DECLINED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_SUGGESTION_DECLINED], '1 suggestion declined', '@count suggestions declined');
    }
    if (!empty($counters[L10N_COUNT_APPROVED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_APPROVED], '1 translation approved', '@count translations approved');
    }
    if (!empty($counters[L10N_COUNT_ADDED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_ADDED], '1 translation added', '@count translations added');
    }
    if (!empty($counters[L10N_COUNT_SUGGESTED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_SUGGESTED], '1 suggestion added', '@count suggestions added');
    }
    if (!empty($counters[L10N_COUNT_UPDATED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_UPDATED], '1 translation updated', '@count translations updated');
    }
    if (!empty($counters[L10N_COUNT_DUPLICATE])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_DUPLICATE], '1 duplicate translation not saved', '@count duplicate translations not saved');
    }
    if (!empty($counters[L10N_COUNT_IGNORED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_IGNORED], '1 source string not found; its translation was ignored', '@count source strings not found; their translations were ignored');
    }
    if (!empty($counters[L10N_COUNT_UNCHANGED])) {
      $messages[] = $this->formatPlural($counters[L10N_COUNT_UNCHANGED], '1 translation unchanged', '@count translations unchanged');
    }

    if ($messages) {
      \Drupal::messenger()->addStatus(implode(', ', $messages));
    }
  }

}
+0 −11
Original line number Diff line number Diff line
@@ -303,7 +303,6 @@ class L10nPo {
   * @throws \Exception
   */
  public static function importOneString(array $value, string $langcode = NULL, bool $is_suggestion = FALSE, int $uid = NULL) {
    \Drupal::messenger()->addError(__METHOD__);
    $user = \Drupal::currentUser()->getAccount();

    // Trim translation (we will apply source string based whitespace later).
@@ -331,10 +330,7 @@ class L10nPo {
        ->query("SELECT sid FROM {l10n_server_string} WHERE hashkey = :hashkey", [
          ':hashkey' => md5($value['msgid'] . $value['msgctxt']),
        ])->fetchField();
      \Drupal::messenger()->addError('SID::' . $sid);
      if ($sid) {
        \Drupal::messenger()->addError('SID::' . $sid);

        // Merge plural versions into one for saving values.
        $value['msgstr'] = is_array($value['msgstr']) ? implode("\0", $value['msgstr']) : $value['msgstr'];

@@ -384,8 +380,6 @@ class L10nPo {
   * @throws \Exception
   */
  public static function addSuggestion(int $sid, string $translation, string $langcode, int $uid_attribution, int $uid_user, string $medium, bool $force = FALSE) {
    \Drupal::messenger()->addError(__METHOD__);

    $database = \Drupal::database();
    $translation_storage = \Drupal::entityTypeManager()
      ->getStorage('l10n_server_translation');
@@ -419,7 +413,6 @@ class L10nPo {
      ->fetchObject();

    if (!empty($existing)) {
      \Drupal::messenger()->addError('!empty($existing)');
      if ($existing->status == 0
          || ($existing->suggestion == 1 && $force)) {
        // If the existing item is not active, make it an active suggestion and
@@ -441,7 +434,6 @@ class L10nPo {
      }
    }
    else {
      \Drupal::messenger()->addError('insert new');
      // Insert the new suggestion.
      $translation = $translation_storage
        ->create([
@@ -488,7 +480,6 @@ class L10nPo {
   * @throws \Exception
   */
  public static function approveString(string $langcode, int $sid, int $tid) {
    \Drupal::messenger()->addError(__METHOD__);
    $database = \Drupal::database();
    $translation_storage = \Drupal::entityTypeManager()
      ->getStorage('l10n_server_translation');
@@ -552,7 +543,6 @@ class L10nPo {
   *   Optional increment for the counter. Defaults to 1.
   */
  public static function counter($field = NULL, int $increment = 1) {
    \Drupal::messenger()->addError(__METHOD__);
    static $counters = [];

    if (isset($field)) {
@@ -579,7 +569,6 @@ class L10nPo {
   * @throws \Exception
   */
  public static function updateStringStatus(string $langcode, int $sid) {
    \Drupal::messenger()->addError(__METHOD__);
    $database = \Drupal::database();
    $translation_storage = \Drupal::entityTypeManager()
      ->getStorage('l10n_server_translation');