Language Switching

07-09-2012

Language Switching is our method for building web sites in multiple languages.

UPDATE: Since this article was originally posted there have been some excellent Add Ons released for Concrete5 that makes adding languages easier than using this method. 

Check these links:

http://www.concrete5.org/marketplace/addons/internationalization/

http://www.concrete5.org/marketplace/addons/language-manager/

http://www.concrete5.org/marketplace/addons/translations-manager/

 

We do a lot of sites in multiple languages and have our own twist on language switching.  It's not a lot different than you will see many other people recommending.  It is easy to install and easy for clients to maintain.


Decide your default language, in this example English.  The default Home directory will be the English Home Directory, you can call it whatever you want and put whatever pages you want beneath it.

Our second language will be Spanish, create a directory under Home called 'ES' and check the attribute box for Exclude from Nav. This page is a holder for all Spanish pages.

Go to Dashboard - Pages and Themes - Attributes and add a new CheckBox Atribute.

spanish_attributes.png

Click the Page Types tab and edit your page types to include Spanish Menus as a default Attribute.

Go to Dashboard - Scrapbook and create a new Scrapbook called Language Menus.  

Add 3 new blocks to the Scrapbook:

Language Selection (Content Block)

language_select.png

We use a list for this and style it either horizontal or vertical with links to the home page for English and to es/inicio for Spanish.  You can use a list or just links, if you don't want to put this in a Scrapbook you can code it into your pages.

 

language_select_html.png

 

English Menus (Auto-Nav)


english_menus.png

Spanish Menus (Auto-Nav)

spanish_menus.png

Now create pages under ES, including a home page (Inicio) and as you add them add the Spanish Menus Attribute to each page.

language_sitemap.png

Here's the twist, our language Selecting Widget.  Edit your page files that contain your navigation areas (probably [your_theme]/elements/header.php.  You will see a div like this:

<div id="headerNav">
  <?php 
  $a = new Area('Header Nav');
  $a->display($c);
  ?>
</div>
 
Replace it with:
 
<div id="headerNav">
  <?php
    if ($c->getCollectionAttributeValue('spanish_menus')) {
      $block = Block::getByName('Spanish Menus');  
      if( is_object($block) ) $block->display();  
    } else {  
      $block = Block::getByName('English Menus');  
      if( is_object($block) ) $block->display();  		
    } 
  ?>
</div>

Now any page that has the "Spanish Menus" attribute set will display menus in Spanish, others will display in English.

Decide where you want your Language Selection menu to go and add this div to the page:

<div id="languageSelect">
  <?php
    $block = Block::getByName('Language Select');  
    if( is_object($block) ) $block->display();  
  ?>
</div>
 

You can style that div as you need.

So there you have it, you can see this working at www.bbsanpedro.com

 

* Need more than two languages?  Make more directories as place holders for your different languages, FR for french, EN for English, etc. Add  links to the Language Select links, then use an 'else if' statement in your widget.  Here's an example with the default language set for Danish and having French and English menus:

<div id="nav">
   <?php 
     if ($c->getCollectionAttributeValue('french_language')) {			  
      $block = Block::getByName('French Menus');  
      if( is_object($block) ) $block->display();  
 
    } else if ($c->getCollectionAttributeValue
('english_language')) {   
      $block = Block::getByName('English Menus');  
      if( is_object($block) ) $block->display();  
 
    } else {  
      $block = Block::getByName('Danish Menus');  
      if( is_object($block) ) $block->display();  
 
    } 
  ?>
</div>
 

 

Information provided here has been compiled from Emails, Documentation and the Concrete5 Support Forums.   Examples of PHP, HTML and CSS code displayed on these pages may not be fully functional and may require to be used with other components.  Information here is provided as examples without warranty of any kind.  Weblicating.com has no direct affiliation with Concrete5 CMS Inc.  Thanks to everyone in the C5 community who has provided information for these pages.