首頁 > 後端開發 > php教程 > php簡單實作多語言切換的方法_php技巧

php簡單實作多語言切換的方法_php技巧

WBOY
發布: 2016-05-16 09:00:05
原創
1976 人瀏覽過

本文實例講述了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程式設計有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板