1

I have a PHP site. Now I need to "convert" it into a multilingual site by adding an option where the users will be able to select the language of their preference. Let's say I want 2 languages for the beggining and then a smart way to add new languages when it needed.

Database VS Language files...

As I know there is two options.

1) I can use MySQL database tables with languages, translations, etc and every time based on user's preferred language to retrieve data from the db which means lots of database requests.

2) I can use external language PHP files using define command (like some e-commerce frameworks who have different languages files into a language folder such as fr.php eng.php etc)

What is better? The answers must based on the following arguments with their pros and cons.

1) Best performance

2) Flexibility

3) More efficient

4) Intelligent way

5) Resources usage (CPU, Memory etc)

P.S. The Google translation tool for websites isn't a proper solution in my case.

Mansfield
  • 14,445
  • 18
  • 76
  • 112
AlexCode
  • 655
  • 5
  • 18
  • in my workings we always used language files. They reside on the server, they are easily accessed via the frameworks and the response time to download them is near nil. In the event your database or whatever goes down (be it for maint. or otherwise) at least your languages will always be available to the users. This is not to say that putting it in the DB is not a good idea, I just have more experience the other way. – Mike McMahon Apr 06 '12 at 17:02
  • take a look at this question: http://stackoverflow.com/questions/8281589/develop-multilangual-system – raPHPid Apr 06 '12 at 17:02

1 Answers1

2

The database solution is very expensive in my honest opinion and not really needed. Probably your website has static (that don't changes) language phrases. In this case you should use a language array to store them as $array['key'] = 'phrase' and be able to retrieve them via something global. Something like this:

class Lang {

    private static $loaded;
    private static $array;

    private static function set($key, $value) {
        self::$array[$key] = $value;
    }

    public static function get($key, $default = 'empty') {
        $lang = LANG;
        $file = "lang/$lang.php";
        if (!in_array($lang, self::$array) and file_exists($file)) {
            include($file);
            self::$loaded[] = $lang;
        }
        return (isset(self::$array[$key])
            ? self::$array[$key]
            : $default;
    }

}

Now you'll just have to stores language files as lang/en.php or lang/it.php... Which contains something like this: self::set('MY_KEY', 'This is a phrase'); And then just set LANG to be the correct one for each request. I used a constant LANG but there are other ways to store something global.

You can, and is probably preferred, to use a non-static class. Such as:

class Lang {

    private $lang;

    public function __construct($lang) {
        $file = "lang/{$lang}.php";
        if (file_exists($file))
            include($file);
    }

    private function set($key, $value) {
        $this->lang[$key] = $value;
    }

    public function get($key, $default = 'empty') {
        return (isset($this->lang[$key])
            ? $this->lang[$key]
            : $default;
    }

}

But then you should use Dependency Injection to inject this class every time you need. An usage example:

$lang = new Lang('en');
$lang->get('LANG_KEY');
Shoe
  • 74,840
  • 36
  • 166
  • 272