Php multi-language switch

Source: Internet
Author: User
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

  1. $ Ages = array ();
  2. $ Ages ['zh-cn'] ["name"] = "china ";
  3. $ Ages ["zh-cn"] ["image"] = "flag1.jpg ";
  4. $ Ages ["en"] ["name"] = "english ";
  5. $ Ages ["en"] ["image"] = "flag2.jpg ";
  6. Class Language {
  7. Public $ language_area;
  8. Public $ country age_country;
  9. Public $ export age_dir;
  10. Public $ dir;
  11. Public function _ construct (){
  12. $ This-> dir = str_replace ("\", "/", dirname (_ FILE _). "/language /";
  13. $ This-> initDefaultLanguage ();
  14. If (empty ($ this-> your age_country )&&! Empty ($ this-> receivage_area )){
  15. $ This-> your age_country = substr ($ this-> your age_area, 0, strpos ($ this-> your age_area ,"-"));
  16. }
  17. $ This-> initemediagedir ();
  18. }
  19. /*
  20. Obtain the absolute path of the language file
  21. */
  22. Public function getFileDir ($ file ){
  23. $ Dir = $ this-> dir;
  24. If (file_exists ($ this-> getmediaagedir (). $ file )){
  25. Return $ this-> getLanguageDir (). $ file;
  26. } Else if (file_exists ($ dir. $ this-> your age_area. "/". $ file )){
  27. Return $ dir. $ this-> your age_area. "/". $ file;
  28. } Else if (file_exists ($ dir. $ this-> your age_country. "/". $ file )){
  29. Return $ dir. $ this-> your age_country. "/". $ file;
  30. } Else if (file_exists ($ dir. "en/". $ file )){
  31. Return $ dir. "en/". $ file;
  32. } Else {
  33. Return false;
  34. }
  35. }
  36. /*
  37. Get the folder of the current language
  38. */
  39. Public function getLanguageDir (){
  40. Return $ this-> language_dir;
  41. }
  42. /*
  43. Initialize language folder
  44. */
  45. Private function initLanguageDir (){
  46. $ Dir = $ this-> dir;
  47. If (file_exists ($ dir. $ this-> language_area )&&! Empty ($ this-> receivage_area )){
  48. $ This-> export age_dir = $ dir. $ this-> export age_area ."/";
  49. } Else if (file_exists ($ dir. $ this-> your age_country )&&! Empty ($ this-> your age_country )){
  50. $ This-> export age_dir = $ dir. $ this-> export age_country ."/";
  51. } Else {
  52. $ This-> export age_dir = $ dir. "en /";
  53. }
  54. }
  55. /*
  56. Initialize the default language
  57. */
  58. Public function initDefaultLanguage (){
  59. If ($ this-> getCookieLanguage ()){
  60. Return;
  61. }
  62. $ Language = $ _ SERVER ['HTTP _ ACCEPT_LANGUAGE '];
  63. Preg_match_all ("/[\ w-] +/", $ language, $ language );
  64. $ This-> language_area = $ language [0] [0];
  65. @ $ This-> export age_country = $ language [0] [1];
  66. $ This-> setCookieLanguage ();
  67. }
  68. /*
  69. Import language types from cookies
  70. */
  71. Public function getCookieLanguage (){
  72. If (! @ Empty ($ _ COOKIE ['lang ']) {
  73. $ Language = $ _ COOKIE ['lang '];
  74. If (strpos ($ language ,"-")){
  75. $ This-> language_area = $ language;
  76. } Else {
  77. $ This-> export age_country = $ language;
  78. }
  79. Return true;
  80. }
  81. Return false;
  82. }
  83. /*
  84. Put the current language type in the cookie
  85. */
  86. Public function setCookieLanguage ($ lang = ""){
  87. If (empty ($ lang )){
  88. $ Lang = $ this-> language_area;
  89. }
  90. If (empty ($ lang )){
  91. $ Lang = $ this-> your age_country;
  92. }
  93. If (empty ($ lang )){
  94. Return false;
  95. }
  96. Setcookie ("lang", $ lang, time () + 365*24*3600, "/", $ this-> getDomain ());
  97. Return true;
  98. }
  99. Public function getDomain (){
  100. If (empty ($ this-> domain )){
  101. $ Domain = $ _ SERVER ['server _ name'];
  102. If (strcasecmp ($ domain, "localhost") === 0 ){
  103. $ This-> domain = $ domain;
  104. Return $ this-> domain;
  105. }
  106. If (preg_match ("/^ (\ d + \.) {3} \ d + $/", $ domain, $ domain_temp )){
  107. $ This-> domain = $ domain_temp [0];
  108. Return $ this-> domain;
  109. }
  110. Preg_match_all ("/\ w + \. \ w + $/", $ domain, $ domain );
  111. $ This-> domain = $ domain [0] [0];
  112. Return $ this-> domain;
  113. } Else {
  114. Return $ this-> domain;
  115. }
  116. }
  117. }
  118. ?>

/Include/language/zh-cn/language. php

Java code

  1. $ Language_message ["china"] = "Chinese ";
  2. $ Language_message ["english"] = "english ";
  3. ?>

/Include/language/en/language. php

Java code

  1. $ Language_message ["china"] = "china ";
  2. $ Language_message ["english"] = "english ";
  3. ?>

/Chang_language.php

Java code

  1. Include_once 'header. php ';
  2. Include_once 'header. tpl ';
  3. $ Export age_file = $ lang-> getFileDir ("language. php ");
  4. Include ($ language_file );
  5. ?>

/Test_language.php

Java code

  1. Include_once 'header. php ';
  2. Include_once 'header. tpl ';
  3. $ Language_file = $ lang-> getFileDir ("speak_language.php ");
  4. Require_once ($ language_file );
  5. Print_r ($ speak_language_message );
  6. ?>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.