What is the recommended way to generate non-route URLs that include the language prefix?
Context: In CiviCRM, an open source third-party project that integrates with Drupal, we have a function that generates internal URLs (a wrapper over the CMS-specific function).
Since not all URLs referenced have a proper route, so far fromUri has given the best results (rather than fromRoute).
However, the URLs generated by fromUri do not seem to include the language prefix for multi-lingual configurations when using
base as the prefix. The language prefix is included with
internal, but if I understand correctly,
internal should only be used for user-specified links, such as a link field.
Example on a site that has English and French, with French being the default language:
print \Drupal\Core\Url::fromUri("base:/admin")->toString() . "\n"; print \Drupal\Core\Url::fromUri("internal:/admin")->toString() . "\n";
$ drush scr test.php /civicrm/dashboard?reset=1 /fr/civicrm/dashboard?reset=1
I also tried passing in the
$language object, but the behaviour is the same:
$language = \Drupal::languageManager()->getCurrentLanguage(); print \Drupal\Core\Url::fromUri("base:/admin", [ 'language' => $language, ])->toString() . "\n";
Related question: Generating arbitrary internal URLs