Commit 78f9db42 authored by Tim Bozeman's avatar Tim Bozeman Committed by Tim Bozeman
Browse files

Issue #3325340 by Tim Bozeman: Translation improvements

parent 282f0557
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ use Drupal\Core\Config\ConfigRenameEvent;
use Drupal\Core\Language\LanguageDefault;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Config\ConfigCollectionInfo;
use Drupal\location_variant\LocationHandler;
use Drupal\Core\Config\ConfigFactoryOverrideBase;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Config\ConfigFactoryOverrideInterface;
@@ -78,6 +79,10 @@ class ConfigLocationVariantFactoryOverride extends ConfigFactoryOverrideBase imp
   */
  public function getStorage($langcode) {
    if (!isset($this->storages[$langcode])) {
      if (\str_contains($langcode, '-')) {
        // Config doesn't use localization so just pass the langcode.
        $langcode = LocationHandler::parseCode($langcode)['langcode'];
      }
      $this->storages[$langcode] = $this->baseStorage->createCollection($this->createConfigCollectionName($langcode));
    }
    return $this->storages[$langcode];
+0 −6
Original line number Diff line number Diff line
@@ -48,12 +48,6 @@ class LanguageRequestSubscriber implements EventSubscriberInterface {
  private function setLanguageOverrides() {
    if ($this->languageManager instanceof VariantsLanguageManager) {
      $language = $this->languageManager->getCurrentLanguage();
      if ($language && \str_contains($language->getId(), '-')) {
        // Config doesn't use localization so just pass the langcode.
        [$langcode, ] = \explode('-', $language->getId());
        $language = $this->languageManager->getLanguage($langcode);

      }
      $this->languageManager->setConfigOverrideLanguage($language);
    }
  }
+1 −1
Original line number Diff line number Diff line
@@ -68,7 +68,7 @@ class LocationController extends ControllerBase {
      $view_links = [];
      $delta = 0;
      foreach ($supported_langcodes as $langcode) {
        $variation_code = $this->entityVariationHandler->generateVariationCode($langcode, $entity, $variant);
        $variation_code = $this->entityVariationHandler->generateVariationCode(LocationHandler::parseCode($langcode)['langcode'], $entity, $variant);
        $localized_translation = $variant->getVariation($variation_code);
        if ($localized_translation) {
          // View entity links.
+5 −7
Original line number Diff line number Diff line
@@ -65,7 +65,8 @@ class EntityVariationHandler extends EntityHandlerBase {
   *   The variant.
   */
  public function getVariantByVariationCode(EntityInterface $entity, string $variation_code): ?Variant {
    [, $variant_delta] = \explode('-', $variation_code);
    $variant_delta = LocationHandler::parseCode($variation_code)['location'];

    $variants = $entity->get('variants')->referencedEntities();
    return $variants[$variant_delta] ?? NULL;
  }
@@ -103,7 +104,7 @@ class EntityVariationHandler extends EntityHandlerBase {
      // Build a list of translations or possible translations of this localized variation.
      $supported_langcodes = $variant->getSupportedLangcodes();
      foreach ($supported_langcodes as $langcode) {
        $variation_code = $this->generateVariationCode($langcode, $entity, $variant);
        $variation_code = $this->generateVariationCode(LocationHandler::parseCode($langcode)['langcode'], $entity, $variant);
        $localized_translation = $variant->getVariation($variation_code);
        $variation_info = [
          'variation' => $localized_translation ? $localized_translation : $localized_version,
@@ -480,7 +481,7 @@ class EntityVariationHandler extends EntityHandlerBase {
            /** @var LocationNegotiator $negotiator */
            $negotiator = \Drupal::service('location_variant.negotiator');
            $current_language = $negotiator->getCurrentLanguage($this->request);
            [, $location_id] = explode('-', $current_language->getId());
            $location_id = LocationHandler::parseCode($current_language->getId())['location'];
            $location = $this->locationHandler->getLocation($location_id);
            $locations[$location->getId()] = $location;
          }
@@ -566,7 +567,7 @@ class EntityVariationHandler extends EntityHandlerBase {
    $variation_code = $entity->language()->getId();
    if (\str_contains($variation_code, '-')) {
      $variant = $this->getVariantByVariationCode($entity, $variation_code);
      [$langcode,] = \explode('-', $variation_code);
      $langcode = LocationHandler::parseCode($variation_code)['langcode'];
      $locations = $variant->getLocations();
      $langcode = $this->locationHandler->generateLangcode(\array_key_first($locations), $langcode);
      $url->setOption('language', \Drupal::languageManager()->getLanguage($langcode));
@@ -593,9 +594,6 @@ class EntityVariationHandler extends EntityHandlerBase {
   *   The variation code.
   */
  public function generateVariationCode(string $langcode, EntityInterface $entity, Variant $variant = NULL): string {
    if (\str_contains($langcode, '-')) {
      $langcode = \explode('-', $langcode)[0];
    }
    $variants = $entity->get('variants')->getValue();
    // Find this variants delta.
    if ($variant) {
+35 −1
Original line number Diff line number Diff line
@@ -238,7 +238,7 @@ class LocationHandler {
   *
   * The translation system is used for both localizations and translations. The
   * langcodes are like unique IDs. These are the langcodes that are used as
   * URL prefixes.
   * URL prefixes. e.g. en-US, de-DE, ko-KR.
   *
   * @param string $location
   *   The supported locations within the Location Reference.
@@ -252,4 +252,38 @@ class LocationHandler {
    return "$langcode-$location";
  }

  /**
   * Parse Langcode.
   *
   * @param string $langcode
   *   The langcode as it is used in URL prefixes.
   *
   * @return array
   *   The langcode parsed into the langcode and either the location or variant
   *   delta.
   */
  public static function parseCode(string $langcode): array {
    $pieces = \explode('-', $langcode);
    $location_or_delta = \array_pop($pieces);
    $info['type'] = \is_numeric($location_or_delta) ? 'variation_code' : 'langcode';
    $info['location'] = $location_or_delta;
    $info['langcode'] = \implode('-', $pieces);

    return $info;
  }

  /**
   * Is Variation code.
   *
   * @param string $code
   *   The variation code or langcode to check.
   *
   * @return bool
   *   Whether the code is a variation code.
   */
  public static function isVariationCode(string $code): bool {
    $code_info = self::parseCode($code);
    return $code_info['type'] === 'variation_code';
  }

}
Loading