PHP - HTML本地化无法正常工作

时间:2015-07-30 16:42:24

标签: php html localization

我正在努力建立一个能够支持英语和西班牙语两种语言的网站。我仍然从所有这些网站创建开始,所以我花了很多时间试图找到一个我能理解的本地化教程。最后,我发现了一个非常有趣的(http://www.webreference.com/programming/php/localization/index.html),但是没有用。使用时 <? echo WELCOME_TXT ?> 在标题上,它实际显示代码行而不是WELCOME_TXT文本。我尝试过使用“WELCOME_TXT”,常量(“WELCOME_TXT”)等,但没有一个有效。我做错了什么?

这是我的代码: lang.php

<?php
  if ($_SESSION[lang] == "") {
    $_SESSION[lang] = "en";
    $currLang = "en";
  } else {
    $currLang = $_GET[lang];
    $_SESSION[lang] = $currLang;
  }
  switch($currLang) {
    case "en":
      define("CHARSET","ISO-8859-1");
      define("LANGCODE", "en");
      break;
    case "de":
      define("CHARSET","ISO-8859-1");
      define("LANGCODE", "de");
      break;
    case "ja":
      define("CHARSET","UTF-8");
      define("LANGCODE", "ja");
      break;
    default:
      define("CHARSET","ISO-8859-1");
      define("LANGCODE", "en");
      break;
  }
header("Content-Type: text/html;charset=".CHARSET);
header("Content-Language: ".LANGCODE);
?>

txt.php

<?php
  function defineStrings() {
    switch($_SESSION[lang]) {
      case "en":
        define("WELCOME_TXT","Welcome!");
        define("CHOOSE_TXT","Choose Language");
        break;
      case "de":
        define("WELCOME_TXT","Willkommen!");
        define("CHOOSE_TXT","Sprache auswählen");
        break;
      case "ja":
        define("WELCOME_TXT","[ Japanese characters here]");
        define("CHOOSE_TXT","[ Japanese characters here]");
        break;
      default:
        define("WELCOME_TXT","Welcome!");
        define("CHOOSE_TXT","Choose Language");
        break;
    }
  }
?>

的index.php

<?
  session_start();
  include("lang.php");
  include("txt.php");
  defineStrings();
?>
<HTML>
<HEAD>
<TITLE><? echo WELCOME_TXT ?></TITLE>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=<? echo CHARSET; ?>">
<META HTTP-EQUIV="Content-Language" content="<? echo LANGCODE; ?>">
<BODY>
<h1 align=center><? echo WELCOME_TXT; ?></h1>
  <p align=center><strong><? echo CHOOSE_TXT; ?></strong><br><br>
<a href="<? echo $_SERVER[PHP_SELF].'?lang=en'; ?>"><img src="en_flag.gif" border=0></a>
<a href="<? echo $_SERVER[PHP_SELF].'?lang=de'; ?>"><img src="de_flag.gif" border=0></a>
<a href="<? echo $_SERVER[PHP_SELF].'?lang=ja'; ?>"><img src="ja_flag.gif" border=0></a>
</p>
</BODY>
</HTML>

(我知道这是教程代码,但它对我来说都不起作用)

0 个答案:

没有答案