目录结构: //============================================ 文件内容: /include/language.

目录结构:

php 多语言切换-LMLPHP

//============================================

文件内容:

/include/language.

Java代码 php 多语言切换-LMLPHP

  1. $languages = array ();
  2. $languages ['zh-cn'] ["name"] = "china";
  3. $languages ["zh-cn"] ["image"] = "flag1.jpg";
  4. $languages ["en"] ["name"] = "english";
  5. $languages ["en"] ["image"] = "flag2.jpg";
  6. class Language {
  7. public $language_area;
  8. public $language_country;
  9. public $language_dir;
  10. public $dir;
  11. public function __construct() {
  12. $this->dir = str_replace ( "\\", "/", dirname ( __FILE__ ) ) . "/language/";
  13. $this->initDefaultLanguage ();
  14. if (empty ( $this->language_country ) && ! empty ( $this->language_area )) {
  15. $this->language_country = substr ( $this->language_area, 0, strpos ( $this->language_area, "-" ) );
  16. }
  17. $this->initLanguageDir ();
  18. }
  19. /*
  20. 取得语言文件的绝对路径
  21. */
  22. public function getFileDir($file) {
  23. $dir = $this->dir;
  24. if (file_exists ( $this->getLanguageDir () . $file )) {
  25. return $this->getLanguageDir () . $file;
  26. } else if (file_exists ( $dir . $this->language_area . "/" . $file )) {
  27. return $dir . $this->language_area . "/" . $file;
  28. } else if (file_exists ( $dir . $this->language_country . "/" . $file )) {
  29. return $dir . $this->language_country . "/" . $file;
  30. } else if (file_exists ( $dir . "en/" . $file )) {
  31. return $dir . "en/" . $file;
  32. } else {
  33. return false;
  34. }
  35. }
  36. /*
  37. 取得当前使用语言的文件夹
  38. */
  39. public function getLanguageDir() {
  40. return $this->language_dir;
  41. }
  42. /*
  43. 初始化语言文件夹
  44. */
  45. private function initLanguageDir() {
  46. $dir = $this->dir;
  47. if (file_exists ( $dir . $this->language_area ) && ! empty ( $this->language_area )) {
  48. $this->language_dir = $dir . $this->language_area . "/";
  49. } else if (file_exists ( $dir . $this->language_country ) && ! empty ( $this->language_country )) {
  50. $this->language_dir = $dir . $this->language_country . "/";
  51. } else {
  52. $this->language_dir = $dir . "en/";
  53. }
  54. }
  55. /*
  56. 初始化默认语言
  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->language_country = $language [0] [1];
  66. $this->setCookieLanguage ();
  67. }
  68. /*
  69. 从cookie中导入语言种类
  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->language_country = $language;
  78. }
  79. return true;
  80. }
  81. return false;
  82. }
  83. /*
  84. 把当前的语言种类放到cookie中
  85. */
  86. public function setCookieLanguage($lang = "") {
  87. if (empty ( $lang )) {
  88. $lang = $this->language_area;
  89. }
  90. if (empty ( $lang )) {
  91. $lang = $this->language_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.

Java代码 php 多语言切换-LMLPHP

  1. $language_message["china"]="中文";
  2. $language_message["english"]="英文";
  3. ?>

/include/language/en/language.

Java代码 php 多语言切换-LMLPHP

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

/chang_language.

Java代码 php 多语言切换-LMLPHP

  1. include_once 'header.;
  2. include_once 'header.tpl';
  3. $language_file=$lang->getFileDir("language.);
  4. include($language_file);
  5. ?>

/header.

Java代码 php 多语言切换-LMLPHP

  1. include_once (dirname ( __FILE__ ) . "/include/language.);
  2. $lang = new Language ( );
  3. //print_r($lang->language_area);
  4. $current_language = getCurrentLanguage ();
  5. //print_r($current_language);
  6. function getCurrentLanguage() {
  7. global $lang, $languages;
  8. if (array_key_exists ( $lang->language_area, $languages )) {
  9. return $lang->language_area;
  10. } else if (array_key_exists ( $lang->language_country, $languages )) {
  11. return $lang->language_country;
  12. } else {
  13. return "en";
  14. }
  15. }
  16. ?>

/header.tpl

Html代码 php 多语言切换-LMLPHP

  1. >
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>多语言测试title>
  6. <link href="css/css.css" rel="stylesheet" type="text/css" />
  7. <script type="text/javascript" src="javascript/prototype_1_6.js">script>
  8. head>

/test_language.

Java代码 php 多语言切换-LMLPHP

  1. include_once 'header.;
  2. include_once 'header.tpl';
  3. $language_file = $lang->getFileDir ( "speak_language. );
  4. require_once ($language_file);
  5. print_r($speak_language_message);
  6. ?>

测试地址为:http://127.0.0.1/multiple_language/chang_language.

localhost不能使用,正式域名及IP地址可以使用。

09-02 22:24