php简单实现多语言切换的方法_php技巧

WBOY
发布: 2016-05-16 09:00:05
原创
1950 人浏览过

本文实例讲述了php简单实现多语言切换的方法。分享给大家供大家参考,具体如下:

1.主程序代码:

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

登录后复制

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

<&#63;php
$name = "name";
&#63;>
Chinese.inc
<&#63;php
$name = "名字";
&#63;>

登录后复制

3. 所包含的function.php

<&#63;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";
 }
&#63;>

登录后复制

4.所包含的language.js

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

登录后复制

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《PHP数学运算技巧总结》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家PHP程序设计有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板