I was given this idea from Bogdan Günther and it sounded like a great idea if you are using FURLs. If you set everything up the way they recommend in the wiki, things work well. But when you have 100’s of pages, it gets very difficult to map all translated pages.
Bogdan said: you really need this feature you could use friendly URLs to accomplish this if you use the same alias in both languages:
Code:
mysite.com/de/category-10/product-4/variant-17.html
mysite.com/en/category-10/product-4/variant-17.html
Then you only have to change the language indicator in the URI.
That is so obvious and brilliant. So I tried cobbling a solution together. Mostly by copying the Multi-Language wiki.
All I have to do now is check to see if the page is available and if not, redirect to the main language page. That is where I am now. Can anyone help me? Also, if you wish, please clean up the script. I am a PHP newbie.
I also created a new TV that shows what language you are in. I made it a radio button, so it is very easy for the user. The value is the ISO 2-letter code.
English==en||Español==es||Nederlands==nl||Português==pt||Русский==ru||中文==zh
<?php
// Most code copied from Multi-language wiki page
// Thanks to Bogdan Günther for giving me the idea!
$pageURL = $_SERVER["REQUEST_URI"];
if ($pageURL == '/') $pageURL = '/en/'; //only here because my start page doesn't have a language designation.
// get current language
$tv = $modx->getTemplateVar('Lang', "", $modx->documentIdentifier);
$curlang = $tv['value'];
$langnames = array(
"en" => "English",
"es" => "Español",
"nl" => "Nederlands",
"pt" => "Português",
"ru" => "Русский",
"zh" => "中文"
);
$output = "";
// loop through language entries
for ($e = 0; $e < sizeof($langnames); $e++)
{
$lname = current($langnames);
$target = key($langnames);
$nPageURL = str_replace('/'.$curlang.'/', '/'.$target.'/', $pageURL); //replace current language folder with target language folder
$image = '<img src="assets/images/flags/'.$target.'.gif" width="16" height="11" alt="'.$target.'" title="'.$lname.'" />';
if (strlen($output))
{
$output .= ' ';
}
if ($curlang != $target) { //prevents adding a flag for the current language
$output .= '<a href="'.$nPageURL.'">'.$image.'</a>';
}
next($langnames);
}
return $output;
?>
Here is my test site (not fully implemented, because I don’t have any language pages below the home pages yet):
http://test.valcompanies.com/