One more approach to build a multilingual site with the regular CMSMS

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Karolis wrote: the second language button (I am testing with two) does not appear.
I also use two languages. I changed the order in the languages array, and it works fine too.

When show_current=true I get this:

Code: Select all

<ul><li class='currentpage'>button in es</li><li><a href="http://domain/site/page-in-eo" title="title in eo">button in eo</a></li></ul>
And when show_current is not set, I get this:

Code: Select all

<ul><li><a href="http://domain/site/page-in-eo" title="title in eo">button in eo</a></li></ul>
What HTML do you get?
Last edited by alinome.net on Tue Feb 05, 2008 12:09 pm, edited 1 time in total.
Marcos Cruz
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Very strange. Send me or post the code of your {language_menu}. I'll compare both codes with the vim editor.

Meanwhile, above this line:

Code: Select all

echo "<ul>";
add this debug info:

Code: Select all

echo "<p><strong>Debug info:</strong><br/>";
echo "page_lang=$page_lang<br/>";
echo "show_title=$show_title<br/>";
echo "show_current=$show_current<br/>";
echo "page_title=$page_title<br/>";
echo "language_buttons=<br/>";
print_r($language_buttons);
echo "</br>language_versions=<br/>";
print_r($language_versions);
echo "</p>";
Is everything right? Maybe there's a clue there.

Also, you can put debug points echo-ing some texts inside the foreach loop.
Last edited by alinome.net on Tue Feb 05, 2008 7:35 pm, edited 1 time in total.
Marcos Cruz
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Here is an improved version of {language_menu}. The new features are listed in the code. I have tested it and it works fine with all parameter combinations.

The code looks much longer because of the many comments, optional debug lines, and cleaner layout.

Code: Select all

/*

Create a language menu (version 4).

2008-02-05 Por/Far/By Marcos Cruz (alinome.net)

Different page names in every language, all specified in one single content block.

New features in this version:
- Every language has a default page
  to be used when no translation has been specified for the current page.
  or when the specified page doesn't exist.
- The HTML attribute hreflang is added to the links.
- The UDT parameter show_title now works in inactive links to the current page too.

Changes in the code:
- The function smarty_cms_selflink has been factored to make it easier to reuse the code.
- The conditional operator "?:" used instead of several if-structures.
- Code revised according to the PHP coding guidelines (spaces, string quotes, braces...)
- Optional debug points homogenized and marked with the word "DEBUG". Delete them if you want.
- New comments for beginners.

Possible future improvements:
- Spaces allowed in the template variable $other_languages (syntactic sugar to make it error-proof).

*/


global $gCms;


// ----------------------------------------------------------------
// Parameters

$show_current = $params['show_current']; // Show an inactive link to the current page in the menu? true/false
$show_title = $params['show_title']; // Use the page titles instead of the defined buttons? true/false


// ----------------------------------------------------------------
// Language data common to all pages

/*
The array $language_buttons keeps information about all languages of the site.
It is indexed by the language code. Every item is an array that contains two items:
0) The default page for the language (used when there's no valid alternative)
1) The HTML code of the link button (used when the parameter show_title is false or unset).
*/

/* Example:
$language_buttons = array(
  "es" => array("inicio", "<span xml:lang='es'>en castellano</span>"),
  "eo" => array("komenco", "<span xml:lang='eo'>esperante</span>"),
  "lt" => array("lt-home", "<span xml:lang='lt'>lt-button</span>"),
  "ru" => array("ru-home", "<span xml:lang='ru'>ru-button</span>"),
  "en" => array("home", "<span xml:lang='en'>in English</span>") );
*/

$language_buttons = array(
  "eo" => array("komenco", "<span xml:lang='eo'>en esperanto</span>"),
  "es" => array("inicio", "<span xml:lang='es'>en castellano</span>") );


// ----------------------------------------------------------------
// Variables about the current page

$page_lang = $gCms->smarty->get_template_vars('page_lang'); // assigned in the template by the User Defined Tag {assign_page_lang}


// ----------------------------------------------------------------
// Language versions of the current page

/*
The template variable $other_languages is assigned with a content block in the template. eg.:
{content block="Other languages" oneline="true" assign="other_languages" wysiwyg=false}
For every page, it has to filled with a string, using the following format:
"c1=page1;c2=page2;c3=page3" (no spaces allowed),
where c(n) are language codes (two letters);
any number of languages can be specified in any order.
That string is converted into the array $language_versions.
*/

$other_languages = $gCms->smarty->get_template_vars('other_languages');
foreach ( explode(';', $other_languages) as $other_language )
{
  $language_codes[]=substr($other_language, 0, 2);
  $language_pages[]=substr($other_language, 3);
}
$language_versions = array_combine($language_codes, $language_pages);


/*
echo "<p><strong>DEBUG</strong>:";
echo "<br/>language_codes="; print_r($language_codes);
echo "<br/>language_pages="; print_r($language_pages);
echo "<br/>language_versions="; print_r($language_versions);
echo "</p>";
*/


// ----------------------------------------------------------------

function smarty_tag ($smarty_tag)
{

  // Call a smarty tag.
  // Reference:
  // http://wiki.cmsmadesimple.org/index.php/User_Handbook/Admin_Panel/Extensions/User_Defined_Tags#How_to_Execute_Smarty_Tags_from_A_User_Defined_Tag_.28UDT.29

  global $gCms;

  /*
  echo "<p><strong>smarty_tag DEBUG</strong>:";
  echo "<br/>smarty_tag=$smarty_tag";
  echo "</p>";
  */

  $smarty = &$gCms->GetSmarty();
  $smarty->_compile_source('temporary template', $smarty_tag, $_compiled);
  @ob_start();
  $smarty->_eval('?>' . $_compiled);
  $_contents = @ob_get_contents();
  @ob_end_clean();
  echo $_contents;

}


// ----------------------------------------------------------------

function smarty_cms_selflink ($page, $text, $more)
{

  // Call the CMSMS tag {cms_selflink}.

  /*
  echo "<p><strong>smarty_self_link DEBUG</strong>:";
  echo "<br/>page=$page";
  echo "<br/>text=$text";
  echo "<br/>more=$more";
  echo "</p>";
  */

  smarty_tag ("{cms_selflink page=$page" . ( (empty($text)) ? '' : ' text="' . $text .'"' ) . ' more="' . $more . '"}');

}


// ----------------------------------------------------------------

function valid_page($page_alias)
{

  // Is a page alias valid?

  // References:
  // http://forum.cmsmadesimple.org/index.php/topic,3778.msg20921.html#msg20921

  global $gCms;

  $manager =& $gCms->GetHierarchyManager();
  $node =& $manager->getNodeByAlias($page_alias);
  return ( $node != null );

}


// ----------------------------------------------------------------

function current_page_title()
{

  // Return the title of the current page.
  // This is needed only to print and inactive link to the current page when the parameter $show_title==true.

  // References:
  // http://forum.cmsmadesimple.org/index.php/topic,3778.msg20921.html#msg20921
  // http://forum.cmsmadesimple.org/index.php/topic,3778.msg21051.html#msg21051

  global $gCms;

  $this_page = $gCms->variables['content_id'];
  $manager =& $gCms->GetHierarchyManager();
  $node =& $manager->GetNodeById($this_page);
  $content =& $node->getContent();
  return $content->Name();

}


// ----------------------------------------------------------------
// Create the menu

/*
echo "<p><strong>DEBUG</strong>:";
echo "<br/>page_lang=$page_lang";
echo "<br/>show_title=$show_title";
echo "<br/>show_current=$show_current";
echo "<br/>page_title=$page_title";
echo "<br/>language_versions="; print_r($language_versions);
echo "<br/>language_buttons="; print_r($language_buttons);
echo "</p>";
*/

echo "<ul>";

foreach ( $language_buttons as $language => $button )  // explore all possible languages of the site
{

  // $language = code of the language being explored
  // $button[0] = the default page used when no alternative has been defined for the language
  // $button[1] = HTML used as button for the link, when $show_title==false.

  /*
  echo "<p><strong>menu DEBUG</strong>:";
  echo "<br/>language=$language";
  echo "<br/>button="; print_r($button);
  echo "</p>";
  */

  if ( $language != $page_lang )
  {
    $alternative_page = $language_versions[$language];
    // echo "alternative_page=$alternative_page"; // DEBUG
    if ( valid_page($alternative_page) )
    {
      echo "<li>";
      // echo "ALTERNATIVE: $alternative_page"; // DEBUG
      smarty_cms_selflink($alternative_page,($show_title == true ) ? "" : $button[1], "hreflang='".$language."'");
      echo "</li>";
    }
    else // no alternative page valid for this language
    {
      echo "<li>";
      // echo 'NO ALTERNATIVE!'; // DEBUG
      smarty_cms_selflink($button[0],( ($show_title == true ) ? "" : $button[1] ) , "hreflang='".$language."'");
      echo "</li>";
    }
  }
  elseif ( $show_current == true )
  {
    // echo "SHOW CURRENT"; // DEBUG
    echo "<li class='currentpage'>" . ( ($show_title == true) ? current_page_title() : $button[1] ) . "</li>";
  }
}

echo "</ul>";
Last edited by alinome.net on Wed Feb 06, 2008 11:26 am, edited 1 time in total.
Marcos Cruz
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Karolis wrote: Tested and confirmed: it works!
show_current functionality also started working with this new version.
Great! Last night I had a new idea: A parameter "show_default" would be useful. Maybe some people prefer no link or an inactive one when no alternative language has been specified... I'll try that.
Karolis wrote: I think I will translate the instructions and put it on Lithuanian forum today.
Good idea. I'll do the same in the Spanish one. I suggest to put a link to this thread, where I'll post possible improvements. Also, I will post here a step by step guide for the last version.
Last edited by alinome.net on Wed Feb 06, 2008 11:48 pm, edited 1 time in total.
Marcos Cruz
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Final step by step implementation guide of this multilingual model

Post by alinome.net »

[Update/Actualización: He publicado una versión en español de este mensaje. I've posted a Spanish version of this message.]

[Update (2009-08-26): Pierre-Luc has written a module called Babel, based on this method, easier to use and with more features.]


Hi all,

These days this proposal of mine has been improved several times, mainly following the Karolis' useful suggestions. To help people implement this multilingual model, I repeat here all the steps needed, with the last version of the code.

I think this model is very easy to implement and to use for a normal user. You don't need to be a programmer, but just a bit acquainted with CMS Made Simple. Nevertheless I'll try to explain everything very easily.

Features
  • * Only one template needed for all pages in all languages (though of course you can use more if you want).
  • * Only two User Defined Tags needed.
  • * Suitable for any number of languages.
  • * No need to have all language versions ready at the same time.
  • * The alias of the language versions can be completely different.
  • * Default page can be specified for every language, to be linked when no translation is avalaible.
  • * Link buttons can be specified for every language (any HTML can be used, also images).
  • * Language menu fully configurable with parameters:
    • [li] - Use the page titles or the configurable HTML buttons as the link text.
    • - Show the current page as an inactive link or show nothing.
    • - Link the language default page when no translation is avalaible, or create no link.
    • - Show an inactive button when no default page has been specified, or nothing.
    [/li]
  • * Fully commented code. Easy to understand and adapt.
Step 1: Create the content hierarchy

Create a root section header for every language of your site following this steps:

1) Give it the name you like (the name of the language is a good option).
2) The alias must be the language code (two letters).
3) Delete the tick "Show in menu". We don't want the section header in the menu.

All your content (pages, sections headers...) in a language must be under the corresponding root section header.

Step 2: Get the language of every page

Create a new User Defined Tag and name it assign_page_lang.

Copy and paste this code into it:

Code: Select all

/*
Assign the Smarty variable $page_lang depending on the root parent of the current page.
The root parent alias must be the language code (two letters).

2008-02-02 Marcos Cruz (http://alinome.net)

Second version, without array

Reference:
http://wiki.cmsmadesimple.org/index.php/Share_your_tags_here#Get_a_page.27s_root_parent.27s_alias
*/

global $gCms;
global $smarty;

$manager =& $gCms->GetHierarchyManager();

$result = '??';
$thisPage = $gCms->variables['content_id'];
$currentNode = &$manager->sureGetNodeById($thisPage);
while( isset($currentNode) && $currentNode->getLevel() >= 0 )
{
    $currentContent =& $currentNode->getContent();
    $result = $currentContent->Alias();
    $currentNode =& $currentNode->getParentNode();
}
$smarty->assign('page_lang',$result);
Then put the tag at the top of your template(s), like this:

Code: Select all

{assign_page_lang}
Step 3: Select content depending on the page language

Now you can use the Smarty variable $page_lang in your templates and pages
to select content.

Example 1:

Code: Select all

<__html xmlns='http://www.w3.org/1999/xhtml' xml:lang='{$page_lang}'>
Example 2:

Code: Select all

{if $page_lang == 'es'}<h1>Título en castellano</h1>
{elseif $page_lang == 'eo'}<h1>Esperanto-titolo</h1>
{else}<h1>English title</h1>
{/if}
   

Step 4: Create the content menu

How do you show in your navigation menu only the pages in the current language?

You can use the {menu} tag with the parameter start_element, to select the root section header of the language. This is an example:

Code: Select all

<div id='menu'>
{if $page_lang == "es"}{menu template='my_menu' start_element='1.1'}
{else}{menu template='my_menu' start_element='2.1'}
{/if}
</div>
Another method is to use the parameter start_level. Then only one {menu} is needed:

Code: Select all

<div id='menu'>
{menu template='my_menu' start_level='2'}
</div>
Read the help of the menu module to learn how to use all its parameters.

Step 5: Specify the language translations of every page

Put this at the top of your template:

Code: Select all

{content block='Other languages' oneline='true' assign='other_languages' wysiwyg='false'}
That will create a new content block for every page. Its content will be asigned to the Smarty variable other_languages. When you edit a page, you must fill this block with the names of its translations, using the following format:

en=alias-of-the-corresponding-english-page;ru=alias-of-the-corresponding-russian-page

No spaces are allowed. Language codes must have two letters. Use semicolon to apart the languages. You can specify as many languages as you like, in any order.

Step 6: Create the language menu

Create an User Defined Tag and name it language_menu.

Copy and paste this code into it (it has a lot of comments to help understand it):

Code: Select all

/*

Create a language menu (version 5).

2008-02-06 Por/Far/By Marcos Cruz (http://alinome.net)

New features in this version 5:
- New parameter show_default.
- New parameter show_inactive.

Changes in the code in this version 5:
- Some forgotten doble quotes has been changed to single quotes for better perfomance.
- Doble and single quotes changed in the example HTML buttons.
- New constants DEFAULT_PAGE and BUTTON to make the code more readable without using keys in the language array.
- Language array and other variables renamed to make the code more readable.
- More comments to help beginners understand the code.

New features in version 4:
- Every language has a default page to be used when no translation has been specified for the current page,
  or when the specified page doesn't exist.
- The HTML attribute hreflang is added to the links.
- The UDT parameter show_title now works in inactive links to the current page too.

Changes in the code in version 4:
- The function smarty_cms_selflink has been factored to make it easier to reuse the code.
- The conditional operator "?:" used instead of several if-structures.
- Code revised according to the PHP coding guidelines (spaces, string quotes, braces...)
- Optional debug points homogenized and marked with the word "DEBUG". Delete them if you want.
- New comments for beginners.

Possible future improvements:
- Spaces allowed in the template variable $other_languages (syntactic sugar to make it error-proof).

*/


global $gCms;


// ----------------------------------------------------------------
// Parameters

// Show the page title in the links? (true/false)
// (if false, show the defined buttons)
$show_title = isset($params['show_title']) ? $params['show_title'] : false;

// Show an inactive link to the current page in the menu? (true/false)
// (if false, don't create a menu entry for the current page)
$show_current = isset($params['show_current']) ? $params['show_current'] : false; 

// If there's no valid translation, then show a link to the default page? (true/false)
// (if false, don't create a menu entry for that language)
$show_default = isset($params['show_default']) ? $params['show_default'] : true;

// If  $show_default==false or there's no valid default page, then show an inactive link? (true/false)
// (if false, don't create a menu entry for that language)
$show_inactive = isset($params['show_inactive']) ? $params['show_inactive'] : true; 


// ----------------------------------------------------------------
// Constants

define ("DEFAULT_PAGE",0);
define ("BUTTON",1);


// ----------------------------------------------------------------
// Language data common to all pages

/*
The array $languages keeps information about all languages of the site.
It is indexed by the language code.

Every item is an array that contains two items:
0) The optional default page for the language (used when there's no valid translation, if the parameter show_default is true). It can be empty (and then the parameter show_inactive will decide what to do).
1) The HTML code of the link button (used when the parameter show_title is false or unset).
To make the code more readable, these items are accessed with the constants DEFAULT_PAGE and BUTTON in the main loop.

*/

// Example data
$languages = array(
  'en' => array('home', '<span xml:lang="en">English</span>'),
  'es' => array('inicio', '<span xml:lang="es">Castellano</span>'),
  'eo' => array('komenco', '<span xml:lang="eo">Esperanto</span>'),
  'lt' => array('pradinis', '<span xml:lang="lt">Lietuviškai</span>'),
  'ru' => array('glavnaja', '<span xml:lang="ru">Pусский</span>') );


// ----------------------------------------------------------------
// Variables about the current page

$page_lang = $gCms->smarty->get_template_vars('page_lang'); // assigned in the template by the User Defined Tag {assign_page_lang}


// ----------------------------------------------------------------
// Language versions of the current page

/*
The template variable $other_languages is assigned with a content block in the template. eg.:
{content block='Other languages' oneline='true' assign=other_languages wysiwyg='false'}
For every page, it has to be filled with a string in the following format:
"c1=page1;c2=page2;c3=page3" (no spaces allowed),
where c(n) are language codes (two letters);
any number of languages can be specified in any order.
That string is converted into the array $language_versions.
*/

$other_languages = $gCms->smarty->get_template_vars('other_languages');
foreach ( explode(';', $other_languages) as $other_language )
{
  $language_codes[]=substr($other_language, 0, 2);
  $language_pages[]=substr($other_language, 3);
}
$language_versions = array_combine($language_codes, $language_pages);


/*
echo '<p><strong>DEBUG</strong>:';
echo "<br/>language_codes="; print_r($language_codes);
echo "<br/>language_pages="; print_r($language_pages);
echo "<br/>language_versions="; print_r($language_versions);
echo '</p>';
*/


// ----------------------------------------------------------------

function smarty_tag ($smarty_tag)
{

  // Call a smarty tag.
  // Reference:
  // http://wiki.cmsmadesimple.org/index.php/User_Handbook/Admin_Panel/Extensions/User_Defined_Tags#How_to_Execute_Smarty_Tags_from_A_User_Defined_Tag_.28UDT.29

  global $gCms;

  /*
  echo '<p><strong>smarty_tag DEBUG</strong>:';
  echo "<br/>smarty_tag=$smarty_tag";
  echo '</p>';
  */

  $smarty = &$gCms->GetSmarty();
  $smarty->_compile_source('temporary template', $smarty_tag, $_compiled);
  @ob_start();
  $smarty->_eval('?>' . $_compiled);
  $_contents = @ob_get_contents();
  @ob_end_clean();
  echo $_contents;

}


// ----------------------------------------------------------------

function smarty_cms_selflink ($page, $text, $more)
{

  // Call the CMSMS tag {cms_selflink}.

  /*
  echo '<p><strong>smarty_self_link DEBUG</strong>:';
  echo "<br/>page=$page";
  echo "<br/>text=$text";
  echo "<br/>more=$more";
  echo '</p>';
  */

//  smarty_tag ("{cms_selflink page=$page" . ( (empty($text)) ? '' : ' text="' . $text .'"' ) . ' more="' . $more . '"}');

  smarty_tag ("{cms_selflink page=$page" . ( (empty($text)) ? '' : " text='$text'" ) . ' more="' . $more . '"}');

}


// ----------------------------------------------------------------

function valid_page($page_alias)
{

  // Is a page alias valid?

  // References:
  // http://forum.cmsmadesimple.org/index.php/topic,3778.msg20921.html#msg20921

  global $gCms;

  $manager =& $gCms->GetHierarchyManager();
  $node =& $manager->getNodeByAlias($page_alias);
  return ( $node != null );

}


// ----------------------------------------------------------------

function current_page_title()
{

  // Return the title of the current page.
  // This is needed only to print and inactive link to the current page when $show_title==true.

  // References:
  // http://forum.cmsmadesimple.org/index.php/topic,3778.msg20921.html#msg20921
  // http://forum.cmsmadesimple.org/index.php/topic,3778.msg21051.html#msg21051

  global $gCms;

  $this_page = $gCms->variables['content_id'];
  $manager =& $gCms->GetHierarchyManager();
  $node =& $manager->GetNodeById($this_page);
  $content =& $node->getContent();
  return $content->Name();

}


// ----------------------------------------------------------------
// Create the menu

/*
echo '<p><strong>DEBUG</strong>:';
echo "<br/>page_lang=$page_lang";
echo "<br/>show_title=$show_title";
echo "<br/>show_current=$show_current";
echo "<br/>page_title=$page_title";
echo "<br/>language_versions="; print_r($language_versions);
echo "<br/>language_buttons="; print_r($language_buttons);
echo '</p>';
*/

echo '<ul>';

foreach ( $languages as $language_code => $language )  // explore all possible languages of the site
{

  // $language_code = code of the language to create a link for
  // $language[DEFAULT_PAGE] = the default page used when no alternative has been defined for the language
  // $language[BUTTON] = HTML used as button for the link, when $show_title==false.

  /*
  echo '<p><strong>menu DEBUG</strong>:';
  echo "<br/>language_code=$language_code";
  echo "<br/>language="; print_r($language);
  echo '</p>';
  */

  if ( $language_code != $page_lang )
  {
    $alternative_page = $language_versions[$language_code];
    // echo "alternative_page=$alternative_page"; // DEBUG
    if ( valid_page($alternative_page) )
    {
      echo '<li>';
      // echo "ALTERNATIVE: $alternative_page"; // DEBUG
      smarty_cms_selflink($alternative_page,($show_title == true ) ? "" : $language[BUTTON], "hreflang='".$language_code."'");
      echo '</li>';
    }
    else // no alternative page is valid for this language
    {
      if ( $show_default == true and valid_page($language[DEFAULT_PAGE]) )
      {
        echo '<li>';
        // echo 'NO ALTERNATIVE! DEFAULT: '; // DEBUG
        smarty_cms_selflink($language[DEFAULT_PAGE],( ($show_title == true ) ? "" : $language[BUTTON] ) , "hreflang='".$language_code."'");
        echo '</li>';
      }
      else
      {
        if ( $show_inactive == true )
        {
          echo '<li class="inactive">';
          // echo 'DEFAULT CAN NOT BE USED! INACTIVE BUTTON: '; // DEBUG
          echo $language[BUTTON] . '</li>';
        }
      }
    }
  }
  elseif ( $show_current == true )
  {
    // echo 'SHOW CURRENT'; // DEBUG
    echo '<li class="currentpage">' . ( ($show_title == true) ? current_page_title() : $language[BUTTON] ) . '</li>';
  }
}

echo '</ul>';
Then configure the code: Change the content of the array $languages with the languages you use, their default home page (if any) and their default HTML code for buttons. The code is clear enough and it's fully commented.

Then put the new tag {language_menu} in the desired place of your template, e.g.:

Code: Select all

<div id='langmenu'>
{language_menu}
</div>
That code must be always after the "Other languages" content block (that's why in Step 5[/i ]we put that content block at the top of the template).

You can use optional parameters (they are fully explained in the code itself), e.g.:

Code: Select all

<div id='langmenu'>
{language_menu show_title='false' show_current='true' show_default='true' show_inactive='false'}
</div>
I kept some comments marked with the word "DEBUG" because they are useful while modifing the code. If you need, uncomment them; if you have no interest, delete them.

The end

That's all. I hope you find it useful. I'm using it in a new personal site of mine. I'll post the URL when there's enough content. It's a bilingual site. Its URL is http://alinome.net/alien.

Marcos
Last edited by alinome.net on Wed Aug 26, 2009 2:18 pm, edited 1 time in total.
Marcos Cruz
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Karolis wrote: It is fully enough to use {menu start_level="2"} ir all languages. You don't need to define separate menu for each language, unless you want to use different menu templates.
Sure? I'll explore that. The other day I read all {menu} parameters and after many tests I found the combination that works in my case:

Code: Select all

{if $page_lang == "es"}{menu template='menu_1' start_element='1.1' number_of_levels='4'}
{else}{menu template='menu_1' start_element='2.1' number_of_levels='4'}
{/if}
That's because the items I want to show in the menu (the main categories) are in one section header (categories), and the general content pages are all together in another one (pages). The reason is that one single page can belong to many categories at the same time... I'm implementing a method to assign non-hierarchical categories to pages, blog-like, that's my next project...  :) I need that too for my new site.

But I cann't understand that you need only one content menu for all languages. I thought a different start_element is needed.
Marcos Cruz
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Karolis wrote: {menu start_level="2"} is enough.
I don't really know how to explain this in simple words  just try and you'll see
There's something I don't understand: My content is one level deeper, as I told before, so I have to use {menu start_level='3'}. But then {menu} doesn't create the HTML tags and ! I have to put them in the template! I don't really understand how start_level works, but you are right: this way I need only one {menu} for all languages. Thanks.

I'll update the final implementation guide with this detail.

When possible, try the final version 5 of {menu_language}.
Marcos Cruz
sirjuh

Re: One more approach to build a multilingual site with the regular CMSMS

Post by sirjuh »

Hello,

It seems that array_combine() is a PHP5-only function. So to ensure PHP4 compatibility you should add this code in language_menu:

Code: Select all

//----------------------------------------------------------------------------
 // PHP 4 compatibility.
/**
 * Replace array_combine().
 *
 * Borrowed from PHP Compat 1.5 (http://pear.php.net/package/PHP_Compat) and 
 */
if (!function_exists('array_combine')) {
    function array_combine($keys, $values)
    {
        if (!is_array($keys)) {
            user_error('array_combine() expects parameter 1 to be array, ' .
                gettype($keys) . ' given', E_USER_WARNING);
            return;
        }

        if (!is_array($values)) {
            user_error('array_combine() expects parameter 2 to be array, ' .
                gettype($values) . ' given', E_USER_WARNING);
            return;
        }

        $key_count = count($keys);
        $value_count = count($values);
        if ($key_count !== $value_count) {
            user_error('array_combine() Both parameters should have equal number of elements', E_USER_WARNING);
            return false;
        }

        if ($key_count === 0 || $value_count === 0) {
            user_error('array_combine() Both parameters should have number of elements at least 0', E_USER_WARNING);
            return false;
        }

        $keys    = array_values($keys);
        $values  = array_values($values);

        $combined = array();
        for ($i = 0; $i < $key_count; $i++) {
            $combined[$keys[$i]] = $values[$i];
        }

        return $combined;
    }
}
Anyway, thanks a lot for your work: it has saved me migrating from CMSMS  to "something-else-multilang-ready"
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

sirjuh wrote: It seems that array_combine() is a PHP5-only function. So to ensure PHP4 compatibility you should add this code in language_menu:
Thank you very much. I searched in php.net for the function I needed but I didn't realize that it's PHP5-only.

I updated my main messages in this thread with the link of a personal bilingual site of mine where I use this method: http://alinome.net/alien. I would love to see implementation examples by other people.
Last edited by alinome.net on Sat May 17, 2008 9:22 am, edited 1 time in total.
Marcos Cruz
atired
Forum Members
Forum Members
Posts: 26
Joined: Mon May 21, 2007 8:37 pm

Re: One more approach to build a multilingual site with the regular CMSMS

Post by atired »

Your code worked just fine while I was testing it at a localhost. But when I uploaded to my web server I got an error message. As i´m just a designer, it took me a while to figure out the problem was the php version. Then I tried to paste the extra code (sirjuh's post) but it still wouldn't work. I finally got my web server to upgrade the php version. It all works wonderfully now.
Just one question: What would I have to do to have flag images instead of the language's names in the links buttons?
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

Hi atired, Karolis gave you the right answer. You see, it's easy.

There's only a little mistake with the quotes, and the array name in the last version of the code is $languages. Also, I propose a little change:

Code: Select all

$languages = array(
  "eo" => array("komenco", "<img xml:lang='eo' src='/uploads/images/eo.jpg' alt='esperanto' />"),
  "es" => array("inicio", "<img xml:lang='es' src='/uploads/images/es.jpg' alt='español' />") );
That way, the image text is clear for people surfing the net without images, and the language markup is kept.

A bit off topic about flags and languages in the web:

I think flags can be useful in some cases. For example, for a company that has branches in different countries. In that case the flags represent the countries. That's right, because flags represent "political territories", not languages.  But if you try to use country flags as language icons, most of the times you get problems. For example, in Spain there are several official languages and three of them are spoken in other countries too (France, Andorra and the Americas); the Spanish spoken in Argentina or Mexico is quite different from the variants spoken in Spain. English has many dialects too. Variants of German are widely spoken in several countries... The list in endless all over the world. Also, flags are very controversial in the web accesibility field.

That's why IMHO the best solution is to use the language name in the language itself.

Anyway, I'm glad the code is useful to you. May you post a link when your web is ready?
Last edited by alinome.net on Wed May 21, 2008 3:00 pm, edited 1 time in total.
Marcos Cruz
ZIGRIB
Forum Members
Forum Members
Posts: 20
Joined: Wed Apr 23, 2008 3:31 pm

Re: One more approach to build a multilingual site with the regular CMSMS

Post by ZIGRIB »

Good morning and thank you for your job

I try to use this functionality and result does not correspond to what it should be...
In effect, when I click on link language, I always meet on the homepage of the site.
It's ok for the home page but not for others pages
Can you help me to understand what does not go?
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

ZIGRIB wrote: when I click on link language, I always meet on the homepage of the site.
Did you fill the alternative languages field in every page form? I mean the 5th step of the Implementation guide. Please confirm that you filled that field in every page. Otherwise language links lead to the home page.
Last edited by alinome.net on Wed May 21, 2008 2:20 pm, edited 1 time in total.
Marcos Cruz
ZIGRIB
Forum Members
Forum Members
Posts: 20
Joined: Wed Apr 23, 2008 3:31 pm

Re: One more approach to build a multilingual site with the regular CMSMS

Post by ZIGRIB »

Yes I do it
User avatar
alinome.net
Forum Members
Forum Members
Posts: 124
Joined: Thu Jan 25, 2007 2:54 pm
Location: España / Hispanujo / Spain

Re: One more approach to build a multilingual site with the regular CMSMS

Post by alinome.net »

How did you configure the $language_buttons $languages array? May you show those lines? May you show an example of the 'Other languages' field for some pages too? Maybe there's a typo.
Last edited by alinome.net on Wed May 21, 2008 3:00 pm, edited 1 time in total.
Marcos Cruz
Post Reply

Return to “Tips and Tricks”