?
avatar Giuse69
Giuse69
22 Nov 2018

Steps to reproduce the issue

Multilingual site with settings:

  • Use URL rewriting = Yes
  • Remove url language code set to Yes
    Browsing a page not in the default language but in another language.

Expected result

Language switcher module should present link to default language without language code.

Actual result

Language switcher module still presents link to default language with language code.

System information (as much as possible)

Joomla 3.9.0

Additional comments

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?

avatar Giuse69 Giuse69 - open - 22 Nov 2018
avatar joomla-cms-bot joomla-cms-bot - change - 22 Nov 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 22 Nov 2018
avatar infograf768 infograf768 - change - 23 Nov 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-11-23 08:13:43
Closed_By infograf768
avatar infograf768
infograf768 - comment - 23 Nov 2018

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.

<link href="http://localhost:8888/installmulti/trunkgitnew/fr/" rel="alternate" hreflang="fr-FR" />
<link href="http://localhost:8888/installmulti/trunkgitnew/" rel="alternate" hreflang="en-GB" />
<link href="http://localhost:8888/installmulti/trunkgitnew/it/" rel="alternate" hreflang="it-IT" />

This is perfectly fine and expected.

Closing as not a bug.

avatar infograf768 infograf768 - close - 23 Nov 2018
avatar Giuse69
Giuse69 - comment - 23 Nov 2018

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.

avatar infograf768
infograf768 - comment - 24 Nov 2018

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:

helper.php.zip

@Bakual @mbabker @bembelimen
Any issue I would not have seen?

avatar infograf768 infograf768 - change - 24 Nov 2018
Status Closed New
Closed_Date 2018-11-23 08:13:43
Closed_By infograf768
avatar infograf768 infograf768 - reopen - 24 Nov 2018
avatar infograf768
infograf768 - comment - 24 Nov 2018

I am reopening as we may have a solution.

avatar infograf768
infograf768 - comment - 25 Nov 2018

@Giuse69
I am waiting for your test before proposing a PR.

avatar Giuse69
Giuse69 - comment - 25 Nov 2018

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

avatar infograf768
infograf768 - comment - 25 Nov 2018

Try modifying line 151 to
if ($remove_default_prefix && isset($languages[$default_lang]->link))

avatar Giuse69
Giuse69 - comment - 25 Nov 2018

yes, the isset has to be with the ->link, notice is infact gone

avatar Giuse69 Giuse69 - change - 25 Nov 2018
Title
Mod_language does not strip default language (again)
Mod_language does not strip default language
avatar Giuse69 Giuse69 - edited - 25 Nov 2018
avatar Giuse69
Giuse69 - comment - 27 Nov 2018

Hi @infograf768, and are you going to propose the PR? Thanks

avatar infograf768
infograf768 - comment - 27 Nov 2018

will do. Too late anyway for 3.9.1

avatar infograf768
infograf768 - comment - 27 Nov 2018

See #23178
closing as we have a patch

avatar infograf768 infograf768 - close - 27 Nov 2018
avatar infograf768 infograf768 - change - 27 Nov 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-11-27 09:46:59
Closed_By infograf768
avatar infograf768
infograf768 - comment - 29 Nov 2018

@Giuse69
Please test #23178 and mark your test on issues.joomla.org

avatar Giuse69
Giuse69 - comment - 14 Dec 2018

Not solved

Add a Comment

Login with GitHub to post a comment