Skip to main content
addendum, improve formatting
Source Link
marco-s
  • 473
  • 1
  • 7
  • 16

Solution

public function register(ContainerBuilder $container) {
  parent::register($container);

  // Define the default language.
  $language = Language::$defaultValues;
  $language['id'] = 'de';
  $language['name'] = 'German';
  $container->setParameter('language.default_values', $language);

  // Register PathProcessorLanguage.
  $container->register('path_processor_language', PathProcessorLanguage::class)
    ->addTag('path_processor_outbound', ['priority' => 100])
    ->addArgument(new Reference('config.factory'))
    ->addArgument(new Reference('language_manager'))
    ->addArgument(new Reference('language_negotiator'))
    ->addArgument(new Reference('current_user'))
    ->addArgument(new Reference('language.config_subscriber'));
}

protected function setUp() {
  parent::setUp();

  // Add additional languages.
  ConfigurableLanguage::createFromLangcode('fr')->save();
  ConfigurableLanguage::createFromLangcode('it')->save();

  // Define language settings.
  $this->config('language.types')->setData([
    'configurable' => ['language_interface'],
    'negotiation' => [
      'language_interface' => [
        'enabled' => [
          LanguageNegotiationUrl::METHOD_ID => 0,
        ],
      ],
    ],
  ])->save();

  $languageKeys = array_keys($this->container->get('language_manager')->getLanguages());
  $this->config('language.negotiation')->setData([
    'url' => [
      'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
      'prefixes' => array_combine($languageKeys, $languageKeys),
    ],
  ])->save();
}

Addition

If you want to use/test path alias configs too, just register the AliasPathProcessor class...

public function register(ContainerBuilder $container) {
  parent::register($container);

  // ...

  $container->register('path_alias.path_processor', AliasPathProcessor::class)
      ->addTag('path_processor_outbound', ['priority' => 300])
      ->addArgument(new Reference('path_alias.manager'));

  //...
}

... and add the path alias entry for the specific language.

protected function setUp() {
  parent::setUp();

  // ...

  $pathAlias = PathAlias::create([
      'path' => '/foo',
      'alias' => '/bar',
      'langcode' => 'it',
    ]);
    $pathAlias->save();

  // ...
}
public function register(ContainerBuilder $container) {
  parent::register($container);

  // Define the default language.
  $language = Language::$defaultValues;
  $language['id'] = 'de';
  $language['name'] = 'German';
  $container->setParameter('language.default_values', $language);

  // Register PathProcessorLanguage.
  $container->register('path_processor_language', PathProcessorLanguage::class)
    ->addTag('path_processor_outbound', ['priority' => 100])
    ->addArgument(new Reference('config.factory'))
    ->addArgument(new Reference('language_manager'))
    ->addArgument(new Reference('language_negotiator'))
    ->addArgument(new Reference('current_user'))
    ->addArgument(new Reference('language.config_subscriber'));
}

protected function setUp() {
  parent::setUp();

  // Add additional languages.
  ConfigurableLanguage::createFromLangcode('fr')->save();
  ConfigurableLanguage::createFromLangcode('it')->save();

  // Define language settings.
  $this->config('language.types')->setData([
    'configurable' => ['language_interface'],
    'negotiation' => [
      'language_interface' => [
        'enabled' => [
          LanguageNegotiationUrl::METHOD_ID => 0,
        ],
      ],
    ],
  ])->save();

  $languageKeys = array_keys($this->container->get('language_manager')->getLanguages());
  $this->config('language.negotiation')->setData([
    'url' => [
      'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
      'prefixes' => array_combine($languageKeys, $languageKeys),
    ],
  ])->save();
}

Solution

public function register(ContainerBuilder $container) {
  parent::register($container);

  // Define the default language.
  $language = Language::$defaultValues;
  $language['id'] = 'de';
  $language['name'] = 'German';
  $container->setParameter('language.default_values', $language);

  // Register PathProcessorLanguage.
  $container->register('path_processor_language', PathProcessorLanguage::class)
    ->addTag('path_processor_outbound', ['priority' => 100])
    ->addArgument(new Reference('config.factory'))
    ->addArgument(new Reference('language_manager'))
    ->addArgument(new Reference('language_negotiator'))
    ->addArgument(new Reference('current_user'))
    ->addArgument(new Reference('language.config_subscriber'));
}

protected function setUp() {
  parent::setUp();

  // Add additional languages.
  ConfigurableLanguage::createFromLangcode('fr')->save();
  ConfigurableLanguage::createFromLangcode('it')->save();

  // Define language settings.
  $this->config('language.types')->setData([
    'configurable' => ['language_interface'],
    'negotiation' => [
      'language_interface' => [
        'enabled' => [
          LanguageNegotiationUrl::METHOD_ID => 0,
        ],
      ],
    ],
  ])->save();

  $languageKeys = array_keys($this->container->get('language_manager')->getLanguages());
  $this->config('language.negotiation')->setData([
    'url' => [
      'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
      'prefixes' => array_combine($languageKeys, $languageKeys),
    ],
  ])->save();
}

Addition

If you want to use/test path alias configs too, just register the AliasPathProcessor class...

public function register(ContainerBuilder $container) {
  parent::register($container);

  // ...

  $container->register('path_alias.path_processor', AliasPathProcessor::class)
      ->addTag('path_processor_outbound', ['priority' => 300])
      ->addArgument(new Reference('path_alias.manager'));

  //...
}

... and add the path alias entry for the specific language.

protected function setUp() {
  parent::setUp();

  // ...

  $pathAlias = PathAlias::create([
      'path' => '/foo',
      'alias' => '/bar',
      'langcode' => 'it',
    ]);
    $pathAlias->save();

  // ...
}
Source Link
marco-s
  • 473
  • 1
  • 7
  • 16

I could solve it by register the PathProcessorLanguage class and add the language.types and language.negotiation config.

public function register(ContainerBuilder $container) {
  parent::register($container);

  // Define the default language.
  $language = Language::$defaultValues;
  $language['id'] = 'de';
  $language['name'] = 'German';
  $container->setParameter('language.default_values', $language);

  // Register PathProcessorLanguage.
  $container->register('path_processor_language', PathProcessorLanguage::class)
    ->addTag('path_processor_outbound', ['priority' => 100])
    ->addArgument(new Reference('config.factory'))
    ->addArgument(new Reference('language_manager'))
    ->addArgument(new Reference('language_negotiator'))
    ->addArgument(new Reference('current_user'))
    ->addArgument(new Reference('language.config_subscriber'));
}

protected function setUp() {
  parent::setUp();

  // Add additional languages.
  ConfigurableLanguage::createFromLangcode('fr')->save();
  ConfigurableLanguage::createFromLangcode('it')->save();

  // Define language settings.
  $this->config('language.types')->setData([
    'configurable' => ['language_interface'],
    'negotiation' => [
      'language_interface' => [
        'enabled' => [
          LanguageNegotiationUrl::METHOD_ID => 0,
        ],
      ],
    ],
  ])->save();

  $languageKeys = array_keys($this->container->get('language_manager')->getLanguages());
  $this->config('language.negotiation')->setData([
    'url' => [
      'source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX,
      'prefixes' => array_combine($languageKeys, $languageKeys),
    ],
  ])->save();
}