Language selection

I am creating a picklist. But I don't know how to change the window language depending on the selected language and I need help opening these translations from $lang.es

and $lang.en

. Here's one example:

<?php
/* 
------------------
Language: English
------------------
*/
$lang = array();

$lang['page_title'] = 'Client Service System';
$lang['username'] = 'Username';
$lang['password'] = 'Password';
$lang['language'] = 'Language';
$lang['es'] = 'Espaniol';
$lang['en'] = 'English';
$lang['forgot'] = 'Forgot password';
$lang['submit'] = 'login';

?>

      

And here is my html php script where the dropdown is:

<tr>
    <td align="right" nowrap><?php echo $lang['language'];?>:</td>
    <td align="left" nowrap>
        <select type="language" id="my-select" name="language" class="text" onchange="javascript:languageChange();">
            <option value="es"><?php echo $lang['es']; ?></option>
            <option value="en"><?php echo $lang['en']; ?></option>
        </select>
    </td>
</tr>    <script type="text/javascript">
        var select = document.forms[0].language;
        select.onchange = function(){
           var kalba =  select.options[select.selectedIndex].value; // to get Value
           var text =  select.options[select.selectedIndex].text; // to get Text
        }; </script>

      

+3


source to share


1 answer


There are many ways to handle localization in PHP. As noted in another question , you can use PHP gettext

. You can also use constants, store in JSON format , store in a database or in an array as you do.

To make your script work, you first need to redirect to the PHP script the parameter for the language being loaded. Then you create a script to conditionally open one of the files.

Here's a simple implementation, with a class to handle loading and retrieving language files.

Basic PHP script



<?php
require 'Localization.Class.php';

$localization = new Localization();
$localization->addLanguage("en");
$localization->addLanguage("es");
$lang = $localization->getLanguageStrings($_GET);
$selectedLanguage = $localization->getSelectedLanguage();
?>

<tr>
    <td align="right" nowrap><?php echo $lang['language'];?>:</td>
    <td align="left" nowrap>
        <form>
            <select type="language" id="my-select" name="language" class="text" onchange="javascript:languageChange();">
                <option value="es"<?php echo ($selectedLanguage == "es") ? " selected" : ""; ?>><?php echo $lang['es']; ?></option>
                <option value="en"<?php echo ($selectedLanguage == "en") ? " selected" : ""; ?>><?php echo $lang['en']; ?></option>
            </select>
        </form>
    </td>
</tr>    

<script type="text/javascript">
var select = document.forms[0].language;
select.onchange = function(){
   var selectedLanguage = select.options[select.selectedIndex].value;
   window.location.href = '?lang=' + selectedLanguage;
}; 
</script>

      

Localization.Class.php

<?php
class Localization
{
    private $defaultLanguage = "en";
    private $chosenLanguage = null;
    private $languages = array();

    public function getLanguageStrings($get) {
        $this->identifyLanguage($get);
        $languageArray = $this->getLanguageFileContents();
        return $languageArray;
    }

    public function addLanguage($lang) {
        $this->languages[] = $lang;
    }

    public function getSelectedLanguage() {
        return $this->chosenLanguage;
    }

    private function identifyLanguage($get) {
        if (isset($get['lang']) and $get['lang'] !== "") {
            if (!in_array($get['lang'], $this->languages)) {
                // this is an important check, as this string will be used
                // as part of the filename to be included
                throw new Exception(__METHOD__ . ": The specified language ($get[lang]) was not found.");
            }
            $this->chosenLanguage = $get['lang'];
        } else {
            $this->chosenLanguage = $this->defaultLanguage;
        }
    }

    private function getLanguageFileContents() {
        $filename = "lang.{$this->chosenLanguage}.php";
        if (!file_exists($filename)) {
            throw new Exception(__METHOD__ . ": The language file \"$filename\" was not found.");
        }
        include $filename;
        return $lang;
    }
}
?>

      

0


source







All Articles