Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Easiest way for multilanguage

Post new topic   Reply to topic    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  

Did this help you?
 0%  [ 0 ]
No big deal...
 0%  [ 0 ]
Script stealers must die!
 0%  [ 0 ]
Total Votes : 0

Author Message
Smarty Rookie

Joined: 09 Apr 2010
Posts: 31

PostPosted: Mon Jan 02, 2012 10:02 pm    Post subject: Easiest way for multilanguage Reply with quote

Hi, several days ago explored chevero script and founded how they implemented multilanguage, afcourse i like it and decided to share with rest.

For path example's i used lampp, so leaved path's how they should look in your script.

The script (a bit added's simple, most of you understand it):
/* Default language for users */
$config['def_lang'] = 'en';
/* Auto detect language for user */
$config['auto_lang'] = 'yes';
$default_lang = '/opt/lampp/htdocs/includes/langs/'.$config['def_lang'].'/lang.php';

/* Check if  language letters greater then 0 and language file exist */
if(strlen($config['def_lang'])>0 and file_exists($default_lang)) {
   /* If auto_lang  equals to yes, search for  language file and use it */
   if($config['auto_lang'] == 'yes') {
      $lang_code = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,2);
      $auto_lang = '/opt/lampp/htdocs/includes/langs/'.$lang_code.'/lang.php';
      if(file_exists($auto_lang)) {
         define('LANGS', $auto_lang);
      } else {
         define('LANGS', $default_lang);
   } else {
      define('LANGS', $default_lang);
} else {
   die('No lang file.');
/* Check for valid and setted values */
function check_value($value) {
   if(count($value)>0 and !empty($value) and isset($value)) {
      return true;
/* Get variables content*/
function get_lang_txt($lang_txt) {
   global $lang;
   if(check_value($lang[$lang_txt])) {
      return $lang[$lang_txt];
   } else {
      return 'LANG_NOT_FOUND';
/* Print variables content on document */
function lang_txt($lang_txt) {
   return get_lang_txt($lang_txt);

Now create folder: langs in that folder create another folder's (Ex. en, ru, it, de, nl and so on...)
In each folder create file: lang.php
Open lang.php and for example write: $lang['example'] = 'Example'; (Afcourse its gonna be for en language, open another folder's, lang.php file (ex. de) and write: $lang['example'] = 'Beispiel'; (I think you got idea...).

Now, usage in templates are easy, just assign them
$smarty->assign('lang_how', lang_txt('example'));


My opinions:
If producing site with options to choose which langugae to use on site, make sure turn off auto detection, because each time after refresh it change language.

Include language files before script or function execution.

I take no credit's, everythink goes to Chevereto
Back to top
View user's profile Send private message
Smarty Rookie

Joined: 08 Apr 2012
Posts: 10

PostPosted: Wed Apr 25, 2012 3:53 pm    Post subject: Reply with quote

Thanks ;)
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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