Home  >  Article  >  Backend Development  >  A simple method to implement multi-language switching in php_php skills

A simple method to implement multi-language switching in php_php skills

WBOY
WBOYOriginal
2016-05-16 09:00:051899browse

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:

<?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. language pack included: english.inc

<?php
$name = "name";
?>
chinese.inc
<?php
$name = "名字";
?>

3. contained 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. contained 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;
}

readers who are interested in more php-related content can check out the special topic of this site: "complete php array (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 "a summary of common php database operation skills

i hope this article will be helpful to everyone in php programming.

Statement:
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