这篇文章将为大家详细讲解有关使用php怎么实现一个多语言切换功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
创新互联公司自2013年创立以来,先为鲁山等服务建站,鲁山等地企业,进行企业商务咨询服务。为鲁山企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。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怎么实现一个多语言切换功能-创新互联
新闻来源:https://www.cdcxhl.com/article26/hgdjg.html
成都网站建设公司_创新互联,为您提供微信小程序、App设计、企业网站制作、品牌网站建设、小程序开发、全网营销推广
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联