我正在创建选择列表。但我不知道如何根据所选语言更改窗口语言,我需要通过打开$lang.es
和$lang.en
文件中的这些翻译来获得帮助。以下是一个例子:
<?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';
?>
这是我的html php脚本,下拉列表是:
<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>
答案 0 :(得分:0)
有许多方法可以在PHP中处理本地化。如another question所述,您可以使用PHP gettext
。你也可以使用常量,store in JSON format,存储在数据库或数组中,就像你正在做的那样。
要使脚本正常工作,首先需要重定向到PHP脚本,设置要加载的语言。然后让你的脚本有条件地打开其中一个文件。
这是一个简单的实现,带有一个用于处理语言文件加载和检索的类。
主要PHP脚本
<?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;
}
}
?>