getLangcode example

'customized' => FALSE,
      'not_customized' => FALSE,
      'not_translated' => FALSE,
    ];
    $this->options = $options;
  }

  /** * {@inheritdoc} */
  public function getHeader() {
    return new PoHeader($this->getLangcode());
  }

  /** * Implements Drupal\Component\Gettext\PoMetadataInterface::setHeader(). * * @throws \Exception * Always, because you cannot set the PO header of a reader. */
  public function setHeader(PoHeader $header) {
    throw new \Exception('You cannot set the PO header in a reader.');
  }

  

  protected function negotiateLanguage($type$method_id) {
    $langcode = NULL;
    $method = $this->negotiatorManager->getDefinition($method_id);

    if (!isset($method['types']) || in_array($type$method['types'])) {
      $langcode = $this->getNegotiationMethodInstance($method_id)->getLangcode($this->requestStack->getCurrentRequest());
    }

    $languages = $this->languageManager->getLanguages();
    return $languages[$langcode] ?? NULL;
  }

  /** * {@inheritdoc} */
  public function getNegotiationMethods($type = NULL) {
    $definitions = $this->negotiatorManager->getDefinitions();
    
'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
          'prefixes' => $prefixes,
        ],
      ],
    ]);

    $request = Request::create('/' . $prefix . '/foo', 'GET');
    $method = new LanguageNegotiationUrl();
    $method->setLanguageManager($this->languageManager);
    $method->setConfig($config);
    $method->setCurrentUser($this->user);
    $this->assertEquals($expected_langcode$method->getLangcode($request));

    $cacheability = new BubbleableMetadata();
    $options = [];
    $method->processOutbound('foo', $options$request$cacheability);
    $expected_cacheability = new BubbleableMetadata();
    if ($expected_langcode) {
      $this->assertSame($prefix . '/', $options['prefix']);
      $expected_cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL]);
    }
    else {
      $this->assertFalse(isset($options['prefix']));
    }
// Pass on the trim length to the #pre_render callback via a property.       $element['#text_summary_trim_length'] = $this->getSetting('trim_length');
    };

    // The ProcessedText element already handles cache context & tag bubbling.     // @see \Drupal\filter\Element\ProcessedText::preRenderText()     foreach ($items as $delta => $item) {
      $elements[$delta] = [
        '#type' => 'processed_text',
        '#text' => NULL,
        '#format' => $item->format,
        '#langcode' => $item->getLangcode(),
      ];

      if ($this->getPluginId() == 'text_summary_or_trimmed' && !empty($item->summary)) {
        $elements[$delta]['#text'] = $item->summary;
      }
      else {
        $elements[$delta]['#text'] = $item->value;
        $render_as_summary($elements[$delta]);
      }
    }

    

  protected function mustResave() {
    return $this->getValue()['value'] == 'resave';
  }

  /** * {@inheritdoc} */
  public function delete() {
    parent::delete();
    $deleted_languages = \Drupal::state()->get('entity_test.delete.' . $this->getFieldDefinition()->getName()[]);
    $deleted_languages[] = $this->getLangcode();
    \Drupal::state()->set('entity_test.delete.' . $this->getFieldDefinition()->getName()$deleted_languages);
  }

}

  public function processOutbound($path, &$options = [], Request $request = NULL, BubbleableMetadata $bubbleable_metadata = NULL) {
    // If appropriate, process outbound to add a query parameter to the URL and     // remove the language option, so that URL negotiator does not rewrite the     // URL.
    // First, check if processing conditions are met.     if (!($request && !empty($options['route']) && $this->hasLowerLanguageNegotiationWeight() && $this->meetsContentEntityRoutesCondition($options['route']$request))) {
      return $path;
    }

    if (isset($options['language']) || $langcode = $this->getLangcode($request)) {
      // If the language option is set, unset it, so that the URL language       // negotiator does not rewrite the URL.       if (isset($options['language'])) {
        $langcode = $options['language']->getId();
        unset($options['language']);
      }

      if (!isset($options['query'][static::QUERY_PARAMETER])) {
        $options['query'][static::QUERY_PARAMETER] = $langcode;
      }

      
$langcode_key => LanguageInterface::LANGCODE_NOT_SPECIFIED,
      ]);

    $this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $entity->language()->getId()new FormattableMarkup('%entity_type: Entity language not specified.', ['%entity_type' => $entity_type]));
    $this->assertEmpty($entity->getTranslationLanguages(FALSE)new FormattableMarkup('%entity_type: No translations are available', ['%entity_type' => $entity_type]));

    // Set the value in default language.     $entity->set($this->fieldName, [0 => ['value' => 'default value']]);
    // Get the value.     $field = $entity->getTranslation(LanguageInterface::LANGCODE_DEFAULT)->get($this->fieldName);
    $this->assertEquals('default value', $field->value, new FormattableMarkup('%entity_type: Untranslated value retrieved.', ['%entity_type' => $entity_type]));
    $this->assertEquals(LanguageInterface::LANGCODE_NOT_SPECIFIED, $field->getLangcode()new FormattableMarkup('%entity_type: Field object has the expected langcode.', ['%entity_type' => $entity_type]));

    // Try to get add a translation to language neutral entity.     try {
      $entity->addTranslation($this->langcodes[1]);
      $this->fail('Adding a translation to a language-neutral entity results in an error.');
    }
    catch (\InvalidArgumentException $e) {
      // Expected exception; just continue testing.     }

    // Now, make the entity language-specific by assigning a language and test

  public function access(RouteMatchInterface $route_match, AccountInterface $account$langcode = NULL) {
    $mapper = $this->getMapperFromRouteMatch($route_match);

    try {
      $source_langcode = $mapper->getLangcode();
      $source_language = $this->languageManager->getLanguage($source_langcode);

      $target_language = $this->languageManager->getLanguage($langcode);

      return $this->doCheckAccess($account$mapper$source_language$target_language);
    }
    catch (ConfigMapperLanguageException $exception) {
      return AccessResult::forbidden();
    }
  }

  
$language = $this->languageManager->getLanguage($langcode);
    if (!$language) {
      throw new NotFoundHttpException();
    }

    $this->mapper = $mapper;
    $this->language = $language;

    // ConfigTranslationFormAccess will not grant access if this raises an     // exception, so we can call this without a try-catch block here.     $langcode = $this->mapper->getLangcode();

    $this->sourceLanguage = $this->languageManager->getLanguage($langcode);

    // Get base language configuration to display in the form before setting the     // language to use for the form. This avoids repetitively settings and     // resetting the language to get original values later.     $this->baseConfigData = $this->mapper->getConfigData();

    // Set the translation target language on the configuration factory.     $original_language = $this->languageManager->getConfigOverrideLanguage();
    $this->languageManager->setConfigOverrideLanguage($this->language);

    
use ComputedItemListTrait;

  /** * {@inheritdoc} */
  protected function computeValue() {
    // Default the langcode to the current language if this is a new entity or     // there is no alias for an existent entity.     // @todo Set the langcode to not specified for untranslatable fields     // in https://www.drupal.org/node/2689459.     $value = ['langcode' => $this->getLangcode()];

    $entity = $this->getEntity();
    if (!$entity->isNew()) {
      /** @var \Drupal\path_alias\AliasRepositoryInterface $path_alias_repository */
      $path_alias_repository = \Drupal::service('path_alias.repository');

      if ($path_alias = $path_alias_repository->lookupBySystemPath('/' . $entity->toUrl()->getInternalPath()$this->getLangcode())) {
        $value = [
          'alias' => $path_alias['alias'],
          'pid' => $path_alias['id'],
          'langcode' => $path_alias['langcode'],
        ];
$mapper->populateFromRouteMatch($route_match);

    $page = [];
    $page['#title'] = $this->t('Translations for %label', ['%label' => $mapper->getTitle()]);

    $languages = $this->languageManager->getLanguages();
    if (count($languages) == 1) {
      $this->messenger()->addWarning($this->t('In order to translate configuration, the website must have at least two <a href=":url">languages</a>.', [':url' => Url::fromRoute('entity.configurable_language.collection')->toString()]));
    }

    try {
      $original_langcode = $mapper->getLangcode();
      $operations_access = TRUE;
    }
    catch (ConfigMapperLanguageException $exception) {
      $items = [];
      foreach ($mapper->getConfigNames() as $config_name) {
        $langcode = $mapper->getLangcodeFromConfig($config_name);
        $items[] = $this->t('@name: @langcode', [
          '@name' => $config_name,
          '@langcode'  => $langcode,
        ]);
      }
      

  public function getLangcodeByRelationship(ResultRow $row, string $relationship): string {
    // This method needs to be overridden if the relationship is needed in the     // implementation of getLangcode().     return $this->getLangcode($row);
  }

  /** * {@inheritdoc} */
  public function query(QueryPluginBase $query$relationship = NULL) {
  }

  /** * {@inheritdoc} */
  
/** * {@inheritdoc} */
  public function getEntity() {
    return $this->getParent()->getEntity();
  }

  /** * {@inheritdoc} */
  public function getLangcode() {
    return $this->getParent()->getLangcode();
  }

  /** * {@inheritdoc} */
  public function getFieldDefinition() {
    return $this->definition->getFieldDefinition();
  }

  /** * Returns the array of field settings. * * @return array * The array of settings. */

  }

  /** * Reacts to the populating of a configuration mapper. * * @param \Drupal\config_translation\Event\ConfigMapperPopulateEvent $event * The configuration mapper event. */
  public function addConfigNames(ConfigMapperPopulateEvent $event) {
    $mapper = $event->getMapper();
    if ($mapper->getBaseRouteName() === 'system.site_information_settings' && $mapper->getLangcode() === 'en') {
      $mapper->addConfigName('config_translation_test.content');
    }
  }

}
/** * Updates the locale strings when a configuration override is saved/deleted. * * @param \Drupal\language\Config\LanguageConfigOverrideCrudEvent $event * The language configuration event. */
  public function onOverrideChange(LanguageConfigOverrideCrudEvent $event) {
    // Only attempt to feed back configuration override changes to locale if     // the update itself was not initiated by locale data changes.     if (!InstallerKernel::installationAttempted() && !$this->localeConfigManager->isUpdatingTranslationsFromLocale()) {
      $translation_config = $event->getLanguageConfigOverride();
      $langcode = $translation_config->getLangcode();
      $reference_config = $this->configFactory->getEditable($translation_config->getName())->get();
      $this->updateLocaleStorage($translation_config$langcode$reference_config);
    }
  }

  /** * Update locale storage based on configuration translations. * * @param \Drupal\Core\Config\StorableConfigBase $config * Active configuration or configuration translation override. * @param string $langcode * The language code of $config. * @param array $reference_config * (Optional) Reference configuration to check against if $config was an * override. This allows us to update locale keys for data not in the * override but still in the active configuration. */
Home | Imprint | This part of the site doesn't use cookies.