|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Did this help you? |
Good |
|
0% |
[ 0 ] |
No big deal... |
|
0% |
[ 0 ] |
Script stealers must die! |
|
0% |
[ 0 ] |
|
Total Votes : 0 |
|
Author |
Message |
suf3r Smarty Rookie
Joined: 09 Apr 2010 Posts: 31
|
Posted: Mon Jan 02, 2012 10:02 pm Post subject: Easiest way for multilanguage |
|
|
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 explaining...it's simple, most of you understand it):
Code: | <?php
/* 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);
}
require_once(LANGS);
} 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
(php):
$smarty->assign('lang_how', lang_txt('example'));
(tpl):
{$lang_how}
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 |
|
kataxxx Smarty Rookie
Joined: 08 Apr 2012 Posts: 10
|
Posted: Wed Apr 25, 2012 3:53 pm Post subject: |
|
|
Thanks ;) |
|
Back to top |
|
|
|
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
|