gpt4 book ai didi

php - 语言选择列表

转载 作者:搜寻专家 更新时间:2023-10-31 21:58:36 26 4
gpt4 key购买 nike

我正在创建选择列表。但我不知道如何根据所选语言更改窗口语言,我需要帮助打开 $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>

最佳答案

在 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>

本地化.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;
}
}
?>

关于php - 语言选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31160794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com