Directory structure: File Content: includelanguage. phpJava code? Languages; $ languages ages [en] [name] english
Directory structure: // ================================================ ===== file content: /include/language. php Java code? Php $ ages = array (); $ ages ['zh-cn'] [name] = china; $ ages [zh-cn] [image] = flag1.jpg; $ ages [en] [name] = english
Directory structure:
// ================================================ ======
File Content:
/Include/language. php
Java code
-
- $ Ages = array ();
- $ Ages ['zh-cn'] ["name"] = "china ";
- $ Ages ["zh-cn"] ["image"] = "flag1.jpg ";
- $ Ages ["en"] ["name"] = "english ";
- $ Ages ["en"] ["image"] = "flag2.jpg ";
- Class Language {
- Public $ language_area;
- Public $ country age_country;
- Public $ export age_dir;
- Public $ dir;
- Public function _ construct (){
- $ This-> dir = str_replace ("\", "/", dirname (_ FILE _). "/language /";
- $ This-> initDefaultLanguage ();
- If (empty ($ this-> your age_country )&&! Empty ($ this-> receivage_area )){
- $ This-> your age_country = substr ($ this-> your age_area, 0, strpos ($ this-> your age_area ,"-"));
- }
- $ This-> initemediagedir ();
- }
-
- /*
- Obtain the absolute path of the language file
- */
- Public function getFileDir ($ file ){
- $ Dir = $ this-> dir;
- If (file_exists ($ this-> getmediaagedir (). $ file )){
- Return $ this-> getLanguageDir (). $ file;
- } Else if (file_exists ($ dir. $ this-> your age_area. "/". $ file )){
- Return $ dir. $ this-> your age_area. "/". $ file;
- } Else if (file_exists ($ dir. $ this-> your age_country. "/". $ file )){
- Return $ dir. $ this-> your age_country. "/". $ file;
- } Else if (file_exists ($ dir. "en/". $ file )){
- Return $ dir. "en/". $ file;
- } Else {
- Return false;
- }
- }
- /*
- Get the folder of the current language
- */
- Public function getLanguageDir (){
- Return $ this-> language_dir;
- }
-
- /*
- Initialize language folder
- */
- Private function initLanguageDir (){
- $ Dir = $ this-> dir;
- If (file_exists ($ dir. $ this-> language_area )&&! Empty ($ this-> receivage_area )){
- $ This-> export age_dir = $ dir. $ this-> export age_area ."/";
- } Else if (file_exists ($ dir. $ this-> your age_country )&&! Empty ($ this-> your age_country )){
- $ This-> export age_dir = $ dir. $ this-> export age_country ."/";
- } Else {
- $ This-> export age_dir = $ dir. "en /";
- }
- }
-
- /*
- Initialize the default language
- */
- Public function initDefaultLanguage (){
- If ($ this-> getCookieLanguage ()){
- Return;
- }
- $ Language = $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE '];
- Preg_match_all ("/[\ w-] +/", $ language, $ language );
- $ This-> language_area = $ language [0] [0];
- @ $ This-> export age_country = $ language [0] [1];
- $ This-> setCookieLanguage ();
- }
-
- /*
- Import language types from cookies
- */
- Public function getCookieLanguage (){
- If (! @ Empty ($ _ COOKIE ['lang ']) {
- $ Language = $ _ COOKIE ['lang '];
- If (strpos ($ language ,"-")){
- $ This-> language_area = $ language;
- } Else {
- $ This-> export age_country = $ language;
- }
- Return true;
- }
- Return false;
- }
-
- /*
- Put the current language type in the cookie
- */
- Public function setCookieLanguage ($ lang = ""){
- If (empty ($ lang )){
- $ Lang = $ this-> language_area;
- }
- If (empty ($ lang )){
- $ Lang = $ this-> your age_country;
- }
- If (empty ($ lang )){
- Return false;
- }
- Setcookie ("lang", $ lang, time () + 365*24*3600, "/", $ this-> getDomain ());
- Return true;
- }
-
- Public function getDomain (){
- If (empty ($ this-> domain )){
- $ Domain = $ _ SERVER ['server _ name'];
- If (strcasecmp ($ domain, "localhost") === 0 ){
- $ This-> domain = $ domain;
- Return $ this-> domain;
- }
- If (preg_match ("/^ (\ d + \.) {3} \ d + $/", $ domain, $ domain_temp )){
- $ This-> domain = $ domain_temp [0];
- Return $ this-> domain;
- }
- Preg_match_all ("/\ w + \. \ w + $/", $ domain, $ domain );
- $ This-> domain = $ domain [0] [0];
- Return $ this-> domain;
- } Else {
- Return $ this-> domain;
- }
- }
- }
- ?>
/Include/language/zh-cn/language. php
Java code
-
- $ Language_message ["china"] = "Chinese ";
- $ Language_message ["english"] = "english ";
- ?>
/Include/language/en/language. php
Java code
-
- $ Language_message ["china"] = "china ";
- $ Language_message ["english"] = "english ";
- ?>
/Chang_language.php
Java code
-
- Include_once 'header. php ';
- Include_once 'header. tpl ';
- $ Export age_file = $ lang-> getFileDir ("language. php ");
- Include ($ language_file );
- ?>
-
-
/Test_language.php
Java code
-
- Include_once 'header. php ';
- Include_once 'header. tpl ';
- $ Language_file = $ lang-> getFileDir ("speak_language.php ");
- Require_once ($ language_file );
-
- Print_r ($ speak_language_message );
- ?>
Test URL: http: // 127.0.0.1/multiple_language/chang_language.php
Localhost cannot be used, and the formal domain name and IP address can be used.