Multiple languages

Started on the old forum in April 2011 · view the original in the archive · old links redirect here

No replies · 1,180 views · started 15 years ago · 2 min read

arnage Lead · 811 posts #1

Here are used tree languages in files srpski.php , english.php , spain.php stored in /translate/ folder, srpski.php (sr) is default.

$lang = isset($_GET['lang']) ? trim($_GET['lang']) : 'sr';

$langs = array('sr' => 'srpski', 'en' => 'english', 'sp' => 'spain');

if (!empty($lang) && !preg_match('/^[a-z]+$/D', $lang) || strlen($lang) > 2) { exit; }

if (isset($lang) && array_key_exists($lang, $langs)) {
$token_lang = $langs[$lang];
if (is_file($_SERVER['DOCUMENT_ROOT'].'/translate/'.$token_lang.'.php')) { require_once($_SERVER['DOCUMENT_ROOT'].'/translate/'.$token_lang.'.php'); }
} else {
if (is_file($_SERVER['DOCUMENT_ROOT'].'/translate/srpski.php')) { require_once($_SERVER['DOCUMENT_ROOT'].'/translate/srpski.php'); }
}

Files srpski.php , english.php , spain.php are for smaller translations like link names, categories, title... and contains something like:

$tlang = array();
$tlang['home'] = 'Home';
$tlang['back'] = 'Back';
$tlang['wallpapers'] = 'Free Wallpapers';
// ...

This function is for translation pages, to open the folder and include specified tpl file in case of choosen language.

function get_lng_inc($inc_folder, $inc_file) { global $lang;
if ($lang == 'en' || $lang == 'sp') {
if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$inc_folder.'/'.$lang.'/'.$inc_file.'.tpl')) { require_once($_SERVER['DOCUMENT_ROOT'].'/'.$inc_folder.'/'.$lang.'/'.$inc_file.'.tpl'); }
} else {
if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$inc_folder.'/sr/'.$inc_file.'.tpl')) { require_once($_SERVER['DOCUMENT_ROOT'].'/'.$inc_folder.'/sr/'.$inc_file.'.tpl'); }
}
}

For example folder structure like: root/folder/subfolder1/sr/file.tpl en/file.tpl sp/file.tpl /folder/subfolder2/sr/file.tpl en/file.tpl sp/file.tpl ... usage for the function when ur in /folder/ index.php is:

	echo head($tlang['PageTitle']);

echo get_lng_inc('subfolder2', 'file');

	echo foot();

Usage:

if ($lang == 'en') {
echo '
<a href="./index.php?lang=sr">Srpski</a>
<a href="./index.php?lang=sp">Espańol</a>';
}
elseif ($lang == 'sp') {
echo '
<a href="./index.php?lang=en">English</a>
<a href="./index.php?lang=sr">Srpski</a>';
} else {
echo '
<a href="./index.php?lang=en">English</a>
<a href="./index.php?lang=sp">Espańol</a>';
}
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close