简体   繁体   English

如何从这个example.com/index.php?lang=en更改我的多语言网址到这个example.com/en/?

[英]How can I change my multilingual urls from this example.com/index.php?lang=en to this example.com/en/?

Somebody told me that example.com/en is more friendly than example.com/index.php?lang=en . 有人告诉我, example.com/enexample.com/index.php?lang=en更友好。 How can I change my URL last part from ?lang=en to /en /? 如何将我的URL最后一部分从?lang=en更改为/en /?

(pd: I don't want to use gettext or any framework like zend to accomplish this) (pd:我不想使用gettext或像zend这样的任何框架来实现这一点)

This is how I'm internationalizing and localizing my web page: 这就是我将我的网页国际化和本地化的方式:

(live example: alexchen.co.nr/ ) (实例: alexchen.co.nr/

lang.php: 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']='zh-tw';

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

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

session.php: session.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:
  //set cookie
  $langs = array('en','es','zh-tw', 'zh-cn');

  if(isset($_GET['lang'])){
   if(in_array($_GET['lang'],$langs)){
    $this->lang =  $_SESSION['lang'] = $_GET['lang'];
    setcookie("lang", $_SESSION['lang'], time() + (3600 * 24 * 30));
   }
  }
  else if(isSet($_COOKIE['lang'])) {
   $_SESSION['lang'] = $_COOKIE['lang'];
  }
  else {
   $_SESSION['lang'] = 'zh-tw';
  }
 }
};
/**
 * Initialize session object - This must be initialized before g
 * the form object because the form uses session variables,
 * which cannot be accessed unless the session has started.
 */
$session = new Session;
?>

localization.php: localization.php:

    <?php
    include('session.php'); //language detector

    // determine the value of $lang_file according the one in $lang
    $languages = array('en', 'es', 'zh-tw', 'zh-cn');
    if (in_array($_SESSION['lang'], $languages)) {
        $lang_file = 'lang.'.$_SESSION['lang'].'.php';
    } else {
        $lang_file = 'lang.zh-tw.php';
    }

    // translation helper function
    function l($localization) {
     global $lang;
     return $lang[$localization]; }

    // include file for final output
     include_once 'languages/'.$lang_file;
?>

ModRewrite is your friend. ModRewrite是你的朋友。 Throw this in your .htaccess file. 将此内容放在.htaccess文件中。 You may want it to go to your session page instead of index, then redirect with PHP. 您可能希望它转到会话页面而不是索引,然后使用PHP重定向。 Note , this only works for one page. 请注意 ,这仅适用于一个页面。

RewriteEngine On
RewriteBase /

# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将example.com/index.php/lib更改为example.com/lib? - How to change example.com/index.php/lib to example.com/lib? 将example.com/dashboard.php更改为example.com/index.php?action=dashboard - Change example.com/dashboard.php to example.com/index.php?action=dashboard 不需要来自 example.com/{lang}/index.php 的 index.php - Not needing index.php from example.com/{lang}/index.php 如何将example.com/forum/index.php重定向到example.com/forum/ - How to redirect example.com/forum/index.php to example.com/forum/ Ubuntu-如何直接将example.com/opencart/index.php定向到http://example.com/和www.example.com? - Ubuntu - How direct example.com/opencart/index.php to http://example.com/ and www.example.com? 如何将example.com/index.php#anchor的URL重写为example.com/anchor? - How do I rewrite the URL for example.com/index.php#anchor to example.com/anchor? 是否可以将 example.com/index.php?page=abc 重定向到 example.com/abc? - Is it possible to redirect the example.com/index.php?page=abc to example.com/abc? 使用,htaccess将example.com?id=12重定向到example.com/index.php?id=12 - using ,htaccess redirect example.com?id=12 to example.com/index.php?id=12 如何使用.htaccess将http://example.com/test.php设置为http://example.com/index.php?do=test? - How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess? “ example.com/index.php/page/about/”是好的做法吗? - Is “example.com/index.php/page/about/” good practice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM