getUntranslatedString example

/** * {@inheritdoc} */
  public function translate($string, array $args = [], array $options = []) {
    return new TranslatableMarkup($string$args$options$this);
  }

  /** * {@inheritdoc} */
  public function translateString(TranslatableMarkup $translated_string) {
    return $translated_string->getUntranslatedString();
  }

  /** * {@inheritdoc} */
  public function formatPlural($count$singular$plural, array $args = [], array $options = []) {
    return new PluralTranslatableMarkup($count$singular$plural$args$options$this);
  }

}
/** * {@inheritdoc} */
  public function translate($string, array $args = [], array $options = []) {
    return new TranslatableMarkup($string$args$options$this);
  }

  /** * {@inheritdoc} */
  public function translateString(TranslatableMarkup $translated_string) {
    return $this->doTranslate($translated_string->getUntranslatedString()$translated_string->getOptions());
  }

  /** * Translates a string to the current language or to a given language. * * @param string $string * A string containing the English text to translate. * @param array $options * An associative array of additional options, with the following elements: * - 'langcode': The language code to translate to a language other than * what is used to display the page. * - 'context': The context the source string belongs to. * * @return string * The translated string. */
continue;
      }
      if (is_array($item)) {
        // Only add this key if there was a translated value underneath.         $value = $this->processTranslatableData($name$active[$key]$item$langcode);
        if (!empty($value)) {
          $translated[$key] = $value;
        }
      }
      else {
        if (locale_is_translatable($langcode)) {
          $value = $this->translateString($name$langcode$item->getUntranslatedString()$item->getOption('context'));
        }
        else {
          $value = $item->getUntranslatedString();
        }
        if (!empty($value)) {
          $translated[$key] = $value;
        }
      }
    }
    return $translated;
  }

  
'langcode' => 'fr',
      'content_options[not_customized]' => FALSE,
      'content_options[customized]' => FALSE,
      'content_options[not_translated]' => TRUE,
    ], 'Export');

    // Ensure we have a translation file.     $this->assertSession()->pageTextContains('# French translation of Drupal');
    // Ensure no customized translations exist in the file.     $this->assertSession()->responseNotContains('msgstr "janvier"');
    // Ensure the untranslated strings exist in the file, and with right quotes.     $this->assertSession()->responseContains($this->getUntranslatedString());
  }

  /** * Tests exportation of translation template file. */
  public function testExportTranslationTemplateFile() {
    // Load an admin page with JavaScript so _drupal_add_library() fires at     // least once and _locale_parse_js_file() gets to run at least once so that     // the locales_source table gets populated with something.     $this->drupalGet('admin/config/regional/language');
    // Get the translation template file.
protected function getCollectionRoute(EntityTypeInterface $entity_type) {
    // If the entity type does not provide an admin permission, there is no way     // to control access, so we cannot provide a route in a sensible way.     if ($entity_type->hasLinkTemplate('collection') && $entity_type->hasListBuilderClass() && ($admin_permission = $entity_type->getAdminPermission())) {
      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $label = $entity_type->getCollectionLabel();

      $route = new Route($entity_type->getLinkTemplate('collection'));
      $route
        ->addDefaults([
          '_entity_list' => $entity_type->id(),
          '_title' => $label->getUntranslatedString(),
          '_title_arguments' => $label->getArguments(),
          '_title_context' => $label->getOption('context'),
        ])
        ->setRequirement('_permission', $admin_permission);

      return $route;
    }
  }

  /** * Gets the type of the ID key for a given entity type. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * An entity type. * * @return string|null * The type of the ID key for a given entity type, or NULL if the entity * type does not support fields. */

  public static function dayOfWeekName($date = NULL, $abbr = TRUE) {
    $date = $date ?? 'now';
    if (!$date instanceof DrupalDateTime) {
      $date = new DrupalDateTime($date);
    }
    if (!$date->hasErrors()) {
      $dow = self::dayOfWeek($date);
      $days = $abbr ? self::weekDaysAbbr() : self::weekDays();
      return $days[$dow]->getUntranslatedString();
    }
    return NULL;
  }

}

  public function testFormatInterval($interval$granularity$expected$langcode = NULL) {
    // Mocks a simple formatPlural implementation.     $this->stringTranslation->expects($this->any())
      ->method('translateString')
      ->willReturnCallback(function DTranslatableMarkup $arg) {
        return $arg->getUntranslatedString();
      });

    // Check if the granularity is specified.     if ($granularity) {
      $result = $this->dateFormatter->formatInterval($interval$granularity$langcode);
    }
    else {
      $result = $this->dateFormatter->formatInterval($interval);
    }

    $this->assertEquals(new TranslatableMarkup($expected[]['langcode' => $langcode]$this->stringTranslation)$result);
  }

  public function getStringTranslationStub() {
    $translation = $this->createMock('Drupal\Core\StringTranslation\TranslationInterface');
    $translation->expects($this->any())
      ->method('translate')
      ->willReturnCallback(function D$string, array $args = [], array $options = []) use ($translation) {
        return new TranslatableMarkup($string$args$options$translation);
      });
    $translation->expects($this->any())
      ->method('translateString')
      ->willReturnCallback(function DTranslatableMarkup $wrapper) {
        return $wrapper->getUntranslatedString();
      });
    $translation->expects($this->any())
      ->method('formatPlural')
      ->willReturnCallback(function D$count$singular$plural, array $args = [], array $options = []) use ($translation) {
        $wrapper = new PluralTranslatableMarkup($count$singular$plural$args$options$translation);
        return $wrapper;
      });
    return $translation;
  }

  /** * Sets up a container with a cache tags invalidator. * * @param \Drupal\Core\Cache\CacheTagsInvalidatorInterface $cache_tags_validator * The cache tags invalidator. * * @return \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit\Framework\MockObject\MockObject * The container with the cache tags invalidator service. */

  public function __construct($string, array $arguments = [], array $options = [], TranslationInterface $string_translation = NULL) {
    if (!is_string($string)) {
      $message = $string instanceof TranslatableMarkup ? '$string ("' . $string->getUntranslatedString() . '") must be a string.' : '$string ("' . (string) $string . '") must be a string.';
      throw new \InvalidArgumentException($message);
    }
    parent::__construct($string$arguments);
    $this->options = $options;
    $this->stringTranslation = $string_translation;
  }

  /** * Gets the untranslated string value stored in this translated string. * * @return string * The string stored in this wrapper. */
/** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->translation = $this->getObjectForTrait('\Drupal\Core\StringTranslation\StringTranslationTrait');
    $mock = $this->prophesize(TranslationInterface::class);
    $mock->translate(Argument::cetera())->shouldNotBeCalled();
    $mock->formatPlural(Argument::cetera())->shouldNotBeCalled();
    $mock->translateString(Argument::cetera())->will(function D$args) {
      return $args[0]->getUntranslatedString();
    });
    $this->translation->setStringTranslation($mock->reveal());
    $this->reflection = new \ReflectionClass(get_class($this->translation));
  }

  /** * @covers ::t */
  public function testT() {
    $method = $this->reflection->getMethod('t');

    
$namespaces = new \ArrayObject($paths);
    $discovery = new AnnotatedClassDiscovery('Entity', $namespaces, 'Drupal\Core\Entity\Annotation\EntityType');
    $definitions = $discovery->getDefinitions();

    foreach ($definitions as $definition) {
      /** @var \Drupal\Core\Entity\EntityType $definition */

      /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
      $label = $definition->getLabel();
      $collection_label = $definition->getCollectionLabel();

      $label_string = $label->getUntranslatedString();
      $collection_label_string = $collection_label->getUntranslatedString();

      // Keep the first word as it is for nouns that are all capital letters       // (like RDF, URL alias etc.) so we can't run strtolower() for the entire       // string. Special cases may need to be added to this test in the future       // if an acronym is in a different position in the label.       $first_word = strtok($label_string, " ");
      $remaining_string = strtolower(strstr($label_string, " "));
      $this->assertEquals($first_word . $remaining_string$label_string);

      $first_word = strtok($collection_label_string, " ");
      
if (!isset($config[$key])) {
        if (isset($reference_config[$key])) {
          $this->resetExistingTranslations($name$translatable[$key]$reference_config[$key]$langcode);
        }
        continue;
      }
      if (is_array($item)) {
        $reference_config_item = $reference_config[$key] ?? [];
        $this->processTranslatableData($name$config[$key]$item$langcode$reference_config_item);
      }
      else {
        $this->saveCustomizedTranslation($name$item->getUntranslatedString()$item->getOption('context')$config[$key]$langcode);
      }
    }
  }

  /** * Reset existing locale translations to their source values. * * Goes through $translatable to reset any existing translations to the source * string, so prior translations would not reappear in the configuration. * * @param string $name * The configuration name. * @param array|\Drupal\Core\StringTranslation\TranslatableMarkup $translatable * Either a possibly nested array with TranslatableMarkup objects at the * leaf items or a TranslatableMarkup object directly. * @param array|string $reference_config * Either a possibly nested array with strings at the leaf items or a string * directly. Only those $translatable items that are also present in * $reference_config will get translations reset. * @param string $langcode * The language code of the translation being processed. */
Home | Imprint | This part of the site doesn't use cookies.