1

本教程是关于根据浏览器上的首选语言选择语言。

lang.php:

<?php

function dlang($Var)
{
 if(empty($GLOBALS[$Var]))
 {
  $GLOBALS[$Var]=(!empty($GLOBALS['_SERVER'][$Var]))?
  $GLOBALS['_SERVER'][$Var]:
  (!empty($GLOBALS['HTTP_SERVER_VARS'][$Var]))?
  $GLOBALS['HTTP_SERVER_VARS'][$Var]:'';
 }
}

function language()
{
 // Detect HTTP_ACCEPT_LANGUAGE & HTTP_USER_AGENT.
 dlang('HTTP_ACCEPT_LANGUAGE');
 dlang('HTTP_USER_AGENT');

 $_AL=strtolower($GLOBALS['HTTP_ACCEPT_LANGUAGE']);
 $_UA=strtolower($GLOBALS['HTTP_USER_AGENT']);

 // Try to detect Primary language if several languages are accepted.
 foreach($GLOBALS['_LANG'] as $K)
 {
  if(strpos($_AL, $K)===0)
   return $K;
 }

 // Try to detect any language if not yet detected.
 foreach($GLOBALS['_LANG'] as $K)
 {
  if(strpos($_AL, $K)!==false)
   return $K;
 }
 foreach($GLOBALS['_LANG'] as $K)
 {
  if(preg_match("/[\[\( ]{$K}[;,_\-\)]/",$_UA))
   return $K;
 }

 // Return default language if language is not yet detected.
 return $GLOBALS['_DLANG'];
}

// Define default language.
$GLOBALS['_DLANG']='es';

// Define all available languages.
// WARNING: uncomment all available languages

$GLOBALS['_LANG'] = array(
'es',
'en',
'zh'
);
?>

会话.php:

<?php
//proc all page display
include('lang.php'); //language detector
class Session
{
 var $lang;         //Username given on sign-up
 var $url;          //The page url current being viewed
    var $referrer;     //Last recorded site page viewed

 /* Class constructor */
   function Session(){
      $this->time = time();
      $this->startSession();
   }

   function cf($filename){//function to clean a filename string so it is a valid filename
     $fp = explode('/',$filename);
  $num = count($fp);
  return $fp[$num-1];
 }

   /**
    * startSession - Performs all the actions necessary to 
    * initialize this session object. Tries to determine if the
    * the user has logged in already, and sets the variables 
    * accordingly. Also takes advantage of this page load to
    * update the active visitors tables.
    */
   function startSession(){
     session_start();   //Tell PHP to start the session

  /* Set referrer page */
      if(isset($_SESSION['url'])){
         $this->referrer = $search = $this->cf($_SESSION['url']);
      }else{
         $this->referrer = "/";
      }

      /* Set current url */
      $this->url = $_SESSION['url'] = $this->cf($_SERVER['PHP_SELF']);

   /* Set user-determined language: */
    //set up languages array:
    $langs = array('en','es','zh');
  //
   if(isset($_GET['lang'])){
    if(in_array($_GET['lang'],$langs)){
   $this->lang =  $_SESSION['lang'] = $_GET['lang'];
  }
   }
   if(!isset($_SESSION['lang']) || !in_array($_SESSION['lang'],$langs)){
      $this->lang = $_SESSION['lang'] = language();
   }
 }
};
/**
 * Initialize session object - This must be initialized before
 * the form object because the form uses session variables,
 * which cannot be accessed unless the session has started.
 */
$session = new Session;
?>

现在我想根据(en, es, etc...)的值来选择一个lang.en.php或者一个lang.es.php 。$lang我希望每种语言的内容都有自己的文件。

我认为这段代码可能有效:

// use appropiate lang.xx.php file according to the value of the $lang
switch ($lang) {
case 'en':
 $lang_file = 'lang.en.php';
 break;

case 'es':
 $lang_file = 'lang.es.php';
 break;

case 'tw':
 $lang_file = 'lang.tw.php';
 break;

case 'cn':
 $lang_file = 'lang.cn.php';
 break;

default:
 $lang_file = 'lang.en.php';
}

 include_once 'languages/'.$lang_file;
?>

lang.en.php(示例):

<?php
$lang = array(
 'tagline_h2' => 'English Tagline',

我将该 switch 语句放在session.php文件的末尾,但无论我做什么,它都会卡住显示默认语言 (en)。

有什么建议么?

4

1 回答 1

1

尝试回$lang显,看看它的价值是什么——这样你就可以先看看这是否是问题所在?另外,我建议你把开关换成普通的

include_once("languages/lang.$lang.php");

如果你想检查它是否是一种有效的语言,你可以将它与数组进行比较,或者首先检查文件是否存在。

于 2010-02-28T08:59:54.267 回答