欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 22:07:10
本文实例讲述了php简单实现多语言切换的方法。分享给大家供大家参考,具体如下: 1.主程序代码:

本文实例讲述了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'     => '*n',
      '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程序设计有所帮助。

上一篇:

下一篇: