Forums / Developer / Example of code for one flag picture for translation

Example of code for one flag picture for translation

Author Message

Mahmoud BECHAA

Thursday 09 April 2009 3:17:57 am

Hi there,

As I saw that on many forums people were asking about how to display flag picture instead of language name label, I thought it was good to put here an example of code to display just on flag if, for example your site has only two languages contents (for example French and English). With this piece of code, you'll be able to display an English flag when you're on a French content and a French flag when you're on an English content offering you the possibility to switch to the other language translation:

The following has to be inserted in the pagelayout.tpl :

at the beginning of the pagelayout.tpl I defined a variable which will get the right part (on 3 positions) of the current language code (for example if we are on a French content, the variable $langue will be equal to "fre-FR" and the variable $indice_langue will be equal to "fre" (you'll see why, below in this piece of code).

{def $langue = ezini( 'RegionalSettings', 'Locale', 'site.ini' )}

{set $indice_langue = $langue|extract_left(3)}

then I modify the div named "languages" which you find in the default pagelayout.tpl to make it fit the following


<div id="languages">
    {* we read all available translation values (labels, codes of translations) of the site      *}

        {def $locales=fetch( 'content', 'translation_list' )}
<ul>        
{foreach $pagedesign.data_map.language_settings.content.rows.sequential as $row}
        {def $site_url = $row.columns[0]
         $language = $row.columns[2]}
   <li>
{* 
if the current page language is not equal to the current read value of language translation 
we display the flag of the language. It will do the job, as if I'm on a French content and the current value of translation is English, we will display the English flag.
*}
                         {if ne($row.columns[1],$indice_langue)}
 
   <a href="{concat( "http://", $site_url,"/",
                         $DesignKeys:used.url_alias
                         )}">
{* my pictures are named flag_fre.gif and flag_eng.gif   *}

       <img src={concat('flag_', $row.columns[1], '.gif')|ezimage()} alt="{$language}" title="{$language}"/>
      </li>
	   {/if}
 
{/foreach}

</ul>
    </div>

If you have any suggestion concerning this please fill free to write.

Best regards.

Yannick Komotir

Thursday 09 April 2009 7:26:04 am

hi,

why don't use the native code ?

{def $site_languages=ezini('RegionalSettings', 'SiteLanguageList')}
{def $locales=fetch('content','locale_list')}
{foreach $site_languages as $lang}
        {foreach $locales as $locale}
                 {if eq($locale.locale_code, $lang)}
                 <a href={concat('index.php/',$locale.language_code|downcase())|ezroot}>
       <img src={$locale.locale_code |flag_icon} alt="{$locale.language_name}" title="{$locale.language_name}"/>&nbsp;
   </a>
                     {break}
                 {/if}
        {/foreach}
{/foreach}

<|- Software Engineer @ eZ Publish developpers -|>
@ http://twitter.com/yannixk

Mahmoud BECHAA

Thursday 09 April 2009 8:45:07 am

Yes Yannick,

Your code is cleaner and easier. I didn't try it but I think that it should work fine. I'll make a try as soon as possible. Thank you for this suggestion.

I tried also to preserve, as much as possible, the default code of pagelayout.tpl template page (a newbie reflex).

Yours Sincerely.

Mahmoud BECHAA

Friday 10 April 2009 3:26:04 am

After testing Yannick's Code, I finally kept my initial code as it was not really what I wanted to do. Yannick's code scans the whole language possibilities of EZP and displays flags for all languages (Catalan, Spanish, Chinese, Japanese, English, Canadian...). It was not really what I wanted to do. My aim was to display just One flag (on the two translation languages which are avalaible on my site, see my first post upper in this page).

Changing Yannick's code to something a bit lighter, could do the job except for language_name to be displayed as tip which, for the moment, I did'nt find an easy way to get it passing the equivalent locale_code. So, if someone has some advice concerning this ...

Here is the code which I tested and works correctly (except for what I said) :


<div id="languages">

         {def $langue = ezini( 'RegionalSettings', 'Locale', 'site.ini' )}
         {def $site_languages=ezini('RegionalSettings', 'SiteLanguageList')}




{foreach $site_languages as $lang}


                {if ne($langue, $lang)}
                <a href={concat('index.php/',$lang|extract_left(3)|downcase())|ezroot}>
		      <img src={$lang |flag_icon} alt="{$lang}" title="{$lang}"/>&nbsp;

                 </a>


                    {break}


                {/if}




{/foreach}

</div>