Make it possible to provide 'langcode' in $options for URL generation instead of Language object

Created on 25 October 2014, about 10 years ago
Updated 9 September 2024, 4 months ago

Problem/Motivation

A lot of code uses just langcodes everywhere.

The url generator code still requires the full language object. This issue exists to reduce the code complexity for developers and themers when building Drupal\Core\Url or Drupal\Core\Render\Element\Link objects.

Proposed resolution

Allow to specify 'language' as a langcode string such as "de" or "en". This langcode will be used to load the proper Language object internally.

For example, the only way to link to a language that is not current LanguageInterface::TYPE_URL used to be before this issue:

$language_manager = \Drupal::service('language_manager');
$url = Url::fromRoute( 'user.register', [], ['language' => $language_manager->getLanguage('it')]);
$link = Link::createFromRoute($text, 'user.register', [], ['language' => $language_manager->getLanguage('de')]);

This issue proposes to add this additional way:

$url = Url::fromRoute( 'user.register', [], ['language' => 'it']);
$link = Link::createFromRoute($text, 'user.register', [], ['language' => 'de']);

Remaining tasks

Review

User interface changes

None.

API changes

Besides setting $options = ['language' => where_do_we_get_language_object_from()] there is now
a simple $options = ['language' => 'en'] when you generate a URL.

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component
Request processingΒ  β†’

Last updated 4 days ago

No maintainer
Created by

πŸ‡©πŸ‡ͺGermany dawehner

Live updates comments and jobs are added and updated live.
  • D8MI

    (Drupal 8 Multilingual Initiative) is the tag used by the multilingual initiative to mark core issues (and some contributed module issues). For versions other than Drupal 8, use the i18n (Internationalization) tag on issues which involve or affect multilingual / multinational support. That is preferred over Translation.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024