Hi there,
I really love Lingua plugin for multi language websites, but what i struggle with are missing friendly urls which would work exactly in same way as in babel. i have tried so many things, asked programers to look at it, but it seems like unachieved task. any ideas?
I made this post some months ago about this topic. Maybe it helps.
hey,
thanks for reply, but it doesn’t work. any ideas why?
i appended .htaccess file according your suggestion, i guess there are necessary changes in lingua selector snippet …
I took another look at the snippet lingua.selector
and the code is a mess (and trying to adjust it only makes things worse). It also doesn’t seem to work correctly if MODX_BASE_URL
isn’t /
or if you don’t use the standard port.
That said, here is what you could try:
- Duplicate the snippet
lingua.selector
first and make the changes to the copy. Don’t change the original snippet. - Replace these lines in the snippet
with this code:
$req_uri = $_SERVER["REQUEST_URI"];
//$modx->log(modX::LOG_LEVEL_ERROR,'BEFORE: $req_uri =' . $req_uri);
$req_uri = preg_replace("#^" . preg_quote(MODX_BASE_URL, '#') . "(de|en|fr|es)(/|$)#i", MODX_BASE_URL, $req_uri);
//$modx->log(modX::LOG_LEVEL_ERROR,'AFTER: $req_uri =' . $req_uri);
if ($_SERVER["SERVER_PORT"] !== "80" && $_SERVER["SERVER_PORT"] !== "443") { //FIX from https://github.com/goldsky/Lingua/pull/67
$pageURL .= $_SERVER["SERVER_NAME"] . ":" . $_SERVER["SERVER_PORT"] . $req_uri;
} else {
$pageURL .= $_SERVER["SERVER_NAME"] . $req_uri;
}
Replace (de|en|fr|es)
with the list of your languages.
This change should remove the language part from the request URI if it exists (changing /en/mypage.html
to /mypage.html
).
- Then replace this line in the snippet
with this code
$parse_pageUrl = parse_url($pageURL);
$myhost = preg_quote($parseUrl['host'], '#');
if ($parse_pageUrl['port']){
$myhost .= ":" . $parse_pageUrl['port'];
}
$regex = "#(?<=" . $myhost . ")" . preg_quote(MODX_BASE_URL, '#') . "#";
//$modx->log(modX::LOG_LEVEL_ERROR,'BEFORE: $pageURL =' . $pageURL);
$language['url'] = preg_replace($regex, MODX_BASE_URL . $language[$codeField] . "/", $pageURL);
//$modx->log(modX::LOG_LEVEL_ERROR,'AFTER: $pageURL =' . $language['url']);
This code should insert the language into the URL (changing https://www.domain.com/mypage.html
to https://www.domain.com/en/mypage.html
)
- If the links on your homepage don’t look correct, try changing this line in the snippet. Maybe just to
$pageURL = $parseUrl['scheme'] . '://' . $parseUrl['host'] . '/';
- If your resources have different aliases for different languages and they don’t show up, try changing these lines.