PHP simple translation approach - your opinion

Sandro picture Sandro · Dec 29, 2009 · Viewed 15.7k times · Source

I am doing a PHP web site, without using any framework. I need that the site is available in several languages, and I was reading about it and it seems to be a little bit confusing. There are several solutions but all seem to depend on a specific framework.

What you think of using a simple translation function like the one shown below?

I mean, I would like to know what can be a disadvantage of using such code. Here it is (this is just a simple and incomplete sample):

class Translator{

    private $translations;

    public function __construct(){
        $this->translations = array(
            'Inbox'  => array(
                'en' => 'Inbox',
                'fr' => 'the french word for this'
            ),
            'Messages' => array(
                'en' => 'Messages',
                'fr' => 'the french word for this'
            )
            //And so on...
        );
    }

    public function translate($word,$lang){
        echo $this->translations[$word][$lang];
    }
}

Answer

Peter Lindqvist picture Peter Lindqvist · Dec 29, 2009

It does not look bad. I've seen this used many times.

I would however separate the different strings in one file per language. At least, or if the files get large, one file per module per language.

Then your translation class can load and cache the language files (if you don't rely on any other caching system) every time a new language is to be used.

A little example of what i mean

class Translator {
    private $lang = array();
    private function findString($str,$lang) {
        if (array_key_exists($str, $this->lang[$lang])) {
            return $this->lang[$lang][$str];
        }
        return $str;
    }
    private function splitStrings($str) {
        return explode('=',trim($str));
    }
    public function __($str,$lang) {
        if (!array_key_exists($lang, $this->lang)) {
            if (file_exists($lang.'.txt')) {
                $strings = array_map(array($this,'splitStrings'),file($lang.'.txt'));
                foreach ($strings as $k => $v) {
                    $this->lang[$lang][$v[0]] = $v[1];
                }
                return $this->findString($str, $lang);
            }
            else {
                return $str;
            }
        }
        else {
            return $this->findString($str, $lang);
        }
    }
}

This will look for .txt files named after the language having entries such as this

Foo=FOO
Bar=BAR

It always falls back to the original string in case it does not find any translation.

It's a very simple example. But there is nothing wrong in my opinion with doing this by yourself if you have no need for a bigger framework.

To use it in a much simpler way you can always do this and create a file called 'EN_Example.txt'

class Example extends Translator {
    private $lang = 'EN';
    private $package = 'Example';
    public function __($str) {
        return parent::__($str, $this->lang . '_' . $this->package);
    }
}

Sometimes you wish to translate strings that contain variables. One such approach is this which i find simple enough to use from time to time.

// Translate string "Fox=FOX %s %s"
$e = new Example();
// Translated string with substituted arguments
$s = printf($e->__('Fox'),'arg 1','arg 2');

To further integrate variable substitution the printf functionality can be put inside the __() function like this

public function __() {
    if (func_num_args() < 1) {
        return false;
    }
    $args = func_get_args();
    $str = array_shift($args);
    if (count($args)) {
        return vsprintf(parent::__($str, $this->lang . '_' . $this->package),$args);
    }
    else {
        return parent::__($str, $this->lang . '_' . $this->package);
    }
}