如何使一个多语言PHP网站? [英] how to make a multi-lingual php site?
问题描述
我正在开发使用PHP和MySQL的站点。我想知道......什么是一个很好的方式来处理多语言支持?我希望用户能够从一个下拉选择菜单并选择他们的语言。然后一切(内容,按钮,链接),除了用户写的内容是他们的语言。
I am developing a site using php and mysql. I want to know... what's a good way to deal with multi-lingual support? I want a user to be able to select from a drop down and select their language. Then everything (content, buttons, links) except the user-written content is in their language.
什么是处理这个好办法?使用cookie?会议?
What's a good way to approach this? Use a cookie? Session?
推荐答案
事情是这样的正常工作:
Something like this works fine:
Langs.php
<?
// check if language switch as been set at url var
if ($_GET["lang_change"]) {
$_SESSION['session_name']["lang"] = $_GET["lang_change"];
}
// set value to lang for verification
$active_lang = $_SESSION['session_name']["lang"];
// verify $lang content and set proper file to be load
switch ($active_lang) {
case 'prt':
$lang_file = 'prt.php';
break;
case 'gbr':
$lang_file = 'gbr.php';
break;
case 'fra' :
$lang_file = 'fra.php';
break;
case 'esp' :
$lang_file = 'esp.php';
break;
case 'deu' :
$lang_file = 'deu.php';
break;
default:
$lang_file = 'gbr.php';
}
// load proper language file for site presentation
include_once ('$lang_file);
?>
LANG GBR FILE(gbr.php)
define("LANG_PAGETITLE_HOMEPAGE", 'Homepage');
define("LANG_BTN_KNOW_MORE", 'know more');
方法来改变语言(URL样本)
<a href="index.php?lang_change=gbr" title"">USE ENG</a>
基本上,你有一个常量PHP文件,每个文件与郎。
Basically, you have PHP files with constants, each file with a lang.
在点击设置的URL VAR(例如:lang_change =朗)
On click you set a url var (ex: lang_change = lang).
这将迫使页面重载,而langs.php文件包括您的index.php顶部将加载所选的语言......
That will force page reload, and the langs.php file include at top of your index.php will load the selected language...
如果您需要了解更多这方面的解释,发表评论,我会送你一个工作示例!
If you need more explanation about this, leave a comment and I'll send you a working sample!
诗:在这个code所示会话变量是有效用于登录系统,或者只是互动,以避免在URL参数...
Ps: session variables shown in this code is usefully for interaction with login systems, or just to avoid having the url parameters...
这篇关于如何使一个多语言PHP网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!