1

I'm trying to build a website which will be able to support two languages, English and Spanish. I'm still starting with all this website creation thing, so I spent a lot of time trying to find a tutorial on localization that I could understand. Finally, I found one which looked very interesting (http://www.webreference.com/programming/php/localization/index.html), but that didn't work. When using <? echo WELCOME_TXT ?> on the title, it actually displays that code line instead of the WELCOME_TXT text. I've tried using "WELCOME_TXT", constant("WELCOME_TXT"), etc and none has worked. What am I doing wrong?

Here's my code: 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>

(I know it's the tutorial code, but it doesn't work for me neither)

eric.m
  • 1,577
  • 10
  • 20
  • Do you have short tags enabled? In your other two pages you open PHP with ` – chris85 Jul 30 '15 at 16:46
  • I've enabled them now using the http://stackoverflow.com/a/2185336/3779214 .htaccess thing (I'm hosting it using a friend's FTP) but now it shows an error when accessing the page. Is there any other option? – eric.m Jul 30 '15 at 17:10
  • Why not just not use the short tags? Whats the new error? – chris85 Jul 30 '15 at 17:36
  • Okay, so I changed ' – eric.m Jul 30 '15 at 17:54
  • No, wrong way. `lang` and `txt` were fine they had the full tag. Put them back. Change `index.php` to have the full tag. – chris85 Jul 30 '15 at 17:56
  • Oh, okay, changed it back. It still doesn't work: http://pastebin.com/4NXkXrbK – eric.m Jul 30 '15 at 18:12
  • The code here and on pastebin are different. Take the constant out of quotes. http://stackoverflow.com/questions/1563654/quoting-constants-in-php-this-is-a-my-constant – chris85 Jul 30 '15 at 18:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84731/discussion-between-chris85-and-emd4600). – chris85 Jul 30 '15 at 18:42

0 Answers0