Published on

Translating the home page segment title with the Easy Breadcrumb Drupal module

Drupal

The Easy Breadcrumb module is one of Drupal's most popular breadcrumbs building module.

When using it on a multilingual website, I stumbled upon an issue where I wasn't able to translate the home page crumb title segment (typically Home) in other languages. An issue exists that was marked as Fixed but it doesn't seem to work in my case when using Drupal 10.3.1.

To solve this issue, I created a patch that adds string translation to this string via the $this->t() function where needed thus allowing the home page segment to be translated from the User interface translation page in Drupal's admin.

diff --git a/src/EasyBreadcrumbBuilder.php b/src/EasyBreadcrumbBuilder.php
index 7790b32..15bedc7 100644
--- a/src/EasyBreadcrumbBuilder.php
+++ b/src/EasyBreadcrumbBuilder.php
@@ -339,7 +339,7 @@ class EasyBreadcrumbBuilder implements BreadcrumbBuilderInterface {
         || (!$is_regex && $internal_path == $custom_path)
       ) {
         if ($this->config->get(EasyBreadcrumbConstants::INCLUDE_HOME_SEGMENT)) {
-          $links[] = Link::createFromRoute($this->config->get(EasyBreadcrumbConstants::HOME_SEGMENT_TITLE), '<front>');
+          $links[] = Link::createFromRoute($this->t($this->config->get(EasyBreadcrumbConstants::HOME_SEGMENT_TITLE)), '<front>');
         }

         if ($is_regex && count($regex_group_matches) > 1) {
@@ -680,7 +680,7 @@ class EasyBreadcrumbBuilder implements BreadcrumbBuilderInterface {
       }

       if (!$this->config->get(EasyBreadcrumbConstants::USE_SITE_TITLE)) {
-        $links[] = Link::createFromRoute($this->normalizeText($this->config->get(EasyBreadcrumbConstants::HOME_SEGMENT_TITLE)), $home_route_name);
+        $links[] = Link::createFromRoute($this->normalizeText($this->t($this->config->get(EasyBreadcrumbConstants::HOME_SEGMENT_TITLE))), $home_route_name);
       }
       else {
         $links[] = Link::createFromRoute($this->siteConfig->get('name'), $home_route_name);