setLangcode example


  public static function filesToArray($langcode, array $files) {
    $writer = new PoMemoryWriter();
    $writer->setLangcode($langcode);
    foreach ($files as $file) {
      $reader = new PoStreamReader();
      $reader->setURI($file->uri);
      $reader->setLangcode($langcode);
      $reader->open();
      $writer->writeItems($reader, -1);
    }
    return $writer->getData();
  }

}

        $field = \Drupal::service('plugin.manager.field.field_type')->createFieldItemList($this->getTranslation($langcode)$name$value);
        if ($default) {
          // $this->defaultLangcode might not be set if we are initializing the           // default language code cache, in which case there is no valid           // langcode to assign.           $field_langcode = $this->defaultLangcode ?? LanguageInterface::LANGCODE_NOT_SPECIFIED;
        }
        else {
          $field_langcode = $langcode;
        }
        $field->setLangcode($field_langcode);
        $this->fields[$name][$langcode] = $field;
      }
    }
    return $this->fields[$name][$langcode];
  }

  /** * {@inheritdoc} */
  public function set($name$value$notify = TRUE) {
    // Assign the value on the child and overrule notify such that we get
// If template is required, language code is not given.     if ($form_state->getValue('langcode') != LanguageInterface::LANGCODE_SYSTEM) {
      $language = $this->languageManager->getLanguage($form_state->getValue('langcode'));
    }
    else {
      $language = NULL;
    }
    $content_options = $form_state->getValue('content_options', []);
    $reader = new PoDatabaseReader();
    $language_name = '';
    if ($language != NULL) {
      $reader->setLangcode($language->getId());
      $reader->setOptions($content_options);
      $languages = $this->languageManager->getLanguages();
      $language_name = isset($languages[$language->getId()]) ? $languages[$language->getId()]->getName() : '';
      $filename = $language->getId() . '.po';
    }
    else {
      // Template required.       $filename = 'drupal.pot';
    }

    $item = $reader->readItem();
    

  public static function fileToDatabase($file$options) {
    // Add the default values to the options array.     $options += [
      'overwrite_options' => [],
      'customized' => LOCALE_NOT_CUSTOMIZED,
      'items' => -1,
      'seek' => 0,
    ];
    // Instantiate and initialize the stream reader for this file.     $reader = new PoStreamReader();
    $reader->setLangcode($file->langcode);
    $reader->setURI($file->uri);

    try {
      $reader->open();
    }
    catch (\Exception $exception) {
      throw $exception;
    }

    $header = $reader->getHeader();
    if (!$header) {
      
'#type' => 'table',
      '#header' => [$this->t('Language')$this->t('Operations')],
    ];
    foreach ($languages as $language) {
      $langcode = $language->getId();

      // This is needed because       // ConfigMapperInterface::getAddRouteParameters(), for example,       // needs to return the correct language code for each table row.       $fake_route_match = RouteMatch::createFromRequest($fake_request);
      $mapper->populateFromRouteMatch($fake_route_match);
      $mapper->setLangcode($langcode);

      // Prepare the language name and the operations depending on whether this       // is the original language or not.       if ($langcode == $original_langcode) {
        $language_name = '<strong>' . $this->t('@language (original)', ['@language' => $language->getName()]) . '</strong>';

        // Check access for the path/route for editing, so we can decide to         // include a link to edit or not.         $edit_access = $this->accessManager->checkNamedRoute($mapper->getBaseRouteName()$route_match->getRawParameters()->all()$this->account);

        // Build list of operations.
// Sort plural variants by their form index.       ksort($value['msgstr']);
      $plural = TRUE;
    }

    $item = new PoItem();
    $item->setContext($value['msgctxt'] ?? '');
    $item->setSource($value['msgid']);
    $item->setTranslation($value['msgstr']);
    $item->setPlural($plural);
    $item->setComment($comments);
    $item->setLangcode($this->langcode);

    $this->lastItem = $item;

    $this->context = 'COMMENT';
  }

  /** * Parses a string in quotes. * * @param $string * A string specified with enclosing quotes. * * @return bool|string * The string parsed from inside the quotes. False when the syntax is * invalid. */
Home | Imprint | This part of the site doesn't use cookies.