程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php簡單實現多語言切換的方法,php語言切換

php簡單實現多語言切換的方法,php語言切換

編輯:關於PHP編程

php簡單實現多語言切換的方法,php語言切換


本文實例講述了php簡單實現多語言切換的方法。分享給大家供大家參考,具體如下:

1.主程序代碼:

<?php
include "lib/function.php";
?>
<script src="js/language.js"></script>
<?php
if(isset($_GET["language"])){
  $_SESSION["language"] = $_GET["language"];
}else{
  $_SESSION["language"] = getDefalutlanguage();
}
$language_name = getLanguageName($_SESSION["language"]);
include "lang/".$language_name.".inc";
?>
<SELECT NAME="language" id="language" onchange="changeLanguage(this)">
<?php
  $language_array = array_language();
  foreach($language_array as $key => $value){
    if($_SESSION["language"] == $value){
      $selected = "selected = 'selected' ";
    }else{
      $selected = "";
    }
?>
<OPTION VALUE="<?php echo $value;?>" <?php echo $selected;?>><?php echo getLanguageName($value);?></OPTION>;
<?
  }
?>
</SELECT>
<?php
  if($_GET["language"] == $value){
      //$selected = "selected = 'selected' ";
    }
echo "語言:".$_SESSION["language"];
echo "測試:".$name;
?>

2. 所包含的語言包:English.inc

<?php
$name = "name";
?>
Chinese.inc
<?php
$name = "名字";
?>

3. 所包含的function.php

<?php
 function getVailableLanguage() {
    $language = array(
      'af'     => 'Afrikaans',
      'az'     => 'Azerbaijani',
      'eu'     => 'Basque',
      'be'     => 'Belarusian',
      'be-lat'   => 'Belarusian latin',
      'bg'     => 'Bulgarian',
      'bs'     => 'Bosnian',
      'ca'     => 'Catalan',
      'zh'     => 'Chinese',
      //'zh-TW'     => 'Chinese traditional',
      //'zh-CN'     => 'Chinese simplified',
      'cs'     => 'Czech',
      'da'     => 'Danish',
      'de'     => 'German',
      'el'     => 'Greek',
      'en'     => 'English',
      'es'     => 'Spanish',
      'et'     => 'Estonian',
      'fa'     => 'Persian',
      'fi'     => 'Finnish',
      'fr'     => 'French',
      'gl'     => 'Galician',
      'he'     => 'Hebrew',
      'hi'     => 'Hindi',
      'hr'     => 'Croatian',
      'hu'     => 'Hungarian',
      'id'     => 'Indonesian',
      'it'     => 'Italian',
      'ja'     => 'Japanese',
      'ko'     => 'Korean',
      'ka'     => 'Georgian',
      'lt'     => 'Lithuanian',
      'lv'     => 'Latvian',
      'mk'     => 'Macedonian',
      'mn'     => 'Mongolian',
      'ms'     => 'Malay',
      'nl'     => 'Dutch',
      'no'     => 'Norwegian',
      'pl'     => 'Polish',
      'pt-BR'    => 'Brazilian portuguese',
      'pt'     => 'Portuguese',
      'ro'     => 'Romanian',
      'ru'     => 'Russian',
      'si'     => 'Sinhala',
      'sk'     => 'Slovak',
      'sl'     => 'Slovenian',
      'sq'     => 'Albanian',
      'sr-lat'   => 'Serbian latin',
      'sr'     => 'Serbian',
      'sv'     => 'Swedish',
      'th'     => 'Thai',
      'tr'     => 'Turkish',
      'tt'     => 'Tatarish',
      'uk'     => 'Ukrainian',
    );
    return $language;
  }
 function getLanguageName($language) {
    $languages = getVailableLanguage();
    return $languages[$language];
  }
 function array_language(){
  $array_language = array("en","zh");
  return $array_language;
 }
 function getDefalutlanguage(){
    return "zh";
 }
?>

4.所包含的language.js

function changeLanguage(obj) {
  var url = document.URL;
  var re = re = /[?&]language=[^&]*/;
  url = url.replace(re, "");
  if (url.indexOf("?") > -1) {
    url += "&language=" + obj.value;
  } else {
    url += "?language=" + obj.value;
  }
  location.href = url;
}

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP數組(Array)操作技巧大全》、《PHP數據結構與算法教程》、《PHP數學運算技巧總結》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved