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

Created on 25 October 2014, over 9 years ago
Updated 11 October 2023, 9 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 about 10 hours 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.

  • The Needs Review Queue Bot β†’ tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

    Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

    Consult the Drupal Contributor Guide β†’ to find step-by-step guides for working with issues.

  • πŸ‡ΊπŸ‡¦Ukraine vlad.dancer Kyiv

    > it would be good if we keep the language key to provide a language object, while we can use langcode for a language code string

    I agree too with @tstoeckler and @pfrenssen to have a new langcode property to avoid lots of refactorinf and intoducing BC.

Production build 0.69.0 2024