Jump to content
Phishing - Beware of identity theft... ×
  • 0

What's the best way to implement multi language


Denis

Question

Hi,

 

I searched at google and I didn't find anything useful so I post here.

 

Like the title says I'm trying to create a website with multi language, but I have some doubts about which way should I use, constants or a simple array with all my keys there?

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
  • Bronze

There are many ways to realise a multi language system. I prefer an array like:
 
english.php:

function lang($a){
    static $lang = array(
        'PLACEHOLDER_1' => 'This is a test',
        'PLACEHOLDER_2' => 'Another test'
    );
    return $lang[$a];
}

usage examples:

<?php
// simple
echo lang('PLACEHOLDER_1');

// or like this
echo "<div class="text">".lang('PLACEHOLDER_2')."</div>";
?>
Link to comment
Share on other sites

  • 0

So far I've been using something like this:

<?php    
public function load($lang)
{
    $file = file($lang.".txt", FILE_IGNORE_NEW_LINES);
        
    foreach ($file as $i => $line)
    {
        $line = trim($line);

        $token = explode(" = ", $line);
                
        if(!defined($token[0]))
        {
            define($token[0],$token[1]);
        }
        else
        {
            die($token[0]." is already defined");
        }
    }
}
else
{
    die('error');
}

load('lang'); /* At lang.txt:
    TITLE = Hello
*/
echo TITLE;
?>

You think it's a good idea to use constants or should I use array like you said?

Link to comment
Share on other sites

  • 0

language.class.php

<?php

class dLanguage {

    public function __construct($sLanguage = 'de') {
        if (empty($sLanguage)) {
            $sLanguage = 'de';
        }

        $this->sLanguage = $sLanguage;

        if (file_exists('inc/lang/' . $this->sLanguage . '.lang.php')) {
            $aLanguage = array();

            require_once 'inc/lang/' . $this->sLanguage . '.lang.php';

            $this->aStrings = $aLanguage;
        }
    }

    public function setLanguage($sLanguage = 'de') {
        if (empty($sLanguage)) {
            $sLanguage = 'de';
        }

        $this->sLanguage = $sLanguage;
    }

    public function getText($sKey) {
        if (!isset($this->aStrings[$sKey]) || empty($this->aStrings[$sKey])) {
            return '<span class="dLanguage" id="' . $sKey . '">dLanguage: ' . $sKey . '</span>';
        }

        return '<span class="dLanguage" id="' . $sKey . '">' . $this->aStrings[$sKey] . '</span>';
    }

    public function getArray() {
        if (!isset($this->aStrings)) {
            return array();
        }

        return $this->aStrings;
    }

}

lang.de.php EXAMPLE

<?php

$aLanguage = array(
    'refresh' => 'Aktualisieren',
    'loading' => 'Lade',
    'newsNavigation' => 'Startseite'
);

My config.inc.php

<?php

require_once 'language.class.php';

$sLanguage = 'de';

if (sGetCookie('language') !== '') {
    $sLanguage = sGetCookie('language');
}

setcookie('language', $sLanguage, time() + 60 * 60 * 24 * 365);

$oLanguage = new dLanguage($sLanguage);

You'll get the translated text by

<?php

echo $oLanguage->getText('refresh');

If needed I'll be able to send you my JS namespace for it, so you can translate the language by buttons with jQuery ;-)

  • Love 1
Link to comment
Share on other sites

  • 0
  • Bronze
You think it's a good idea to use constants or should I use array like you said?

 

I have searched the web for more informations expecialy benchmarks about this topic but i cant really find something informative. What i can say (definitively) is: If your langugae file is not getting big (~50mb) you can use your script without thought. I think there is no reason to think about memory usage or something else if your project isnt that big. Personally i would use an array but you can do this with reading a txt file like your example. Constants defined using define() are fairly slow in PHP.

 

edit://

compile-time constants like 

const TITLE = VALUE;

(since php5.3) are 2x faster.

 

Source: http://stackoverflow.com/questions/2447791/define-vs-const

Edited by Ayaka
  • Love 1
Link to comment
Share on other sites

  • 0

 

You think it's a good idea to use constants or should I use array like you said?

 

I have searched the web for more informations expecialy benchmarks about this topic but i cant really find something informative. What i can say (definitively) is: If your langugae file is not getting big (~50mb) you can use your script without thought. I think there is no reason to think about memory usage or something else if your project isnt that big. Personally i would use an array but you can do this with reading a txt file like your example. Constants defined using define() are fairly slow in PHP.

 

edit://

compile-time constants like 

const TITLE = VALUE;

(since php5.3) are 2x faster.

 

Source: http://stackoverflow.com/questions/2447791/define-vs-const

 

 

I'm already using array for translation,anyway, thank you.

  • Love 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


×
×
  • Create New...

Important Information

Terms of Use / Privacy Policy / Guidelines / We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.