www.webdeveloper.com
Results 1 to 5 of 5

Thread: multilanguage site

  1. #1
    Join Date
    Jan 2014
    Posts
    7

    multilanguage site

    I`m trying to creat multilanguage site with OOP php and mysql

    I have created 4 files

    1) index.php
    2) init.php
    3) lang.php
    4) database.php

    database.php

    PHP Code:
    <?php 
        $config 
    = array(
            
    'host'        => '127.0.0.1',
            
    'username'     => 'root',
            
    'password'     => '',
            
    'dbname'     => 'si'
        
    );
        
        
    $db = new PDO('mysql:host=' $config['host'] . ';dbname=' $config['dbname'],$config['username'],$config['password']);
        
    $db->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
        
    mysql_query('SET CHARACTER SET utf8');
        
    mysql_query('SET NAMES utf8');
    ?>
    lang.php

    PHP Code:
    <?php
        
    class Language 
        
    {
            private 
    $db;
            public function 
    __construct($database)
            {
                
    $this->db $database;
            }
            
            public function 
    pagetranslate($langname)
            {
                
    $query    $this->db->prepare("SELECT F.name, FT.trans
                                                FROM `languages` AS L
                                                INNER JOIN `transfunc` AS FT ON FT.langid = L.id
                                                INNER JOIN `func` AS F ON FT.funcid = F.id
                                                WHERE L.name =?"
    );
                
    $query->bindValue(1$langname);
                try
                {
                    
    $query->execute();
                    
    $pagetrans $query->fetch();                
                    return 
    $pagetrans;
                }
                catch(
    PDOException $e)
                {
                    die(
    $e->getMessage());
                }
            } 
        }
    init.php
    PHP Code:
    <?php
        
    if (ob_get_level() == 0){ob_start();}
        if (!isset(
    $_SESSION)){session_start();}
        
        require_once 
    'connect/database.php';
        require_once 
    'class/lang.php';
        
    $language    = new Language($db);
    ?>
    index.php

    HTML Code:
    <?php
    	if (!isset($_SESSION)){session_start();}
    	header('Cache-control: private');
    	if(isset($_GET['lang']))
    	{
    		$langname = $_GET['lang'];
    	}
    	else if(isset($_SESSION['lang']))
    	{
    		$langname = $_SESSION['lang'];
    	}
    	else if(isset($_COOKIE['lang']))
    	{
    		$langname = $_COOKIE['lang'];
    	}
    	else
    	{
    		$langname = 'ka';
    	}
    	include 'system/init.php';
    	$pagetrans = array();
    	$pagetrans = $language->pagetranslate($langname);
    		
    	function pageinfo($searchWord, $pagetrans)
    	{
    		foreach( $pagetrans as $key => $val )
    		{
    			if( $val["name"] == $searchWord )
    			{
    				return $val["trans"];
    			}
    		}
    	}
    ?>
    <!doctype html>
    <html>
    	<head>
            <meta charset="utf-8">
            <title><?php echo pageinfo('hometitle', $pagetrans); ?></title>
             </head>
            <body>
            </body>
    </html>
    and also I have created database

    my DB image

    and when I executing index.php
    it writting this error

    </br> <b>Nobice</b>
    please help me

    I do not understand what is the problem

    when I`m executing the selector in mysql workbench
    please look this image

    please help me!!!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,154
    Is that the entire error message? (And is it really spelled "Nobice" instead of "Notice"?)

    If it is a PHP-generated error, it should include the file where it was generated and the line number, as well as what the (potential) problem is.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2014
    Posts
    4
    Does it have to be with MySQL? If not required, you can use what i use:

    Code:
    <?php
    
    error_reporting(E_ALL);
    
    global $lang; $lang = 0;
    
    function setlanguage($lng)
      {
        static $ERR = 0; $ERL = 0; $REL = 0; $Returnvalue = 0;
    
        if (strtolower(Trim($lng)) == 'english')
          {
            $GLOBALS['lang'] = 0;
          }
        if (strtolower(Trim($lng)) == 'spanish')
          {
            $GLOBALS['lang'] = 1;
          }
        if (strtolower(Trim($lng)) == 'french')
          {
            $GLOBALS['lang'] = 2;
          }
    
      }
    
    function trans($sentence)
      {
        static $ERR = 0; $ERL = 0; $REL = 0; $Returnvalue = 0;
    
        if ($GLOBALS['lang']==0) {return $sentence;}
    
        $sentences = array('Hello World' => array('Hola Mundo', 'Bonjour tout le monde'));
    
        return $sentences[$sentence][$GLOBALS['lang']];
    
      }
    
    setlanguage('spanish');
    
    echo trans('Hello World');
    
    ?>
    You can do it more sophisticated storing the translation string in another file, but i like this way because you can actually see what the translation reads in the original language.

  4. #4
    Join Date
    Jan 2014
    Posts
    4
    Seems like i cannot edit my own posts, this line:

    Code:
    return $sentences[$sentence][$GLOBALS['lang']];
    should be:

    Code:
    return $sentences[$sentence][$GLOBALS['lang']-1];

  5. #5
    Join Date
    Jan 2014
    Posts
    7
    ok
    I did this with this code

    SQL
    --
    -- Table structure for table `func`
    --

    CREATE TABLE IF NOT EXISTS `func` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(30) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

    --
    -- Dumping data for table `func`
    --

    INSERT INTO `func` (`id`, `name`) VALUES
    (1, 'hometitle');

    -- --------------------------------------------------------

    --
    -- Table structure for table `languages`
    --

    CREATE TABLE IF NOT EXISTS `languages` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `name` varchar(10) NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

    --
    -- Dumping data for table `languages`
    --

    INSERT INTO `languages` (`id`, `name`) VALUES
    (1, 'ka'),
    (2, 'ru'),
    (3, 'en');

    -- --------------------------------------------------------

    --
    -- Table structure for table `transfunc`
    --

    CREATE TABLE IF NOT EXISTS `transfunc` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `funcid` int(11) NOT NULL,
    `langid` int(11) NOT NULL,
    `trans` text CHARACTER SET utf8 NOT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;
    lang.php

    <?php
    class Language
    {
    private $db;
    public function __construct($database)
    {
    $this->db = $database;
    }

    public function pagetranslate($langname)
    {
    $query = $this->db->prepare("SELECT F.name, FT.trans
    FROM transfunc FT
    INNER JOIN func F ON FT.funcid = F.id
    INNER JOIN languages L ON FT.langid = L.id AND L.name =?");

    $query->bindValue(1, $langname, PDO::PARAM_STR);
    try
    {
    $query->execute();
    $row = array();
    $row = $query->fetchall();
    return $row;
    }
    catch(PDOException $e)
    {
    die($e->getMessage());
    }
    }
    }
    init.php

    <?php
    if (ob_get_level() == 0){ob_start();}
    if (!isset($_SESSION)){session_start();}

    require_once 'connect/database.php';
    require_once 'class/lang.php';
    $language = new Language($db);
    ?>
    index.php

    <?php
    if (!isset($_SESSION)){session_start();}
    header('Cache-control: private');
    if(isset($_GET['lang']))
    {
    $langname = $_GET['lang'];
    }
    else if(isset($_SESSION['lang']))
    {
    $langname = $_SESSION['lang'];
    }
    else if(isset($_COOKIE['lang']))
    {
    $langname = $_COOKIE['lang'];
    }
    else
    {
    $langname = 'ka';
    }
    include 'system/init.php';
    $trans = array();
    $trans = $language->pagetranslate($langname);
    function pageinfo($searchWord, $trans)
    {
    foreach( $trans as $key => $val )
    {
    if( $val["name"] == $searchWord )
    {
    return $val["trans"];
    }
    }
    }
    if (isset($_GET['page']))
    {
    $page = $_GET['page'];
    $pages = array('home','company','admin','login');
    if(in_array($page,$pages))
    {
    $page .= '.php';
    include($page);
    }
    else
    {
    header('location: index.php?lang='.$langname.'&page=home');
    exit();
    }
    }
    else
    {
    header('location: index.php?lang='.$langname.'&page=home');
    exit();
    }
    ?>
    home.php

    <?php
    if (!isset($_SESSION)){session_start();}
    header('Cache-control: private');
    if(isset($_GET['lang']))
    {
    $langname = $_GET['lang'];
    }
    else
    {
    $langname = 'ka';
    }
    if ($_SERVER['REQUEST_URI'] === '/s/home.php')
    {
    header('location: index.php?lang='.$langname.'&page=home');
    exit();
    }
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title><?php echo pageinfo("hometitle", $trans); ?></title>
    </head>
    <body>
    </body>
    </html>
    it is working...
    but page loading is very very slow
    how can I solve this problem???

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles