Multilingual site with settings:
Language switcher module should present link to default language without language code.
Language switcher module still presents link to default language with language code.
Joomla 3.9.0
This bug was been already fixed as #4215 and #6874. Now it's back again, maybe a side effect of PR #22558 in J3.9.0?
Labels |
Added:
?
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-11-23 08:13:43 |
Closed_By | ⇒ | infograf768 |
Hmm, ok, it's not a bug but rel alternate hreflang are outputted without lang code in the head while mod_switcher does not strip lang code and crawlers acquire an URL that actually will get a redirect, that is not perfect for SEO.
I think it's possible to align mod_languages as an enhancement at least.
It is very easy to take off the url language code when hovering the switcher flag.
We should not forget that a cookie is set when a page is loaded with a specific content language (crawlers don't use the cookie).
Therefore, if we do that, and we are on the home page, the same page will just reload when clicking on the flag/name as it is the cookie set by the languagefilter which decides what to load when there is no url language code.
I have tested here by also setting a cookie in the module helper and it looks like working.
I had to add a new setLanguagecookie()
method as public static
as the one used in languagefilter is private.
Please test.
Here is the diff:
diff --git a/modules/mod_languages/helper.php b/modules/mod_languages/helper.php
index 3b34496..a5b177e 100644
--- a/modules/mod_languages/helper.php
+++ b/modules/mod_languages/helper.php
@@ -30,8 +30,13 @@
$user = JFactory::getUser();
$lang = JFactory::getLanguage();
- $languages = JLanguageHelper::getLanguages();
+ $languages = JLanguageHelper::getLanguages('lang_code');
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
+
+ $plugin = \JPluginHelper::getPlugin('system', 'languagefilter');
+ $params = new \JRegistry($plugin->params);
+ $remove_default_prefix = (boolean) $params->get('remove_default_prefix', 0);
+ $default_lang = \JComponentHelper::getParams('com_languages')->get('site', 'en-GB');
// Get menu home items
@@ -142,7 +147,52 @@
}
}
+
+ // Remove the sef from the default language if "Remove URL Language Code" is on
+ if ($remove_default_prefix && isset($languages[$default_lang]->link))
+ {
+ $languages[$default_lang]->link
+ = preg_replace('|/' . $languages[$default_lang]->sef . '/|', '/', $languages[$default_lang]->link, 1);
+
+ self::setLanguageCookie($default_lang);
+ }
}
return $languages;
}
+
+ /**
+ * Set the language cookie
+ *
+ * @param string $languageCode The language code for which we want to set the cookie
+ *
+ * @return void
+ *
+ * @since __DEPLOY_VERSION__
+ */
+ public static function setLanguageCookie($languageCode)
+ {
+ $app = JFactory::getApplication();
+ $plugin = \JPluginHelper::getPlugin('system', 'languagefilter');
+ $params = new \JRegistry($plugin->params);
+
+ // If is set to use language cookie for a year in plugin params, save the user language in a new cookie.
+ if ((int) $params->get('lang_cookie', 0) === 1)
+ {
+ // Create a cookie with one year lifetime.
+ $app->input->cookie->set(
+ JApplicationHelper::getHash('language'),
+ $languageCode,
+ time() + 365 * 86400,
+ $app->get('cookie_path', '/'),
+ $app->get('cookie_domain', ''),
+ $app->isHttpsForced(),
+ true
+ );
+ }
+ // If not, set the user language in the session (that is already saved in a cookie).
+ else
+ {
+ JFactory::getSession()->set('plg_system_languagefilter.language', $languageCode);
+ }
+ }
}
Full file to use as replacement:
@Bakual @mbabker @bembelimen
Any issue I would not have seen?
Status | Closed | ⇒ | New |
Closed_Date | 2018-11-23 08:13:43 | ⇒ | |
Closed_By | infograf768 | ⇒ |
I am reopening as we may have a solution.
hi @infograf768: I made some tests and seems work, though I get a notice on
Notice: Undefined property: stdClass::$link in \modules\mod_languages\helper.php on line 154
when displaying an article with language = "*", i.e. when there is no associated article
Try modifying line 151 to
if ($remove_default_prefix && isset($languages[$default_lang]->link))
yes, the isset has to be with the ->link, notice is infact gone
Title |
|
Hi @infograf768, and are you going to propose the PR? Thanks
will do. Too late anyway for 3.9.1
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-11-27 09:46:59 |
Closed_By | ⇒ | infograf768 |
Not solved
The language switcher has always been presenting the link with the url language code when hovering on the flag or name for the language.
The magic is done when the page loads. The browser displays without the url language code and when there are associations the alternate is also correct.
This is perfectly fine and expected.
Closing as not a bug.