1

I have a website which i want to create other language version. I don't want to create folder for each language. I was wondering it it's possible to add a combobox on each page or on the main one so that user can setup the language then using php i will check the option and show the right version. Any suggesting to achive that?

0

4 Answers 4

2

If you have a combobox, when the user submits it, store the language in the session (session_start(); has to be called) with $_SESSION['lang'] = $_POST['lang'];. I'd advise you to whitelist languages as such:

session_start();

// define language whitelist
$allowedLangs = array('en', 'de');

// only store the new user language if it's an allowed one
if (isset($_POST['lang']) && in_array($_POST['lang'], $allowedLangs)) {
    $_SESSION['lang'] = $_POST['lang'];
}

// define the user language based on session data or use 'en' as default if not available
$userLang = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en';

// parse some language file according to the language
$translations = // TODO load some file with $userLang here

Of course you should adjust this to your own project and environment. For translation files, you can use a plain PHP file that returns an array like such:

<?php 
// en.php
return array(
    'some.key' => 'Translation',
);

Then if you include that file, the return value of the include will be the array, so in the above code you could do:

$translations = include 'translations/'.$userLang.'.php';

You then have to output all your text through this $translations variable, like echo $translations['some.key'].

Sign up to request clarification or add additional context in comments.

Comments

1

if you wanted to use cookies... in the lang files you would include an array of words or content to use.

 <?php

    if($_GET['language']){
        $lang = (string)$_GET['language'];
        setcookie("lang", $lang, time()+3600);
        header('Location: '.$_SERVER['PHP_SELF']);
        die();
    }elseif(!isset($_COOKIE['lang'])){
        $lang='en';
    }else{$lang=$_COOKIE['lang'];}

    switch($lang){
        case "en":
            include('./lang/en.php');
            break;
        case "fr":
            include('./lang/fr.php');
            break;
        case "pol":
            include('./lang/pol.php');
            break;
        default:
            include('./lang/en.php');
            break;
    }
    ?>

Comments

0

you mean something along the lines of

if ($_GET['language']) {
  include $_GET['language'] . ".php";
}

and then save the languages in a php-file with there name, or a function depending on what you want to do with it

8 Comments

Cool, I get to include any file I'd like :)
i would not use this example as is due to any file can be included
well you could use a post instead to make it abit more secure that was more psevdo-code then for copy paste
People copy paste stuff from everywhere. You should always be careful when publishing code.
Even post is not secure. If you set up an array of all allowed values, you can check to see whether your input is in that array. If so, use that array element, if not, use a default language. Edit: See Saldaek's answer for more info.
|
0

hey for language version. have languages in combobox. maintain your current language in session. When u change language call an ajax call Update the changed language into session and reload the page.

display page view with respect to session stored language.

thats it........

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.