Core PHP Site translate to other language

955 Views Asked by At

I have create array $_en to store English words/sentences and $_no array to store Norwegian text to use it as translation for my core PHP project.

<?php
$_en = array(
    'mail' => 'email',
    'msg1' => 'how are you?'
    
);
$_no = array(
    'mail' => 'epost',
    'msg1' => 'hvordan har du det ?'
);

echo "EMAIL IN ENGLISH:".$_en['mail']."\n"; //email in english
echo "EMAIL IN NORWEGIAN:".$_no['mail']; //email in NORWEGIAN
echo "Message IN NORWEGIAN:".$_no['msg1']; //Message in NORWEGIAN

Based on the array and the key value the text will be called based on the site translation function.

Any better solutions and enhancements are most welcome.

3

There are 3 best solutions below

0
On BEST ANSWER

A better solution, as mentioned my Thamilan in the comments would be to use a class to handle conversions.

Think of it like this;

Your Template File

$trans = new Translate('en');
<h1><?php echo $trans->__('This is a title'); ?></h1>

Translate.php

class Translate {
    public function __construct($lang) {
        $this->lang = $lang;
    } 

    public function __($string) {
        // $translatedString = $this->getTranslatedString($string);
        return $translatedString;
    }
}

In this class, you'll need to fetch the translation from where it's stored. You could store in in this class in an array, or a better solution would be to load them from a CSV file.

0
On

Try this :--

parse_ini_file() is a very powerful little tool that does exactly what you expect. Using a simple file like this which we'll call en.ini:

PAGE_TITLE = My website page title
HEADER_TITLE = My website header title
SITE_NAME = My Website
SLOGAN = My slogan here
HEADING = Heading
MENU_LOGIN = Login
MENU_SIGNUP = Sign up
MENU_FIND_RIDE = Find Ride
MENU_ADD_RIDE = Add Ride
MENU_LOGOUT = Logout

You can simply use: parse_ini_file('en.ini') to return an array exactly as in your switch statement, which will be much easier for other (non-programmers) to read and write for you. And if you were to then continue naming the files with this style, you could reduce userLanguage() to something like:

public function userLanguage()
{
    $file = '/path/to/language/config/' . $this->UserLng . '.ini';
    if(!file_exists($file))
    {
        //Handle Error
    }
    return parse_ini_file($file);
}
0
On

Another Solution Try:--

Create Abstract class

interface Language
{
    public function getPageTitle();
    public function getHeaderTitle();
    public function getSiteName();
    public function getSlogan();
    public function getHeading();
    public function getMenuLogin();
    public function getMenuSignup();
    public function getMenuFindRide();
    public function getMenuAddRide();
    public function getMenuLogout();
}

Though the interface is small, implementing it may produce a big file, though it would arguably be clearer than the array style:

class English implements Language
{
    public function getHeaderTitle()
    {
        return 'My website header title';
    }

    public function getHeading()
    {
        return 'Heading';
    }

    // etc...
}

Alternative:--

Alternatively, you could combine these styles and have a singleton Language with getter methods accessing that array, i.e.:

class Language
{
    private $languageArray;
    private $userLanguage;

    public function __construct($language)
    {
        $this->userLanguage = $language;
        $this->languageArray = self::userLanguage();
    }

    private static function userLanguage()
    {
        $file = '/path/to/language/config/' . $this->userLanguage . '.ini';
        if(!file_exists($file))
        {
            //Handle Error
        }
        return parse_ini_file($file);
    }

    public function getPageTitle()
    {
        return $this->languageArray['PAGE_TITLE'];
    }

    public function getHeaderTitle()
    {
        return $this->languageArray['HEADER_TITLE'];
    }

    //etc...
}

Which will provide the benefits of both. Personally though, unless you're planning on adding more languages in the very near future, I believe solution #2 would suit you best.