Language translation using PHP

Natasha picture Natasha · Nov 1, 2012 · Viewed 60.2k times · Source

Hi i am devloping sample site in php i need to translate whole website in to persian. how can it possible in php?? I have tried using the following code.. This code will working fine for deutsch conversion.

1. class.translation.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 function splitStrings($str) {
        return explode('=',trim($str));
    }

    public function __($str) {  
        if (!array_key_exists($this->language, $this->lang)) {
            if (file_exists($this->language.'.txt')) {
                $strings = array_map(array($this,'splitStrings'),file($this->language.'.txt'));
                foreach ($strings as $k => $v) {
                    $this->lang[$this->language][$v[0]] = $v[1];
                }
                return $this->findString($str);
            }
            else {
                echo $str;
            }
        }
        else {
            return $this->findString($str);
        }
    }
}
?>

2.Register.php

<?php
require_once('class.translation.php');

if(isset($_GET['lang']))
    $translate = new Translator($_GET['lang']);
else
    $translate = new Translator('en');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title><?php $translate->__('CSS Registration Form'); ?></title>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-15"/>
        <link rel="stylesheet" type="text/css" href="css/default.css"/>
    </head>
    <body>    
        <form action="" class="register">
            <h1><?php $translate->__('Registration'); ?><a class="flag_deutsch" title="deutsch" href="register1.php?lang=de"></a><a class="flag_english" title="english" href="register1.php"></a></h1>
            <fieldset class="row1">
                <legend><?php $translate->__('Account Details'); ?></legend>
                <p>
                    <label><?php $translate->__('Email'); ?> *</label>
                    <input type="text"/>
                    <label><?php $translate->__('Repeat email'); ?> *</label>
                    <input type="text"/>
                </p>
            </fieldset>

            <div><button class="button"><?php $translate->__('Register'); ?> &raquo;</button></div>
        </form>
    </body>
</html>

Is it possible to transilate to other laguages using this code?? I changed register1.php?lang=de to register1.php?lang=fa(persian).. But nothing hapens..anybody plese help

Answer

Hkachhia picture Hkachhia · Nov 1, 2012

AS per me you can try this method.This method is already implemented in our system and it is working properly.

Make php file of each language and define all the variables and use those variables in pages.

for e.g For english

english.php

$hello="Hello";

persian.php

$hello=html_entity_decode(htmlentities("سلام"));

Now use this variable to page like this.

your_page.php

<label><?php echo $hello; ?></label>

You have load specific language file as per get language variable from URL.

It is better that you have define this language variable into config file.

config.php

if(isset($_GET['lang']) && $_GET['lang']=='persian')
{
   require_once('persian.php');
}
else
{
   require_once('english.php');
}