0

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.

If any better solutions and enhancements are most welcome. Correct me thanks in advance.

Manoj Sharma
  • 1,467
  • 2
  • 13
  • 20
ben
  • 470
  • 3
  • 11
  • 1
    Better have a language class file with string tokens and their conversions in separate file. Call the file based on the language selected – Thamilhan Jan 04 '17 at 10:40
  • 1
    It is not correct Norwegian to have a space before the question mark, I wonder why some people have started using it. You could also have a look at http://php.net/manual/en/function.gettext.php which is the build in i18n support in php – rypskar Jan 06 '17 at 13:35
  • @rypskar thanks! you can explain me more about it – ben Jan 07 '17 at 05:29
  • 1
    @ben You could have a look at the answers at http://stackoverflow.com/questions/4062832/complete-example-of-gettext-in-php – rypskar Jan 10 '17 at 06:42

3 Answers3

1

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.

Community
  • 1
  • 1
Tom
  • 4,257
  • 6
  • 33
  • 49
1

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

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.