... Elxis version 2009.3 codename Aphrodite is out, download it from Elxis Download Center (EDC) ...

Change logo depending on language

From Elxis Official Documentation

Jump to: navigation, search
Translations Translations: 

English  • Ελληνικά


Some situations may require you to switch logo image depending on selected language. In such case we have multiple solutions to choose from. They are all based on $lang variable. We are going to explore most common cases. Following are some ideas.

Contents

Load specific image based on selected language

Suppose we have mockup like this in our template:

<div id="logo"><img src="images/logo.png" alt="logo" /></div>

Here what we can do:

<div id="logo"><?php
global $lang;
if ($lang == 'english') {
$logo = 'images/english-logo.png';
} else {
$logo = 'images/greek-logo.png';
}
 
echo '<img src="'.$logo.'" alt="logo" />';
 
?></div>


Apply specific background image to selected div

We could have simple mockup like this, and define logo image as background image in template's CSS:

<div id="logo"></div>

In this case, we can add this code inside head (<head></head>) tag. This way we change ONLY background property of given element, supposing you have rest of properties like width, height etc already defined in template's CSS.

<?php
global $lang;
if ($lang == 'english') {
$logo = 'images/logo_english.jpg';
} else {
$logo = 'images/logo_serbian.jpg';
}
 
echo '<style type="text/css">
div.logo {background: url('.$logo.') left top no-repeat}
</style>';
?></head>


Apply different CSS files according to selected language

Again, we will add some code to the head element. This way we can even totally change the look of the template!

<?php
global $lang;
if ($lang == 'english') {
$style = '<link href="templates/your_template/css/english.css" rel="stylesheet" type="text/css" media="all" />';
} else {
$style = '<link href="templates/your_template/css/russian.css" rel="stylesheet" type="text/css" media="all" />';
}
 
echo $style;
?></head>

There are many uses of $lang variable, so we encourage you to explore them.


A better approach

Here is a better way to work with language based files, css, images, etc. This method uses a validator and can work in any site (copy-paste solution).

Let's say we want to change an image (f.e. a logo) depending on the currently selected language. Our logo images are named like this: logo_english.png, logo_greek.png, logo_italian.png, etc.
In Elxis the currently selected language is stored in the global variable $lang.

<?php 
//make sure some global Elxis variables are accessible
global $lang, $mainframe;
 
//the absolute path to the directory where the language specific logos are located.
$logodir_abs = $mainframe->getCfg('absolute_path').'/images/logos';
 
//the URL to the directory where the language specific logos are located.
$logodir_url = $mainframe->getCfg('live_site').'/images/logos';
 
 
if (file_exists($logodir_abs.'/logo_'.$lang.'.png')) { //check current language
	$logo = $logodir_url.'/logo_'.$lang.'.png';
} else if (file_exists($logodir_abs.'/logo_'.$mainframe->getCfg('lang').'.png')) { //check default language
	$logo = $logodir_url.'/logo_'.$mainframe->getCfg('lang').'.png';
} else { //logo not found so use the english one
	$logo = $logodir_url.'/logo_english.png';
}
 
//show the logo
echo '<img src="'.$logo.'" alt="logo" border="0" />';
?>


Using XML parameters (2009.1 and newer)

You can use XML parameters in your template to do they same task and also give the final user the option to easily change the logo from the administration console without touching the code. You can see a sample implementation of this method in the screen shot bellow.

Change logo depending on language using XML parameters
Change logo depending on language using XML parameters

PHP code on your template's index.php file to get the logo from the parameters:

$logo = 'logo.png';
if (($tplparams->get('lang1') == $lang) && ($tplparams->get('logo1') != '')) {
	$logo = $tplparams->get('logo1');
} else if (($tplparams->get('lang2') == $lang) && ($tplparams->get('logo2') != '')) {
	$logo = $tplparams->get('logo2');
} else if (($tplparams->get('lang3') == $lang) && ($tplparams->get('logo3') != '')) {
	$logo = $tplparams->get('logo3');
}
if (!file_exists($mainframe->getCfg('absolute_path').'/templates/'.$mainframe->getTemplate().'/logos/'.$logo)) {
	$logo = 'logo.png';
}

Related articles:
How to create an XML parameter displaying all languages
Template parameters

Personal tools