Re: Translation

From: Date: Fri, 22 Mar 2002 15:32:05 +0000
Subject: Re: Translation
References: 1  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-3281@lists.php.net to get a copy of this message
These are my dirty database-driven multi-language translation plugins : The {tr} function is called that way : {tr t="Hello"} If lang_from is not specified, it's supposed to be english. If lang_to is not specified, it's supposed to be french in my case. So this function will look in the database and return "Bonjour". If the translation is not found, "Hello" will be left. I've also written a "translate" modifier that can be used that way: {a_smarty_function option=$STRING_TO_BE_TRANSLATED|translate} or that way : {html_options options=OPTIONS|@translate selected=$SELECTED_ITEM} As you can see, it's very useful to translate each item of an array. If you have any question... Dominique Peretti. http://www.lachoseinteractive.com Here is the structure of the MySQL table : CREATE TABLE translation ( expr_from varchar(255) NOT NULL default '', expr_to varchar(255) NOT NULL default '', lang_from tinyint(1) NOT NULL default '0', lang_to tinyint(1) NOT NULL default '1', id int(10) unsigned NOT NULL auto_increment, PRIMARY KEY (id), KEY expr_from (expr_from) ) TYPE=MyISAM; # # Dumping data for table translation # INSERT INTO translation VALUES ('Name', 'Nom', 0, 1, 1); <? function smarty_function_tr() { // $t, $lang_from $lang_to $html_result = ''; extract(func_get_arg(0)); if(!isset($lang_from)) $lang_from = 0; $languages = array('fr'=> 1, 'en'=>0); $lang_from = $languages[$lang_from]; $lang_to = $languages[AH_ADMIN_LANG]; global $db_data, $host, $user, $pass; $ide = mysql_connect($host, $user, $pass) or die("DomCMS : couldn't connect to MySQL"); mysql_select_db($db_data, $ide) or die("DomCMS : can't find database"); $query = "SELECT expr_to FROM translation WHERE lang_from = '$lang_from' AND lang_to = '$lang_to' AND expr_from='".AddSlashes($t)."'"; $result = mysql_query($query, $ide) or die(mysql_error()); $r = mysql_fetch_array($result); if($r) { $html_result = $r['expr_to']; } else { $html_result = $t; } print $html_result; } ?> <? function smarty_modifier_translate($t, $lang_from= 0) { $languages = array('fr'=> 1, 'en'=>0); $lang_from = $languages[$lang_from]; $lang_to = $languages[AH_ADMIN_LANG]; global $db_data, $host, $user, $pass; $ide = mysql_connect($host, $user, $pass) or die("DomCMS : couldn't connect to MySQL"); mysql_select_db($db_data, $ide) or die("DomCMS : can't find database"); if(is_array($t)) { $arr = array(); foreach($t AS $key => $item) { $query = "SELECT expr_to FROM translation WHERE lang_from = '$lang_from' AND lang_to = '$lang_to' AND expr_from='".AddSlashes($item)."'"; $result = mysql_query($query, $ide) or die(mysql_error()); $r = mysql_fetch_array($result); if($r) { $arr[$key] = $r['expr_to']; } else { $arr[$key] = $item; } } return $arr; } else { $query = "SELECT expr_to FROM translation WHERE lang_from = '$lang_from' AND lang_to = '$lang_to' AND expr_from='".AddSlashes($t)."'"; $result = mysql_query($query, $ide) or die(mysql_error()); $r = mysql_fetch_array($result); if($r) { return $r['expr_to']; } else { return $t; } } } ?>

« previous php.smarty.general (#3281) next »