欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php 多语言切换

程序员文章站 2022-04-10 13:12:24
...

目录结构: //============================================ 文件内容: /include/language.php Java代码 ?php $languages=array(); $languages[ 'zh-cn' ][ name ]= china ; $languages[ zh-cn ][ image ]= flag1.jpg ; $languages[ en ][ name ]= english

目录结构:

php 多语言切换

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

文件内容:

/include/language.php

Java代码 php 多语言切换

  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.php

Java代码 php 多语言切换

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

/include/language/en/language.php

Java代码 php 多语言切换

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

/chang_language.php

Java代码 php 多语言切换

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