Multiple languages

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Multiple languages

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

    PHP Code:
    $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:

    PHP Code:
    $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.

    PHP Code:
    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:

    PHP Code:
        echo head($tlang['PageTitle']);

    echo 
    get_lng_inc('subfolder2''file');

        echo 
    foot(); 
    Usage:

    PHP Code:
    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>'
    ;

    Last edited by arnage; 20.04.11, 20:22.
Working...
X