Jump to content

What's the best way to implement multi language


Recommended Posts

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

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

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

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

 

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



  • Similar Content

  • Activity

    1. 0

      We are looking for a C++ and Python programmer

    2. 0

      [Quest Scheduler Request] Is there a way to make a quest run independet of player events? Lets say start quest automatically at server startup?

    3. 111

      Ulthar SF V2 (TMP4 Base)

    4. 0

      Quest function when 102.kill definition whereabouts help

    5. 5

      [M2 FILTER] Customized Client Filter

    6. 0

      [INGAME] RGB Color on chat broken

  • Recently Browsing

    • No registered users viewing this page.
×
×
  • 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.