如何在PHP中实现语言包 [英] How to implement language packs in PHP
问题描述
我为我的网站创建了3个语言包:英文,西班牙文和法文。我根据用户选择无法实现它们。我有以下下拉菜单:
< select onchange ='document.location.href = this.options [this.selectedIndex]。价值;'>
< option>选择< / option>
< option value =?lang = eng> US English< / option>
< option value =?lang = esp>Español< / option>
< option value =?lang = fra>Français< / option>
< / select>
如何根据用户选择的内容来包括语言文件在 if语句中,我只是不知道该如何放在条件中。
谢谢。
首先,保存用户在用户会话中选择的值。例如:
switch($ _ POST ['lang']){
case'en' _SESSION ['lang'] ='English';打破;
case'sp':$ _SESSION ['lang'] ='西班牙语';打破;
default:$ _SESSION ['lang'] ='English';打破;
}
在每个页面请求中,根据相关语言文件夹的语言文件您保存的价值。
例如,这将如何查看文件结构:
- English
- Global.php
- Register.php
- 西班牙语
- Global.php
- Register.php
然后,只要您需要加载文本文件,请使用:
function load_text_file($ filename){
include'languages /'。 $ _SESSION ['lang']。 '/'。 $ filename.'php;
return $ txt; // $ txt应该是一个数组
}
//...
$ text = array();
$ text + = load_text_file('Global');
$ text + = load_text_file('Register');
I created 3 language packs for my site: English, Spanish and French. I am just having trouble implementing them based on user selection. I have the following drop down menu:
<select onchange='document.location.href=this.options[this.selectedIndex].value;'> <option>Select</option> <option value="?lang=eng">US English</option> <option value="?lang=esp">Español</option> <option value="?lang=fra">Français</option> </select>
How can I include the language files based on what the user selects, I just don't know what to put as the condition in the if statement.
Thanks.
At first, save the value that the user selected in the user session. e.g.:
switch($_POST['lang']) {
case 'en': $_SESSION['lang'] = 'English'; break;
case 'sp': $_SESSION['lang'] = 'Spanish'; break;
default: $_SESSION['lang'] = 'English'; break;
}
On every page request, fetch the language files from the relevant language folder according to the value the you saved.
For example, this how will look the files structure:
- English
- Global.php
- Register.php
- Spanish
- Global.php
- Register.php
Then, whenever you need to load a text file, use:
function load_text_file($filename) {
include 'languages/' . $_SESSION['lang'] . '/' . $filename.'php';
return $txt; // $txt should be an array
}
//...
$text = array();
$text += load_text_file('Global');
$text += load_text_file('Register');
这篇关于如何在PHP中实现语言包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!