web multi language php with session code example

Example 1: php multilanguage

<?php
session_start();

$host = "localhost"; /* Host name */
$user = "root"; /* User */
$password = ""; /* Password */
$dbname = "tutorial"; /* Database name */

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}
mysqli_set_charset( $con, 'utf8');

Example 2: multi lang in php

<?php
$trans = [
    'en' => [
        'user_where_are_you_text' => 'Where are You, %s? It is me, %s! I am waiting here for %s hours!',
        //...
    ],
    'fr' => [
        'user_where_are_you_text' => 'Où es-tu, %s? C\'est moi, %s! J\'attends ici depuis %s heures!'
        //...
    ],
    //...
];

$name = 'Loz';
$name1 = 'Rasmus';
$time = 3;

function __($key, ...$arguments) {
    global $trans, $lang;
    return sprintf($trans[$lang][$key], ...$arguments);
}

//
$lang = 'en';
echo __('user_where_are_you_text', $name, $name1, $time).PHP_EOL;

//
$lang = 'fr';
echo __('user_where_are_you_text', $name, $name1, $time).PHP_EOL;

Tags:

Php Example