php简单实现多语言切换的方法
发布:smiling 来源: PHP粉丝网 添加日期:2021-08-02 14:09:52 浏览: 评论:0
这篇文章主要介绍了php简单实现多语言切换的方法,涉及php字符串、数组结合session操作实现多语言切换的相关技巧,需要的朋友可以参考下。
本文实例讲述了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;
- }
Tags: php多语言切换
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)