Articles » Administrator » Multi-Language templates
Author

Kim Schulz is a Fundanemt developer and hosts Fundanemt sites in his company DevTeam Denmark.

Website
E-mail

Multi-Language templates with Fundanemt

In the backend fundanemt is very well suited for multi-language sites, but
the frontend templates does however lack on documentation of how to actually use this.
In this article I will explain one way to get nice little flags on your site that makes it possible
to change the to other supported languages.

A couple of things has to be done to make this as smooth and automatically as possible.
The first thing we need to do is to figure out which languages we actually have pages created in:

<?php                       
$res 
$parser->D->dbQuery("SELECT DISTINCT LANG FROM ".TCONTENT." GROUP BY LANG");
$languages=array();
while(
$l $parser->D->dbFetchRow($res)){
    
array_push($languages,$l["LANG"]);
}
?>

The next thing we have to do is to figure out which IDs we should use for the frontpage in the different languages.
If you gave this info via the $frontpageIds array in the constructor for the $parser, then you could simply use this array.
That solution is however not as automatic as the solution proposed here.

<?php 
$parser
->frontpageIds=array(); //we remove whatever was defined in the constructor
foreach($languages as $lang){ 
   
//this is only a good guess 
   
$res $parser->D->dbQuery("SELECT ID FROM ".TINDEX." WHERE LANG='$lang' 
                                        AND X='1' ORDER BY Y ASC LIMIT 1"
);
   while(
$i $parser->D->dbFetchRow($res)){
        
array_push($parser->frontpageIds,$lang => $i["ID"]);
   }
}
?>

So now we have the an array called $parser->frontpageIds that could look somthing like:
$parser->frontpageIds = {"en"=>"10", "da"=>"1","it"=>"42"}

Now all we have to do is to create the actual links for the pages, and for this we have the id2url() method.

<?php
foreach($parser->frontpageIds as $lang=>$id){
   
$urls[$lang] = id2url($id$parser->lang
                              
$parser->php_self,
                              
$parser->TLID
                              
$parser->D
                              
$parser->frontpageIds);
}
?>

If we have some nice small graphical flags as png image files in a directory called /images/flags/ and each flag file
is named according to the language it represents (e.g., da.png, en.png, it.png etc), then we can easily create the list
og flag links:

<?php 
foreach($urls as $lang=>$url){
   print 
"<a href=\"$url\"><img src=\"/images/$lang.png\" style=\"border:0;\"/></a>";
}
?>

and thats actually it! We could put all of it together in a function to place in the index.php file, and it could look somthing like this:

<?php
function printFlags(){
   global 
$parser;
   
$res $parser->D->dbQuery("SELECT DISTINCT LANG FROM ".TCONTENT." GROUP BY LANG");
   
$languages=array();
   while(
$l $parser->D->dbFetchArray($res)){
    
array_push($languages,$l["LANG"]);
   }
   
$parser->frontpageIds=array(); //we remove whatever was defined in the constructor
   
foreach($languages as $lang){ 
      
//this is only a good guess 
      
$res $parser->D->dbQuery("SELECT ID FROM ".TINDEX." WHERE LANG='$lang'
                                      AND X='1' ORDER BY Y ASC LIMIT 1"
);
      while(
$i $parser->D->dbFetchArray($res)){
         
$parser->frontpageIds[$lang] = $i["ID"];
      }
   }
   foreach(
$parser->frontpageIds as $lang=>$id){
     
$urls[$lang] = id2url($id$parser->lang
                                
$parser->php_self,
                                
$parser->TLID
                                
$parser->D
                                
$parser->frontpageIds);
   }
   foreach(
$urls as $lang=>$url){
      print 
"<a href=\"$url\"><img src=\"/images/$lang.png\" style=\"border:0;\"/></a>";
   }   

}
?>

and now all you have to do is to call printFlags() whereever you want them.
Notice that this function is not optimied for speed but readability. Most of the loops could easily be joined into one single loop.