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>   




eZ debug

Timing: Jan 18 2025 04:54:15
Script start
Timing: Jan 18 2025 04:54:15
Module start 'content'
Timing: Jan 18 2025 04:54:16
Module end 'content'
Timing: Jan 18 2025 04:54:16
Script end

Main resources:

Total runtime0.8610 sec
Peak memory usage4,096.0000 KB
Database Queries197

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0055 587.8594180.8125
Module start 'content' 0.00550.6921 768.6719571.2500
Module end 'content' 0.69760.1633 1,339.9219340.7422
Script end 0.8609  1,680.6641 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00390.4517210.0002
Check MTime0.00150.1688210.0001
Mysql Total
Database connection0.00050.063210.0005
Mysqli_queries0.773889.87271970.0039
Looping result0.00230.26371950.0000
Template Total0.833196.820.4166
Template load0.00210.247420.0011
Template processing0.831096.520620.4155
Template load and register function0.00010.014510.0001
states
state_id_array0.00120.142310.0012
state_identifier_array0.00110.132320.0006
Override
Cache load0.00180.2091300.0001
Sytem overhead
Fetch class attribute can translate value0.00140.164430.0005
Fetch class attribute name0.00160.183360.0003
XML
Image XML parsing0.00440.511030.0015
class_abstraction
Instantiating content class attribute0.00000.002180.0000
General
dbfile0.01211.4003330.0004
String conversion0.00000.000830.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
4content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
4content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
6content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 20
 Number of unique templates used: 6

Time used to render debug report: 0.0002 secs