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

yipeiwu_com5年前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程序设计有所帮助。

相关文章

PHP 信号管理知识整理汇总

SIGQUIT    建立CORE文件终止进程,并且生成core文件 SIGILL     建立CORE文件&nbs...

关于PHP5 Session生命周期介绍

它是通过 Session ID 来判断的,什么是 Session ID,就是那个 Session 文件的文件名,Session ID 是随机生成的,因此能保证唯一性和随机性,确保Sess...

php实现的click captcha点击验证码类实例

php实现的click captcha点击验证码类实例

本文实例讲述了php实现的click captcha点击验证码类及其用法,是非常实用的功能。分享给大家供大家参考之用。具体如下: 一、需求: 现在常用的表单验证码大部分都是要用户输入为主...

PHP数据库调用类调用实例(详细注释)

复制代码 代码如下:<?PHP require_once("mssql.class.php"); //1.创建类,并连接数据库 $db = new mssql("dns=aaa;u...

PHP实现正则表达式分组捕获操作示例

PHP实现正则表达式分组捕获操作示例

本文实例讲述了PHP实现正则表达式分组捕获操作。分享给大家供大家参考,具体如下: 经过测试,发现php正则表达式获取分组捕获是从$0开始,而平时工作中JavaScript中的正则是$1....