Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP simple translation approach - your opinion

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];
    }
}
like image 693
Sandro Avatar asked Dec 29 '09 12:12

Sandro


People also ask

What are the approaches of translation?

There are six main approaches within contemporary translation theory: sociolinguistic; communicative; hermeneutic; linguistic; literary; and semiotic.

How can you define translation in your opinion?

Translation is a mental activity in which a meaning of given linguistic discourse is rendered from one language to another. It is the act of transferring the linguistic entities from one language in to their equivalents in to another language.

Why is Philippine translation important?

It turns languages into bridges and opportunities to make new connections, and it makes a country's voice clearer and easier to understand. According to Filipino writer Galileo Zafra, translation can foster the development of a national language.

How can I translate in PHP?

php class Translator { private $language = 'en'; private $lang = array(); public function __construct($language){ $this->language = $language; } private function findString($str) { if (array_key_exists($str, $this->lang[$this->language])) { echo $this->lang[$this->language][$str]; return; } echo $str; } private ...


1 Answers

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);
    }
}
like image 87
Peter Lindqvist Avatar answered Oct 08 '22 19:10

Peter Lindqvist