Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Multiple languages for a web using templates and regexp

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Tue May 27, 2003 3:07 pm    Post subject: Multiple languages for a web using templates and regexp Reply with quote

So, you have your website in a template system? Are you "replacing" all those template variables? Don't want to do that? You want to replace them dynamically without doing something like:
$mytpl->replaceVar("LANGUAGE_HELLO_WORLD",$arrLang["english"]["LANGUAGE_HELLO_WORLD"]);

These files take a template, and replace all the LANGUAGE_* template variables with the ones found in a CSV file. This is EXTREMELY fast, a lot faster than the normal replace routine (find and replace).

(Note: I haven't tested the following script, so you might want to fix some code to make it work :\ However, I'm using this setup and it works really great)


This is an example template file: (index.tpl)
----------------------------------------------------------------------
<html>
<body>
{LANGUAGE_HELLO_WORLD}
</body>
</html>


This is the language file, in CSV (lang.csv)
----------------------------------------------------------------------
LanguageID;english;spanish;deutsch;
LANGUAGE_HELLO_WORLD;Hello world;Hello world in spanish;Hello world in deutsch;


And here is the class file that does the trick (lang.class.php)
----------------------------------------------------------------------
<?
class language {
var $intLangID;
var $arrLangDB = array();

function language() {
global $langid;
$this->intLangID = $langid;

// Open the language file.
$arrLanguageFile = file("lang.csv");
array_shift($arrLanguageFile);


// Loop it and create another array.
for($intA = 0; sizeof($arrLanguageFile) > $intA; $intA++) {
$arrThis = split(";",$arrLanguageFile[$intA]);
if(sizeof($arrThis) > 2) {
$strVarName = $arrThis[0];
array_shift($arrThis);
$arrLang["".$strVarName.""] = $arrThis;
}
}

$this->arrLangDB = $arrLang;
}

function getText($arrLangKey) {
$strLangKey = "LANGUAGE_".$arrLangKey[1];
return $this->arrLangDB["".$strLangKey.""][$this->intLangID];
}

function implementLanguage($res) {
$strText = preg_replace_callback('/{LANGUAGE_(.*?)}/',array($this,'getText'),$res);
return $strText;
}
}

?>


All you need to do in your template system is to call this when reading/parsing the template, e.g.:

$res = implode('',file('index.tpl'));
$res = $GLOBALS["objLanguage"]->implementLanguage($res); //make sure you have created the object objLanguage ($objLanguage = new language()Wink








That's it....i'm sure I might have simplified it...but hey, you'll learn something! Very Happy
Back to top
View user's profile Send private message
appel
Smarty Rookie


Joined: 27 May 2003
Posts: 29

PostPosted: Tue May 27, 2003 4:20 pm    Post subject: Or better, in smarty... Reply with quote

The following will do the trick also.
<?
$arrLang["English"]["LANGUAGE_HELLO_WORLD"] = "Hello world";
$arrLang["Danish"]["LANGUAGE_HELLO_WORLD"] = "Hallo world";
$arrLang["Icelandic"]["LANGUAGE_HELLO_WORLD"] = "Halló heimur";

// passing an associative array
$smarty->assign($arrLang["".$somedynamiclangvar.""]);
?>
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP