您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用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怎么實現一個多語言切換功能就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。