且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使一个多语言PHP网站?

更新时间:2023-01-22 11:23:20

事情是这样的正常工作:

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