Home > Backend Development > PHP Tutorial > A simple method to implement multi-language switching in php, php language switching_PHP tutorial

A simple method to implement multi-language switching in php, php language switching_PHP tutorial

WBOY
Release: 2016-07-12 08:52:37
Original
931 people have browsed it

How to simply implement multi-language switching in php, php language switching

The example in this article describes how to simply implement multi-language switching in php. Share it with everyone for your reference, the details are as follows:

1. Main program code:

<&#63;php
include "lib/function.php";
&#63;>
<script src="js/language.js"></script>
<&#63;php
if(isset($_GET["language"])){
  $_SESSION["language"] = $_GET["language"];
}else{
  $_SESSION["language"] = getDefalutlanguage();
}
$language_name = getLanguageName($_SESSION["language"]);
include "lang/".$language_name.".inc";
&#63;>
<SELECT NAME="language" id="language" onchange="changeLanguage(this)">
<&#63;php
  $language_array = array_language();
  foreach($language_array as $key => $value){
    if($_SESSION["language"] == $value){
      $selected = "selected = 'selected' ";
    }else{
      $selected = "";
    }
&#63;>
<OPTION VALUE="<&#63;php echo $value;&#63;>" <&#63;php echo $selected;&#63;>><&#63;php echo getLanguageName($value);&#63;></OPTION>;
<&#63;
  }
&#63;>
</SELECT>
<&#63;php
  if($_GET["language"] == $value){
      //$selected = "selected = 'selected' ";
    }
echo "语言:".$_SESSION["language"];
echo "测试:".$name;
&#63;>

Copy after login

2. Language pack included: English.inc

<&#63;php
$name = "name";
&#63;>
Chinese.inc
<&#63;php
$name = "名字";
&#63;>

Copy after login

3. Contained function.php

<&#63;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";
 }
&#63;>

Copy after login

4. Contained language.js

function changeLanguage(obj) {
  var url = document.URL;
  var re = re = /[&#63;&]language=[^&]*/;
  url = url.replace(re, "");
  if (url.indexOf("&#63;") > -1) {
    url += "&language=" + obj.value;
  } else {
    url += "&#63;language=" + obj.value;
  }
  location.href = url;
}

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Complete PHP Array Operation Skills", "PHP Data Structure and Algorithm Tutorial", "Summary of PHP Mathematical Operation Skills", "php Date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1126070.htmlTechArticleHow to simply implement multi-language switching in php, php language switching This article describes the simple method of php to implement multi-language switching . Share it with everyone for your reference, the details are as follows: 1. Main program code...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template