4 Replies - 2308 Views - Last Post: 30 April 2009 - 04:40 PM Rate Topic: -----

#1 Stelios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-September 08

How To Select Template Based On Language Switch? (PHP Code For Jooml

Posted 01 April 2009 - 04:28 PM

Hi,

here is the problem that I face, with Joomla 1.5.x and the extension Joom!Fish: I have assigned a different template (as the default) to a few menu-links in Joomla 1.5.x, and I want to use Joomfish to change this template by another, when choosing a different language in the front-end of this template. As I can understand it, this can only be achieved by altering the index.php file of the above template.

The facts are:

1). In Joomla 1.0.x the solution is obvious, adding the following lines in the index.php (so that every template would be named by its standard name and a "-en" or "-de" etc. etc.):

// gets template for page
$cur_template = $mainframe->getTemplate();
// add this
if( $mosConfig_multilingual_support ) {
$cur_template .= "-". $iso_client_lang;
}
else {
$cur_template .= "-en";
}



2). But If you are using Joomla 1.5.x and want to switch templates this
method will not work as the variable $mosConfig_lang is not valid to joomla 1.5.x.In a simple free plug-in for Joomla 1.5.x, that assigns to every language selected in Joomfish a different template to and for the whole website, the php code is:

<?php
jimport( 'joomla.plugin.plugin' );
class plgSystemEXAMPLE_jf_template extends JPlugin
{
function plgSystemEXAMPLE_jf_template(& $subject, $config)
{
parent::__construct($subject, $config);
}
function onAfterRoute(){
global $option, $mainframe;
if ($mainframe->isAdmin()) {
return; // Dont run in admin
}
$registry =& JFactory::getConfig();
$jfLang = $registry->getValue("joomfish.language", false);
if (!$lang) {
return;
}
$str_config = $this->params->get('template_config', '');
$str_config = preg_replace('/\s/', '', $str_config);
if (preg_match('/'. $lang->shortcode . '=>(((.*?),)|((.*?)$))/', $str_config, 

$match)) {
$newTemplate = $match[1];
$newTemplate = preg_replace('/,/', '', $newTemplate);
$mainframe->set('setTemplate', $newTemplate);
}
}
}



(You can then in the back-end select and assign the templates for each language like this e.g. en=>ja_purity (i.e. <params><param
name="template_config" type="" size="" default="" label="" description="shortcode=>template_code, separate by ','. EXAMPLE: en=>beez,de=>rhuk_milkyway,fr=>ja_purity" /></params>)

I tried to change and modify the above code in a way that it could be
added directly into the index.php of a joomla template, but to no avail till
now since I am a novice at such things like Joomla plug-ins and not so
good in PHP.

Can someone with more php experience help and propose a solution on
how to modify this code, so that it can be added directly in a php file (i.e.
in the index.php file of a Joomal 1.5.x template) instead of being used
only as a Joomla plug-in (but in this way someone would be able to assign
to and for a specified template, different other templates for each language selected with Joomfish).

I know that someone must be familiar with Joomla to give me some help,
(maybe he can propose also some other php lines which adding to the
index.php file of Joomla 1.5.x template could help to switch templates
according to language selected with the Joomla extension Joom!Fish)

Thanks for any help in advance

Styl

Is This A Good Question/Topic? 0
  • +

Replies To: How To Select Template Based On Language Switch? (PHP Code For Jooml

#2 Stelios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-September 08

Re: How To Select Template Based On Language Switch? (PHP Code For Jooml

Posted 03 April 2009 - 12:54 PM

Hi,

I tried to make also a workaround to the above problem with a cookie, but Joomla 1.5.x has still some bugs and does not work as it should!

E.g. I established in a menu (e.g. in example pages) a menu link pointing to the external link: a page in my joomla (like http://www.yoursite.com/index.php option=com_content&view=article&id=19&Itemid=27〈=de) and did then assign from the back-end to this menu link a different template (e.g. beez). When I call this link then the default template does not change to the assigned template (here beez)!?!? Therefore some more improvements to Joomla 1.5.9 should be made, since it still does not work always properly, fixing some bugs!

A selected template in the back end for a menu link (pointing to an external link) does not change the default template when you call this link, the link works alright but with the default template!!!

Anyone can test that Joomla 1.5.x does not work properly (and in this direction it must be upgrated to solve among others also this problem): Just go to the Menu Item Manager, choose menu Example Pages, then New and then External Link, give there any title you want e.g. EXPERIMENT and as link give e.g. the link to the page in the main menu e.g. Joomla! Overview (from the integrated sample data)(URLofYourSite/joomla159/index.php?option=com_content&view=article&id=19&Itemid=27〈=en), choose Save, go now to the Template Manager, choose now a different template as the default that you have, e.g. beez and click Edit, then choose Select from List (Menu Assignment) and in the Menu Selection choose the created menu link from the menu ExamplePages EXPERIMENT (hold down the Ctrl key and choose also another link e.g. Section Blog for testing!), click save and then Preview; in your home site you can now choose from the menu Example Pages the link that you have created EXPERIMENT, as you can see the template does not change to beez as you do so!?!?!?, on the contrary if you choose the link Section Blog the template does change to beez...

Has anyone any ideas on how to fix this bug in Joomla 1.5.9 (1.5.x)

Regards, Styl

PS: If Joomla where an European project, then something like Joom!Fish would have been integrated in the core functions of Joomla from the beginning, but still as the saying goes "one nation, one planet", the Joomla team should therefore consider and do that anyway in the future upgrades of Joomla...
Was This Post Helpful? 0
  • +
  • -

#3 Stelios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-September 08

Re: How To Select Template Based On Language Switch? (PHP Code For Jooml

Posted 06 April 2009 - 01:52 PM

Hi again,

Here is some kind of a solution to the above problem, so that someone can do stuffs according to the current language selected. In this way someone could be able to differentiate our template in respect to the each language every time in use - but still the big problem remains: how to assign to each template a different one, for each language selected, adding the appropriate code in the index.php, I hope that somebody will at the end help and solve this problem???

What someone needs to do now in his template is determine if he is on the language in use specific page and make the necessary changes to the template based on which type of page he is on...

here is some code that can help with this:

	$config = &JFactory::getConfig();
	$lang = $config->getValue('language');
	// Predifine this early in the head section
	if ($lang == 'de-DE') {
	  //do the german language stuff
	} else {
	  // do the other languages page stuff
	}




We can use so the "if blocks" where the code between a selected language page and the rest languages page shall be different. For example, lets say that our German language page template and our other language page is the same except that the German language page doesn't have a left column, plus the English language page has a 'picture' position that has a image which comes somewhere under the header. In this case the exact code will be as follows:

in the head section of the template add:

	<?php
		  $config = &JFactory::getConfig();
		  $lang = $config->getValue('language');
	?>




and in the body section add this lines where the code should differ:

	<?php
	   if ($lang != 'de-DE') :  // If the current language is not german include the right column ?>
	   <div id="rightcolumn">
	   <jdoc:include type="modules" name="right" style="xhtml" />
	   </div>
	   <?php endif; ?>
	   ...   
	   <?php
	   if ($lang == 'de-DE') : Include in the german language page the 'picture' image module ?>
	   <div id="picture">
	   <jdoc:include type="modules" name="picture" style="xhtml" />
	   </div>
		<?php endif; ?>
		...




This brief step by step tutorial would have been incomplete without some sample page included? For example, the code of the template that follows has inside some "if blocks" to do separate stuff according to the language in use (or if we are in our home or interior pages, or in a particular component type page etc).

	<?php
	/**
	* Example template code (with two user collapsable positions etc. implementing if blocks for language selected
	* or component in use, or if we are in the home or interior pages ect.
	* .........................
	*/

	// no direct access
	defined('_JEXEC') or die('Restricted access');
	?>
	<?php echo '<?xml version="1.0" encoding="utf-8"?' .'>'; ?>
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="<?php echo $this-

	>language; ?>" lang="<?php echo $this->language; ?>" dir="<?php echo

	$this->direction; ?>" >

	<head>
	<meta http-equiv="Content-Type" content="text/html; <?php echo _ISO; ?>"

	/>
	<?php
	   $pageoption = JRequest::getVar('option', '');
	   $pageview = JRequest::getVar('view', '');
	   $config = &JFactory::getConfig();
	   $lang = $config->getValue('language');
	?>

	   <jdoc:include type="head" />

	   <link href="/templates/<?php echo $this->template; ?

	>/css/template_css.css" rel="stylesheet" type="text/css" />
	</head>
	<body>
	<div id="container">
	  <div id="header">
		 <div id="banner">
		 <a href="/"><img src="/templates/<?php echo $this->template; ?

	>/images/logos/logo.png" alt="Home" border="0" align="left" id="logo"

	/></a>
		<jdoc:include type="modules" name="banner" style="xhtml" />
		<jdoc:include type="modules" name="header" style="xhtml" />
		 </div>
		 <?php if ($this->countModules('top')) : ?>
				<div id="top-module">
				   <jdoc:include type="modules" name="top" style="xhtml" />
				</div><!-- end top-module -->
				<?php endif; ?>
		 </div> <!-- End header -->
	<div id="topnav">
	   <jdoc:include type="modules" name="topnav" style="xhtml" />
	  </div> <!-- End topnav -->
	<!-- HERE IS FOR EXAMPLE THE IF BLOCK to include the 'picture' position that has an image which comes right under the header only if the selected language is English -->
	<?php if($lang == 'en-GB'): ?> :  // If the selected language is English, so include the 'picture' image module ?>
		<div id="picture">
	   <jdoc:include type="modules" name="feature" style="xhtml" />
		</div>
	  <?php endif; ?>
		  <div id="main">
			<div id="content">
	<!-- HERE IS FOR EXAMPLE THE IF BLOCK don't show the breadcrumbs on the extension community builder type pages -->
		 <?php if ($pageoption != 'com_comprofiler') : // Don't show the breadcrumbs on the community builder particular component type pages ?>
		  <div id="breadcrumbs">
			<jdoc:include type="modules" name="breadcrumbs" />
		  </div><!-- end breadcrumbs -->
		 <?php endif; ?>
			  <div id="mainbodby">
		 
				<div id="top-user-modules">
				<?php if ($this->countModules('user1')) : ?>
				<div id="user1">
				<jdoc:include type="modules" name="user1" style="xhtml" />
				</div><!-- end user1 -->
				<?php endif; ?>
				<?php if ($this->countModules('user2')) : ?>		   

				<div id="user2">
				<jdoc:include type="modules" name="user2" style="xhtml" />   
				</div><!-- end user2 -->
				<?php endif; ?>		 
				 </div><!-- end top-user-modules -->
			   
			  <jdoc:include type="component" />
			  </div><!-- end mainbody -->


	<!-- HERE IS FOR EXAMPLE THE IF BLOCK to show the left column only if the language in use is the e.g. the German language -->
		  <?php if($lang == 'de-DE'): ?>
		   <div id="leftcolumn">
		 <jdoc:include type="modules" name="left" style="xhtml" />
			 </div> <!-- End leftcolumn -->
		<?php endif; ?>

	<!-- HERE IS THE FOR EXAMPLE IF BLOCK to show the right column only on the home page -->
		 <?php if ($pageview == 'frontpage') : ?>
			   <div id="rightcolumn">
		  <jdoc:include type="modules" name="right" style="xhtml" />
			   </div> <!-- End rightcolumn -->
	   <?php endif; ?>
			 
		  </div>
		</div><!-- end BodyContent -->
	<div id="footer" class="clearfix">
			   <?php if ($this->countModules('footer')) : ?>
			 <div class="inside">
				   <jdoc:include type="modules" name="footer" style="xhtml" />
				</div><!-- end inside -->
				<?php endif; ?>
			</div><!-- end footer -->
		   </div><!-- end page -->
		<?php if ($this->countModules('debug')) : ?>
		<div id="debug"><jdoc:include type="modules" name="debug" /></div>
		<?php endif; ?>
	</body>
	</html>




The template code above should give us an idea, on how to go about in changing the template for each language in use (or the home or interior pages or particular component type pages).

I hope that someone will help and figure the needed code out, on how to assign to a specific template a different one for each language selected with Joom!Fish.

Regards,

Styl
Was This Post Helpful? 0
  • +
  • -

#4 Stelios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-September 08

Re: How To Select Template Based On Language Switch? (PHP Code For Jooml

Posted 16 April 2009 - 02:28 PM

Hi,

I figured finally out also how to assign for each language selected a different template... So, if you want to create a site with a totally different template for each language selected, then you need to call the templates by using PHP and if conditions in a "fake template". The logic here
is that you must have already defined a default template in your database; in the index.php file of this default template you must also set the right conditions like this:


If the language selected is English include once the file english-template/index.php

\/ (If the answer is NO then continue
\/ with the else statement)
\/
If the language selected is German include once the file german-template/index.php

\/ (If the answer is NO then continue
\/ with the else statement)
\/
If the language selected is French include once the file french- template/index.php

\/
\/
\/
.................


In other words, the fake template will have only this (index.php code) inside:

<?php
  $config=&JFactory::getConfig();
  $lang=$config->getValue('language');
if ($lang=="en-GB")
include_once("english-template/index.php");
else 
if ($lang=="de-DE")
include_once("german-template/index.php");
else
if ($lang=="fr-FR")
include_once("french-template/index.php");
....
?>



As example, if we use for the english language the beez template, for the german language the rhuk_milkyway template, and for the french language the ja_purity template, then your fake template will just say this:

<?php
  $config=&JFactory::getConfig();
  $lang=$config->getValue('language');
if ($lang=="en-GB")
include_once("beez/index.php");
else 
if ($lang=="de-DE")
include_once("rhuk_milkyway/index.php");
else
if ($lang=="fr-FR")
include_once("ja_purity/index.php");
?>



In this example, to be more analytical, I have created in the htdocs/template a template file (e.g. with the name fake-template) and copied in this template file the whole template files of the above three templates.
So when you choose as default template the fake-template, then your default template will be beez, and when you choose then the german language, the template will change to rhuk_milkyway and when you select french the template turns to ja_purity. Of course, in this example, in the two of this templates you can select parameters in the back end, therefore we have to change the index.php code of this templates accordingly, since the parameters can only be called if the specific template is active as a default one in joomla (but obviously our default
template remains always the fake-template). Furthermore you must correct also the path for the css etc. files (except if you have this templates also as separate ones in your templates file, then
the css ect. files can be loaded directly from there). For the rhuk_milkyway the code correction is the following:

This code in the index.php:

...
<head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" 
type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" 
type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/rhuk_milkyway/css/template.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/rhuk_milkyway/css/<?php 
echo $this->params->get('colorVariation'); ?>.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/rhuk_milkyway/css/<?php 
echo $this->params->get('backgroundVariation'); ?>_bg.css" type="text/css" />
<!--[if lte IE 6]>
<link href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/css/ieonly.css" rel="stylesheet" type="text/css" />
<![endif]-->
<?php if($this->direction == 'rtl') : ?>
	<link href="<?php echo $this->baseurl ?>/templates/rhuk_milkyway/css/template_rtl.css" rel="stylesheet" type="text/css" />
<?php endif; ?>
</head>
<body id="page_bg" class="color_<?php echo $this->params->get('colorVariation'); ?> bg_<?php 
echo $this->params->get('backgroundVariation'); ?> width_<?php echo $this->params->get
('widthStyle'); ?>">
...



must be changed to this code (if you want to choose the white variation and the template width medium (i.e. 950px):

...
<head>
<jdoc:include type="head" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/system.css" 
type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/system/css/general.css" 
type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/fake-template/rhuk_milkyway/css/template.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/fake-template/rhuk_milkyway/css/blue.css" type="text/css" />
<link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/fake-template/rhuk_milkyway/css/blue_bg.css" type="text/css" />
<!--[if lte IE 6]>
<link href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template ?>/fake-template/css/ieonly.css" rel="stylesheet" type="text/css" />
<![endif]-->
<?php if($this->direction == 'rtl') : ?>
	<link href="<?php echo $this->baseurl ?>/templates/fake-template/rhuk_milkyway/css/template_rtl.css" rel="stylesheet" type="text/css" />
<?php endif; ?>
</head>
<body id="page_bg" class="medium_small">
...



I hope that the above codes will give an idea on how to go about in changing the templates for the language each time in use...

Regards, Styl


PS: To my earlier post about the "if blocks" I have to add that it is obvious that someone can use "and" & "or" statements to do more complicated stuff, even integrate a whole template... For example, you can write the if blocks like this:

1)
<?php if($lang == 'de-DE or da-DK or fr-FR or ru-RU'): // You can do stuff which will appear only in this four languages: German, Danish, French and Russian ?>



or like this

2)
<?php if($lang == 'de-De' and $pageview != 'frontpage' and $pageoption == 'com_comprofiler'): 
?>: // You can do stuff which
will appear only in the German language selection and only in the interios pages, and if it is community builder particular component type page ?> 



or

3) if you want to have a collapsible e.g. left column which should show only if the selected language is English, then for example in the template ja_purity the code should be modified to:

<?php if ($this->countModules('right') and $lang == 'en-GB'): ?>
		<!-- BEGIN: RIGHT COLUMN -->
		<div id="ja-col2">
			<jdoc:include type="modules" name="right" style="jarounded" />
		</div><br />
		<!-- END: RIGHT COLUMN -->
		<?php endif; ?>



4) you can even integrate a whole template in the if blocks (if you don't want to use a fake template), like this for the english, german and other languages:

...
<head>
...
<?php
if ($this->language=="en-GB"): ?>
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_en.css" type="text/css" />
<?php else 
 if ($this->language=="de-DE"): ?>
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_de.css" type="text/css" />
<?php else 
 if ($this->language=="..-.."): ?>
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_OTHERlanguage.css" type="text/css" />
<?php } endif; ?> 
...
</head>
<body>
...
<?php if ($lang == 'en-GB': //You can put the php body code of your template for the english language which has the template_en.css stylesheet
?>
<?php if ($lang == 'de-DE': //You can put the php body code of your template for the german language which has the template_de.css stylesheet
?>
<?php if ($lang == '..-..': //You can put the php body code of your template for the .. language which has the template_OTHERlanguage.css stylesheet
?>
...
</body>
...


Was This Post Helpful? 0
  • +
  • -

#5 Stelios  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-September 08

Re: How To Select Template Based On Language Switch? (PHP Code For Jooml

Posted 30 April 2009 - 04:40 PM

Hi,

in my earlier post I have mistyped and the following corrections should be made therefore:

The correction for the rhuk_milkyway code is obviously for the blue (background/color) variation (if you want the white variation you have to change the files from blue.css and blue_bg.css to white.css and white_bg.css, or if you want blue background and white color then change to
white.css and blue_bg.css), also for template width you have to correct the code to <body id="page_bg" class="width_medium"> or if you want the "fluid with maximum" one (min-width 750px and max-width 1050px) then change the class to "width_fmax", also correct the path in the IE 6 hack... etc.

In my earlier post in PS: 4) it is obvious also that you should put all your template body code in <?php if ($lang == 'en-GB': // Put here all the body code of your template for the english language ?>...<?php endif; ?> etc., and obviously the head section should be written with the correct placing of "{", etc., so the head/body sections to function properly must be corrected to:

...
<head>
<?php
$config=&JFactory::getConfig();
$lang=$config->getValue('language'); 
 if($lang == 'en-GB') { ?>
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_en.css" type="text/css" />
<?php } else  
 if($lang == 'de-DE') { ?>
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_de.css" type="text/css" />
<?php } else  
if($lang == '..-..') { ?>
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_OTHERlanguage.css" type="text/css" />
<?php } ?> 
</head>

<body>
<?php if ($lang == 'en-GB': //You can put in this if block the php body code of your template for the english language which has the template_en.css stylesheet ?>
...
<?php endif; ?>
<?php if ($lang == 'de-DE': //You can put in this if block the php body code of your template for the german language which has the template_de.css stylesheet ?>
...
<?php endif; ?>
<?php if ($lang == 'fr-FR': //You can put the php body code of your template for the .. (OTHER) language which has the template_OTHERlanguage.css stylesheet ?>
...
<?php endif; ?>
</body>



or, if you prefere, you can write the head section simply with separated if blocks like this:

<head>
<?php if($lang == 'en-GB'): ?> 
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_en.css" type="text/css" />
<?php endif; ?>
<?php if($lang == 'de-DE'): ?> 
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_de.css" type="text/css" />
<?php endif; ?>
<?php if($lang == '..-..'): ?> 
<link rel="stylesheet" href="/<?php echo $this->baseurl ?>/<?php echo $this->template ?>/css/template_OTHERlanguage.css" type="text/css" />
<?php endif; ?>
</head>



I hope this corrections will help

Regards, Styl


PS: It is obvious that you have to create more than one fake template if you want to assign different (as the default) templates to menus and/or links, sections or categories etc..., and in such a case you can assign to your second (or third or whatsoever) fake template whatever other templates you wish for each language selected with Joom!Fish..., something impossible if should use the above mentioned plugin...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1