Sandro
Sandro

Reputation: 123

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];
    }
}

Upvotes: 12

Views: 18183

Answers (10)

Andreas Höhne
Andreas Höhne

Reputation: 11

i would simply use a function with controller and language inputs, for instance:

function getLanguageFile($controller, $lang){
 // security: only allow letters a-z in both strings
 $controller = preg_replace('/([^a-z]*)/', '', $controller);
 $lang = preg_replace('/([^a-z]*)/', '', $lang);
 // return language details if present on disk
 if (is_file('lang/'.$controller.'/'.$lang.'.json')){
  return json_decode(file_get_contents('lang/'.$controller.'/'.$lang.'.json'));
 }
 return false;
}

you simply have to place your json formatted strings in lang/index/en.json if controller is index and language is en. you could add a function for dependencies (for instance you want to load index controller values on access to another controller) all you have to do is to merge the results. you could simply include php files with arrays aswell and just return the array then, but i suggest you split these translations in larger projects. if your project isn't that big, your function is absolutely ok.

Upvotes: 0

Th. Ma.
Th. Ma.

Reputation: 9464

One could also be using the Symfony translation component, no framework is required and composer helps dealing with dependencies:

composer install --prefer-dist "symfony/translation":"@stable"

Upvotes: 1

superjos
superjos

Reputation: 12695

I think that's ok if you're not using any framework for other reasons. We've been in the same scenario as yours, when you cannot/don't want to use a more structured translation framework:

We were working at a small PHP project and where looking for some simple translation mechanism. We used an array approach similar to yours, but with separate files for each language texts. We put up a small component to keep thins as clean as possible.

If you want to give a look, we shared that on https://github.com/BrainCrumbz/simple-php-translate. Please feel free to improve it!

Upvotes: 0

Dereleased
Dereleased

Reputation: 10087

When I had a problem like this (but for a very small site, just a few pages) a long time ago, I created a file named langpack.php and any string of text on my site had to be run through that. Now, I would use a similar approach, but split over multiple files.

Example OOP Approach

langpack.php

abstract class langpack {
    public static $language = array();

    public static function get($n) {
        return isset(self::$language[$n]) ? self::$language[$n] : null;
    }
}

english.php

final class English extends langpack {
    public static $language = array(
        'inbox' => 'Inbox',
        'messages' => 'Messages',
        'downloadError' => 'There was an error downloading your files',
    );
}

french.php

final class French extends langpack {
    public static $language = array(
        'inbox' => 'Inbioux',
        'messages' => 'Omelette du Fromage',
        'downloadError' => 'C\'est la vie',
    );
}

You should get the idea from there. Implement an autoloader in a config file and then loading the language should be something you could easily do from the session, URL, or whatever, by using PHP's variable nature in conjunction with class instantiation, something like this:

$langpack = new $_SESSION['language'];
echo $langpack::get('inbox');

Of course, all this could be done with simple arrays, and accessed in an imperative style (with absolute references handled via $GLOBALS) to reduce some overhead and perhaps even make the mechanisms by which this is all handled a bit more transparent, but hey, that wouldn't be very OO, would it?

Upvotes: 1

AntonioCS
AntonioCS

Reputation: 8496

Is using constants (defines) a bad practice?

That's how I have it setup. It was just to have multi langua support.

I have one portuguese file and an english files filled with:

define('CONST','Meaning');

Maybe this is a bit a memory hog, but I can access from every where I want :)

I may change to a oop approach, but for now I have this.

Upvotes: 1

Peter Lindqvist
Peter Lindqvist

Reputation: 10200

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);
    }
}

Upvotes: 17

Scott
Scott

Reputation: 3485

It's fine to not use a framework. The only problem I see with your function is that it's loading a lot of data into memory. I would recommend having arrays for each language, that way you would only need to load the language that is being used.

Upvotes: 2

John Parker
John Parker

Reputation: 54415

I'd have thought it might be easier to simply use an include for each language, the contents of which could simply be a list of defines.

By doing this, you'd avoid both the overhead of including all the language data and the overhead of calling your 'translate' function on a regular basis.

Then again, this approach will limit things in terms of future flexability. (This may not be a factor though.)

Upvotes: 2

Vegard Larsen
Vegard Larsen

Reputation: 13037

There are a few things it appears you haven't considered:

  • Are you simply translating single words? What about sentence structure and syntax that differs between languages?
  • What do you do when a word or sentence hasn't been translated into a language yet?
  • Does your translations support variables? The order of words in a sentence can differ in different languages, and if you have a variable it usually won't be good enough simply to split the word around the sentence.

There are a two solutions that I've used and would recommend for PHP:

  • gettext - well supported in multiple languages
  • intsmarty - based on Smarty templates

Upvotes: 4

Emil Vikstr&#246;m
Emil Vikstr&#246;m

Reputation: 91892

The advantage with using a class or functions for this is that you can change the storage of the languages as the project grows. If you only have a few strings, there is absolutely no problems with your solution.

If you have a lot of strings it could take time, memory and harddrive resources to load the language arrays on all page loads. Then you probably want to split it up to different files, or maybe even use a database backend. If using i database, consider using caching (for example memcached) so you don't need to query the database hundreds of times with every page load.

You can also check out gettext which uses precompiled language files which are really fast.

Upvotes: 2

Related Questions