Topic: [Tutorial] Multilingual Websites with MODx (improved version)  (Read 38460 times)

Pages: [1] 2 3 ... 6   Go Down

#1: 10-Oct-2008, 04:10 AM

aceman3000
Posts: 60

Multilingual Websites with MODx

After reading the solutions in this forum, none of them convinced me. I wanted that the user stays on the same page if he switches the language.

But i didn´t want to assign the relation between the languages via a template variable where i have to put the corresponding ID of the actual page for every language.
It should function automatic.

So i found a solution for myself. It performs well, but surely has potential for improvements.
Comments and improvements are welcome. Hope my solution helps someone.
Update 02/17/2009
I was asked how to redirect different TLDs to the corresponding language, so updated my code.
Update 02/17/2009
German version available at: http://www.modxcms.de/forum/comments.php?DiscussionID=1771
Update 01/28/2009
Now the connection between languages is made with a template variable called "languageIdentifier". You only need to write a unique name in this field once and then it will be duplicated when you duplicate your language folder.
End Update

Here we go:
1. Modify your configuration under Tools/Configuration

Use the following settings:
friendly URLs: yes
Use friendly aliases: yes
Use friendly alias path: yes

Create a new template variable with name "languageIdentifier"

2. build Site Tree
First make a new page and in the page settings tab click "container" and name the page with your main language as the title. For example „english“, „deutsch“, „français“.
This is what a visitor sees to select his language.
The alias for this folder is a two-letter ISO Country Code that you can find here: http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm

Now you create your pages as you would normally. Enter a languageIdentifier for every page. It could be the same as your alias for example.
After that you duplicate the whole folder for your next language and change the title and the country code in the root folder of that language.

Example:
Code:
english / alias: gb / type: folder (1)
 |-> Welcome  (2)
deutsch / alias: de / type: folder (3)
 |-> Willkommen (4)

Be sure to check the option 'Show in menu' and set the status to 'published' for the folders!

Now under configuration "set Site start": enter the id of the first page of your main language (2 in this example).
You can also set the Site start to the id of the root folder (for example 1) and then use the [[FirstChildRedirect]] snippet on this page.

3. create snippets
Snippet Name: „language“.
Description: creates placeholders and stores the actual language
Code:
<?php
$debugMsg 
'$language is: '.$language.'<br />';
$debugMsg 'LanguageID from cookie: '.$_COOKIE['LanguageID'].'<br />';
$params['topLevel'] =1;

    
// Regular Page
    // find Ultimate Parent of this page. The alias of this page sets the language variable
    
if ($modx->config['error_page'] != $modx->documentIdentifier){
        
$up $modx->runSnippet("UltimateParent"$params);
        
$tv $modx->getTemplateVar('alias'""$up);
        
$language $tv['value'];
        
setcookie("Language",$language,time()+604800"/"""0);
        
// LanguageID is needed for the 404 error page, to show the sitemap in the actual language
        
setcookie("LanguageID",$up,time()+604800"/"""0);
        
$modx->setPlaceholder('LanguageID',$up);
$modx->setPlaceholder('language',$language);
    }
    else{
        
// If Error Page
        
$debugMsg .= '<p>Error Page Start<br />';

        if ( !empty (
$_COOKIE['LanguageID'])){
            
$debugMsg .='$language is empty, try to read from cookie';
            
$language $_COOKIE['Language'];
            
$LanguageID $_COOKIE['LanguageID'];
            
$params['id'] = $LanguageID;
            
$up $modx->runSnippet("UltimateParent"$params);
            
//setcookie("LanguageID",$up,time()+604800, "/", "", 0);
            
$modx->setPlaceholder('LanguageID',$up);
        }
        else {
            
$debugMsg .= '<p>LanguageID Cookie is empty, get ID from Site start</p>'.$modx->config['site_start'];
            
$params['id'] = $modx->config['site_start'];
            
$up $modx->runSnippet("UltimateParent"$params);
            
//setcookie("LanguageID",$up,time()+604800, "/", "", 0);
            
$modx->setPlaceholder('LanguageID',$up);
        }
    }

$debugMsg .= 'LanguageID at finish: [+LanguageID+]<br />';

$debugMsg .= '$language: '.$language.'<br />';
$modx->setPlaceholder('debugMsg'$debugMsg);
//echo $debugMsg;
$modx->setPlaceholder('language'$language);
if (
file_exists("assets/languages/$language.php")) include_once "assets/languages/$language.php";
?>

Snippet Name: „LanguageChooser“
Description: „shortcut for the wayfinder“
Code:
<?php
$langMenu 
$modx->runSnippet(
        
"Wayfinder",
        array(
            
"startId" => "0",
"level"=>"1",
"textOfLinks"=>"alias",
"rowTpl"=>'flagAndLanguage',
"outerTpl"=>'LanguageChooser'
        
)
);
echo 
$langMenu;
?>

Snippet name "link_to_language"
Description: "gets link to actual page in other languages"
Code:
<?php
if (! function_exists('getdocs_with_same_values')){
function 
getdocs_with_same_values($id 'default'$field 'description'

global 
$modx
// Initialize ids 
$ids = array (); 
if (
$id == 'default'

$id $modx->documentIdentifier

$table $modx->getFullTableName('site_tmplvars'); 
$result $modx->db->select('id'$table' name = "'.$field.'"'); 
$array_result $modx->db->makeArray($result); 
if (
count($array_result) > 0

$tvid $array_result[0]['id']; 
$table $modx->getFullTableName('site_tmplvar_contentvalues'); 
$result $modx->db->select('*'$table'contentid = "'.$id.'" and tmplvarid = "'.$tvid.'"'); 
$array_result $modx->db->makeArray($result); 
$fieldvalue $array_result[0]['value']; 
$result $modx->db->select('contentid'$table'value = "'.$fieldvalue.'"'); 
$array_result $modx->db->makeArray($result); 
foreach (
$array_result as $value

$ids[] = $value['contentid']; 


else 

$table $modx->getFullTableName('site_content'); 
$result $modx->db->select($field$table' id = "'.$id.'"'); 
$array_result $modx->db->makeArray($result); 
$fieldvalue $array_result[0][$field]; 
$table $modx->getFullTableName('site_content'); 
$result $modx->db->select($field$table' id = "'.$id.'"'); 
$array_result $modx->db->makeArray($result); 
$fieldvalue $array_result[0][$field]; 
$result $modx->db->select('id'$table$field.' = "'.$fieldvalue.'"'); 
$array_result $modx->db->makeArray($result); 
foreach (
$array_result as $value

$ids[] = $value['id']; 


return 
$ids
}
}

$pages getdocs_with_same_values('default','languageIdentifier');

$langFolder $modx->getDocumentObject('id'$id);
$langFolderID $langFolder['id'];
$langFolderAlias $langFolder['alias'];

// set theURL to the root of the language
$theURL $modx->makeURL($id,'','','full');

foreach (
$pages as $page){
//echo $page;
// Now check if the id of the $value is a child of the UltimateParent
        
$UltimateParent $modx->runSnippet('UltimateParent', array('id' => $page));
//echo "\n".$UltimateParent." ".$langFolderID;  
        //$checks[$counter]['id'] = $value['id'];
        
        // if the UltimateParent is equal to to folderID make link to the actual page in the other language
        
if ($UltimateParent == $langFolderID) {
            
$theURL $modx->makeURL($page,'','','full');
        }
}

return 
$theURL;
?>


create Chunks
Chunk Name: „flagAndLanguage“
Description: „prints a flag and the name of the language as a list item“
Content:
Code:
<li class="[+wf.classnames+]"><a href="[[link_to_language?id=`[+wf.docid+]`&link=`[+wf.link+]`&actualid=`[*id*]`]]"><img src="[(base_url)]assets/images/flags/gif/[+wf.linktext+].gif" alt="[+wf.linktext+]" />&nbsp;[+wf.title+]</a></li>

Chunk Name: "LanguageChooser"
Description: "Outer template for the Language chooser"
Content:
Code:
<ul [+wf.classes+] id="sprachwahl">
[+wf.wrapper+]
</ul>

4. modify Site-Template
Now you insert the follwing code in your template:
in the first row of your template insert the placeholder "[[language]]"
Code:
<div id="languageselection">
[[LanguageChooser]]
</div>
<div id="navigation">
[!Wayfinder? &startId=`[+LanguageID+]`!]
</div>
This includes the languageChooser and a php-file "assets/languages/currentlanguage.php" (currentlanguage is the two-letter ISO Country Code) . And you get a navigation with only the pages in the current language.

Add the placeholder [+language+] where you need it. For example if you have different images in your languages.
Add the placeholder "[+debugMsg+]" for tracing the language snippet

5. Download Icons

This is an optional step. However, if you skip this step, you will need to change the "flagAndLanguage" snippet below.


6. Forms
Open the language file under assets/snippets/eform/lang and add your variables to it

Code:
$_lang['yourname'] = "Your Name";
You can add as many variables as you need.

Now you have to assign those variables to the fields array:
Create a new snippet „setPrompts“
Code:
<?php
function setPrompts(&$fields) {
global 
$_lang;
$fields['yourname'] = $_lang['yourname'];
return 
true;
}
?>
When you create your form chunk you have access to all the placeholders that you defined in the setPrompts snippet:

Code:
[+validationmessage+]
<form method="post" action="[~[*id*]~]">
    <input type="hidden" name="formid" value="feedbackForm" />
    <label for="cf[+yourname+]">[+yourname+]: </label>
<input name="Your Name" id="cf[+yourname+]" class="required text" type="text" eform="[+yourname+]::1:" />
Now insert your form on your contact page with the following code:
Code:
[!setPrompts!]

[!eForm? &formid=`feedbackForm` &to=`my@email.com`&eFormOnBeforeFormMerge=`setPrompts` &language=`german` &subject=`Web site feedback`!]

Most important are the parameters „&language = `german`“ (name needs to be the same as in the assets/snippets/eform/lang folder)  and  „eFormOnBeforeFormMerge=`setPrompts`“ !

Multilingual 404 Error Page
Create a new folder in MODx that is unpublished.
Then create and publish a new page with the title "[+errorpage+]" in it.
Alias: "404"
Code:
[[language]]
[+errorpage+]
[!Wayfinder? &startId=`[+LanguageID+]` &showDescription=`1`!]
Now in your assets/languages/en.php (enter your 2 letter-language-code), add the following:
Code:
<?php
$modx
->setPlaceholder("errorpage",'404 - Page not found');
?>
Translate this message for all of your languages and insert it in the language files.

Your 404 error page is done and provides a sitemap in the current language.

Thats it. Please comment for suggestions.

Redirect TLD to specific language
Hi sorry to bother you. But I was wondering how your website redirects to different TLDs as I need to do this for a site at www.naturediet.co.uk
So you want different TLDs, huh? There you go Smiley
Code:
$e = &$modx->event;
$domains[] = array('url'=>'www.mydomain.de','id'=>72);
$domains[] = array('url'=>'www.mydomain.ch','id'=>920);
$domains[] = array('url'=>'www.mydomain.nl','id'=>242);
$domains[] = array('url'=>'www.mydomain.fr','id'=>494);
$umleitung = false;
$host = $_SERVER['HTTP_HOST'];

if($e->name=='OnWebPageInit') {
foreach ($domains as $domain){
// Wenn beim ersten Aufruf der Seite (site_start) der host == URL ist
if($host == $domain['url'] && $modx->documentIdentifier == $modx->config['site_start']){
$modx->sendRedirect($modx->makeUrl($domain['id'], '', '', 'full'));
exit;
}
}
}

In the array $domains enter your domainnames and the id of the corresponding parent folder and you have it.
« Last Edit: 3-Jan-2010, 08:46 AM by aceman3000 »

#2: 26-Oct-2008, 12:56 PM

makaron
Posts: 2

all works! thx a lot, it's a very usefull tip! but for me don't work a last one thing: setPrompts

do as u wrote... but maybe i'd not fully understand it... plz, if u can, write that steps im more detail, because i'm new to modx...

Thanks a lot!

#3: 27-Oct-2008, 04:27 AM

aceman3000
Posts: 60

Hi folks. I will post updated code for multilingual forms later this day. I modified a lot of things for easier handling.

#4: 27-Oct-2008, 07:22 AM

danielw
Posts: 143

Looks like a great solution! Thanks.

Will try it out and report back with any issues.

#5: 28-Oct-2008, 05:14 AM

aceman3000
Posts: 60

Updated version is online. Added support for a multilingual 404 page

#6: 9-Nov-2008, 12:55 PM

Stiivi
Posts: 6

How do I configure language selection for site root? For example, if I am at mysite/en root then the language menu points to: mysite/en/en, mysite/en/sk ... for other languages it is similar: mysite/sk/en, mysite/sk/sk ... How to fix this?

thanks

#7: 10-Nov-2008, 01:05 AM

aceman3000
Posts: 60

How do I configure language selection for site root? For example, if I am at mysite/en root then the language menu points to: mysite/en/en, mysite/en/sk ... for other languages it is similar: mysite/sk/en, mysite/sk/sk ... How to fix this?

thanks


I think you created your language folders below your english folder. All language folders need to be in the site-root folder.
Just like this:
SiteRoot
    |-> english / alias: en / Type: folder
        |-> Welcome /alias: home (2)
    |-> deutsch / alias: de / Type: folder
        |-> Willkommen /alias: home (3)

#8: 10-Nov-2008, 02:28 AM

Stiivi
Posts: 6

yes, I have the structure like that. problem is, that I am using root/language as language home, instead of root/language/home. I had that structure before I tried to introduce the multilingual code. If you have root/language/home then the code works just fine, because parent of home is language, however for root/language the ultimate parent is the same: language.

how do I make root/language to redirect to root/language/home?

#9: 11-Nov-2008, 08:23 AM

aceman3000
Posts: 60

yes, I have the structure like that. problem is, that I am using root/language as language home, instead of root/language/home. I had that structure before I tried to introduce the multilingual code. If you have root/language/home then the code works just fine, because parent of home is language, however for root/language the ultimate parent is the same: language.

how do I make root/language to redirect to root/language/home?

Hi Stiivi, just try my updated code. I tried it as you explained it and it worked.
I hope it works for you too.

Greetings
Aceman

#10: 14-Nov-2008, 07:14 AM


manu
Posts: 388

WWW
hello, first of all thanks for giving us another solution for multilanguage sites.
Actually i need to redo a Typo3 website in Modx and the multilanguage part is my tricky part so i just try your new solution.

my structure is like yours but i need one sublevel more.. like :

 |-> English / alias: en / Type: folder
        |-> Welcome /alias: home (2)
        |-> Formations /alias: formations(3)
               |-> A /alias: A (4)
               |-> B /alias: B (5)
               |-> C /alias: C (6)

when i m in page A B or C the language link is http:/.... /fr/formations  but should be http:/.... /fr/formations/A.HTML

something I'm doing wrong ?

I'm not a coder but i was thinking :
can't we use the makeUrl function to travel the tree and just replace the /lang/ with your $language?



Code:
$docID =  isset ($id) && intval($id) ? intval($id) : $modx->documentIdentifier;
$myUrl = $modx->makeUrl($docID);
echo('URL :'.$myUrl );


#11: 18-Nov-2008, 08:19 PM

Marketing & Design Team

davidm
Former MODx evangelist
Posts: 7,077

The best way to predict the future is to invent it

WWW
Nice thanks for this, I'll test it and report !
.: COO - Commerce Guys - Community Driven Innovation :.


MODx est l'outil idéal pour les developpeurs et webdesigners qui cherchent un framework de gestion de contenu hautement flexible et performant, tout en étant simple d'accès pour les utilisateurs finaux.

Config : Apache 2.2.8 - MySQL 5.0.67 - PHP 5.2.8 | Debian 4.0 (Etch)

#12: 18-Nov-2008, 09:29 PM

Testers

ganeshXL
Posts: 2,070

true is true

WWW
@manu

Quote
can't we use the makeUrl function to travel the tree and just replace the /lang/ with your $language?

You could use the full URL: $url = $modx->makeUrl($id, '', '', 'full');
Then, do some simple substr() or regex to change domain.com/xx/ -> domain.com/yy/
where xx or yy are your language abbreviations/codes.

in fact, I did this once myself for a threelingual site, works like a charm.

edit:
something along the lines of:

Code:
<?php

// this would be our resulting string fetched via $modx->makeUrl($id, '', '', 'full');
$here "http://www.domain.com/en/news/sport/football/latest-results.html";

// create array from URL:
$ar explode("/"$here);


$de $ar;
$fr $ar;
$it $ar;

// third index is language indicator:
$de[3] = 'de';
$fr[3] = 'fr';
$it[3] = 'it';

$urlDE implode("/"$de);
$urlFR implode("/"$fr);
$urlIT implode("/"$it);

// just for testing... in modx, you'd probably use $modx->setPlaceholder()
echo "original url = $here <br /><br />";
echo 
"german: $urlDE <br />";
echo 
"french: $urlFR <br />";
echo 
"italian: $urlIT <br />";

?>

« Last Edit: 18-Nov-2008, 09:44 PM by ganeshXL »

#13: 19-Nov-2008, 02:41 PM


manu
Posts: 388

WWW
well, tanx ganeshXL for your piece of code.

just to report my experience with the original tutorial :

in fact the code here works ^^ with sub folder too... but all aliases must exist otherwise it break.

#14: 20-Nov-2008, 12:22 PM

virgil
Posts: 52

Hello, and thanks for this!

I'm not sure if I'm doing something completely idiotic here, but I've followed this pretty much to the letter, and whenever I check the links for the language selection, the path inserts "minimal-base" instead of the appropriate alias.

Any ideas?  Thanks!

OK, my bad- I hadn't set the folders that contain the seperate language files to be shown in the menu- therefore the wayfinder call was going all the way to my old, non-published "Home" page and pulling it's alias.

Now, I have another problem- yep, this switches between the language pairs, but after switching from one language to another, the paths degenerate and eventually lead to non-existent pages.  Scenario:

I have two folders, english and french in the root of the directory structure.  Two links  are shown for choosing language: english and french.  Start page in configuration is a page in the english folder.

Click on the french link- the correct alias path is in the link, french page loads.
Click on the english link- the correct alias path is in the link, english page loads.

Here's where it goes pear shaped: 

Hover over the link for either english or french, and the link points to the root language folder only in either language: running on an xampp server, path is localhost/modx/en or localhost/modx/fr.  These folders have no content, but OK, I go ahead and click on one of these links. This is an error, by the way- the paths should still be pointing to the page that is currently loaded, correct?  It seems that the alias for the document has completely disappeared after the alias for the parent language folder!

Now the paths in the links are completely incorrect.  Paths are now shown as localhost/modx/en/en or localhost/modx/fr/en

I really want to deploy this solution, as I don't want a packet of TV's that end-users have to configure!

Edit: figured this one out! 

When calling the snippet [[language]] in the first line of the template code cached, this error occurs!  Simply calling the snippet uncached [!language!], and the problem goes away!  You might want to make mention of this in your usage of this solution, aceman!  Excellent solution, by the way- not too hard to document this for end users!
« Last Edit: 20-Nov-2008, 02:24 PM by virgil »

#15: 24-Nov-2008, 10:01 AM

aceman3000
Posts: 60

Hi folks,

tommorow i will publish a big update.

I modified the complete logic, because some people on the german forum complained about the use of the alias field because it breaks the nature of speaking urls. So now i use the description field for my approach. I could do it with a template variable too, but maybe i post this later.

What changed?
The languageChooser points to the actual page in the corresponding languages.
If the actual page can´t be found in the other language it will take you to the root of the language.
So no more 404s if you switch the language and the page doesn´t exist.
You now can have unique aliases for every page.

This is the old version. Look at first post for the actual version.

Multilingual Websites with MODx

After reading the solutions in this forum, none of them convinced me. I wanted that the user stays on the same page if he switches the language.

But i didn´t want to assign the relation between the languages via a template variable. It should function automatic.

So i found a solution for myself. It performs well, but surely has potential for improvements.
Comments and improvements are welcome. Hope my solution helps someone.

Update
I found out that my solution couldn´t handle subfolders in the language folder. Too bad that my site needs subfolders. So i rewrote the main snippet "language" from scratch. Now I have a very good solution and even a 404 error page, that shows a sitemap in your selected language. If the language can´t be determined, it will show the default language of your site (defined in configuration/site root).

Changes between the previous version are:
Put the snippet [[language]] in the first line of your template.
Add the placeholder [+language+] where you need it. For example if you have different images in your languages.
A brandnew 404 error page
Placeholder "[+debugMsg+]" for tracing the language snippet
End Update

Here we go:
EDIT:
1. Modify your configuration under Tools/Configuration

Use the following settings:
friendly URLs: yes
Use friendly aliases: yes
Use friendly alias path: yes
Allow duplicate aliases: yes

2. build Site Tree
First make a new folder and name it with your main language as the title. For example „english“, „deutsch“, „français“.
This is what a visitor sees to set his language.
The alias for this folder is a two-letter ISO Country Code that you can find here: http://www.iso.org/iso/country_codes/iso_3166_code_lists/english_country_names_and_code_elements.htm

Now you create your pages as you would normally and give them an alias. After that you duplicate the whole folder for your next language and change the title and the country code in the alias field.
The alias of the pages in your language folders is always the same. So if the first page has the alias „home“, the name in every other language is also „home“!!!

Example:
Code:
english / alias: en / type: folder
 |-> Welcome / alias: home (2)
deutsch / alias: de / type: folder
 |-> Willkommen / alias: home (3)

Now under configuration "set Site start": enter the id of the first page of your main language (2 in this example)

3. create snippets
Snippet Name: „language“.
Description: creates placeholders and stores the actual language
Code:
<?php

// Placeholders set by this 
$debugMsg '$language is: '.$language.'<br />';
$debugMsg 'LanguageID from cookie: '.$_COOKIE['LanguageID'].'<br />';
$params['topLevel'] =1;
$aliasPath '';

    
// Regular Page
    // find Ultimate Parent of this page. The alias of this page sets the language variable
    
if ($modx->config['error_page'] != $modx->documentIdentifier){
        
$up $modx->runSnippet("UltimateParent"$params);
        
$tv $modx->getTemplateVar('alias'""$up);
        
$language $tv['value'];
        
setcookie("Language",$language,time()+604800"/"""0);
        
// LanguageID is needed for the 404 error page, to show the sitemap in the actual language
        
setcookie("LanguageID",$up,time()+604800"/"""0);
        
$modx->setPlaceholder('LanguageID',$up);
        
$aliasPath str_replace($language.'/','/',$modx->virtualDir.'/[*alias*]');
    }
    else{
        
// If Error Page
        
$debugMsg .= '<p>Error Page Start<br />';

        if ( !empty (
$_COOKIE['LanguageID'])){
            
$debugMsg .='$language is empty, try to read from cookie';
            
$language $_COOKIE['Language'];
            
$LanguageID $_COOKIE['LanguageID'];
            
$params['id'] = $LanguageID;
            
$up $modx->runSnippet("UltimateParent"$params);
            
//setcookie("LanguageID",$up,time()+604800, "/", "", 0);
            
$modx->setPlaceholder('LanguageID',$up);
        }
        else {
            
$debugMsg .= '<p>LanguageID Cookie is empty, get ID from Site start</p>'.$modx->config['site_start'];
            
$params['id'] = $modx->config['site_start'];
            
$up $modx->runSnippet("UltimateParent"$params);
            
//setcookie("LanguageID",$up,time()+604800, "/", "", 0);
            
$modx->setPlaceholder('LanguageID',$up);
        }
    }

$debugMsg .= 'LanguageID at finish: [+LanguageID+]<br />';

$debugMsg .= '$aliasPath: '.$aliasPath.'<br />';
$modx->setPlaceholder('aliasPath'$aliasPath);

$debugMsg .= '$language: '.$language.'<br />';
$modx->setPlaceholder('debugMsg'$debugMsg);

$modx->setPlaceholder('language'$language);
if (
file_exists("assets/languages/$language.php")) include "assets/languages/$language.php";
?>

Snippet "languageInclude" is no longer needed

Snippet Name: „LanguageChooser“
Description: „shortcut for the wayfinder“
Code:
<?php
echo '[!Wayfinder?&startId=0&level=1&rowTpl=LanguageRow&titleOfLinks=alias!]';
?>
The titleOfLinks=alias sends the two-letter-language-code to the LanguageRow Snippet.

create ChunkUpdate
Chunk Name: „LanguageRow“
Description: „prints a flag and the name of the language as a list item“
Content:
Code:
<li class="category [+wf.classnames+]"><a href="[(site_url)][+wf.title+][+aliasPath+]"><img src="[(base_url)]/assets/images/flags/gif/[+wf.title+].gif" alt="[+wf.linktext+]" /></a>&nbsp;<a href="[(site_url)][+wf.title+][+aliasPath+]" >[+wf.linktext+]</a>[+wf.wrapper+]</li>


4. modify Site-TemplateUpdate
Now you insert the follwing code in your template:
inf first row of your template insert the placeholder "[[language]]"
Code:
<div id="languageselection">
[[LanguageChooser]]
</div>
<div id="navigation">
[!Wayfinder? &startId=`[[UltimateParent]]`!]
</div>
This includes the languageChooser and the php-file in the actual language. And you get a navigation with only the sites in the selected language.

5. Download Icons

This is an optional step. However, if you skip this step, you will need to change the "LanguageRow" snippet below.

  • Download a set of icons whose names correspond to a two-letter Country Code and extract the folder to assets/images/flags
    you get nice looking flags from here: http://www.famfamfam.com/lab/icons/flags/
  • Make a directory assets/languages

6. Forms
Open the language file under assets/snippets/eform/lang and add your variables to it

Code:
$_lang['yourname'] = "Your Name";
You can add as many variables as you need.

Now you have to assign those variables to the fields array:
Create a new snippet „setPrompts“
Code:
<?php
function setPrompts(&$fields) {
global 
$_lang;
$fields['yourname'] = $_lang['yourname'];
return 
true;
}
?>
When you create your form chunk you have access to all the placeholders that you defined in the setPrompts snippet:

Code:
[+validationmessage+]
<form method="post" action="[~[*id*]~]">
    <input type="hidden" name="formid" value="feedbackForm" />
    <label for="cf[+yourname+]">[+yourname+]: </label>
<input name="Your Name" id="cf[+yourname+]" class="required text" type="text" eform="[+yourname+]::1:" />
Now insert your form on your contact page with the following code:
Code:
[[setPrompts]]

[!eForm? &formid=`feedbackForm` &to=`my@email.com`&eFormOnBeforeFormMerge=`setPrompts` &language=`german` &subject=`Web site feedback`!]

Most important are the parameters „&language = `german`“ (name needs to be the same as in the assets/snippets/eform/lang folder)  and  „eFormOnBeforeFormMerge=`setPrompts`“ !

Multilingual 404 Error Page
Create a new folder in MODx that is unpublished.
Then create and publish a new page with the title "[+errorpage+]" in it.
Alias: "404"
Code:
[[language]]
[+errorpage+]
[!Wayfinder? &startId=`[+LanguageID+]` &showDescription=`1`!]

Now in your assets/languages/en.php (enter your 2 letter-language-code), add the following:
[code]<?php
$modx
->setPlaceholder("errorpage",'404 - Page not found');
?>
Translate this message for all of your languages and insert it in the language files.

Your 404 error page is done and provides a sitemap in the actual language.
If the user selects a language for a page, that isn´t yet published for that language, he will be redirected to the 404-page in the selected language.

Thats it. Please comment.[/code]
« Last Edit: 25-Nov-2008, 03:01 AM by aceman3000 »

#16: 28-Jan-2009, 02:01 PM

labasus
Posts: 315

Is it possible to modify your snippet to construct non dublicated language structures, I mean without en, de, jp directories....
There are some reasons to do it before revolution will handle it.

1) For web sites with more than ~ 100 (or even more than 50) documents too difficully to make changes in several documents places, worst thing if you must do it for more than 2 languages.
2) Document consolidation
3) Great solution for MODx community.

Thx

#17: 30-Jan-2009, 11:55 AM

mamuni
Posts: 15

HI. Thak you for this tutorial.
However, I am not sure how to use the TV languageIdentifier.
Can you explain it a little more please.
Thanks in advance

#18: 16-Feb-2009, 01:49 AM

aceman3000
Posts: 60

Oh it´s really simple. If you have a page "contact" you type in the name "contact" in the languageIdentifier TV in every language for this page. This is how my snippet knows wich is the corresponding page in the other languages.

#19: 17-Feb-2009, 04:26 AM

johnboiles
Posts: 15

Thanks for the great tutorial!

Make sure to note that for the language menu to function, 'Show in menu' has to be enabled for the root folders for each language. Otherwise wayfinder won't produce anything!

#20: 20-Feb-2009, 06:05 PM


lesur
Posts: 20

JP

Thanks for this. I was in desperate need of a multilingual solution.  I've got my site set up and running properly, except for one thing, ditto is not working right alongside the multilingual stuff.

Ditto always adds a double language identifier to the links it produces.  For instance it produces "http://mysite.com/us/us/FAQs/name.html"

I'm also learning ditto as I go, but an example call I am using is:

[!Ditto? &parents=`392`&sortBy=`title` &display=`all` &depth=`2` &summarize=`10`  &showPublishedOnly=`1` &tpl=`faqtpl`!]

where my menu structure is like:

    |-> English / alias: us / Type: folder (270)
        |-> Welcome /alias: Welcome (389)
            |-> FAQs /alias: FAQs (392)
                |-> Name /alias: name (393)

Just to clarify, Wayfinder does do the links correctly from the startpage on through all the levels (i.e. &level=`3`). 

How can I fix this?
Pages: [1] 2 3 ... 6   Go Up
0 Members and 2 Guests are viewing this topic.