-1

I would like to implement translation on a website and I'm trying out the method found at http://we-love-php.blogspot.com.ar/2012/07/how-to-implement-i18n-without.html

I can do it with ease when the page requested is a complete page but I don't know what to do when the requested page is composed of many included files.

Here what my index.php looks like:

<?php
include 'includes/header.php';
include 'includes/index.php';
include 'includes/footer.php';
?>

And what I've tried is to put the class inside each of the template files like this:

<?php

class example{
  public function now() {
    return <<<FOOBAR
      <!DOCTYPE html>
      <html>
      <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <title>MY SITE's TITLE</title>
      <meta name="keywords" content="SOME KEYWORDS" />
      <meta name="description" content="SOME DESCRIPTION">
      <meta name="author" content="MY NAME">
      ...........
FOOBAR;
}
}
?>

That didn't work as it tries to create the class more than once. Then I've tried creating the class above header and then closing it after footer. It throw me an error as if PHP was trying to open footer first so then I rearranged the order of included files so it includes footer.php first and header.php last. Not only it came out as a mess but also with a warning message at the bottom saying:

Parse error: syntax error, unexpected end of file, expecting variable

How can I solve this? I liked this method of translating my site but need it to work while including templates. Is it possible?

Thanks in advance!

CesarA
  • 85
  • 1
  • 7

1 Answers1

0

Including a file literally tells the compiler to grab the exact contents of one file and copy/paste them into the other when the script is being ran. This means that if you're putting a class declaration into multiple files that are being included you will end up with multiple declarations of that class.

The whole point of using include is to let you break up your code and cut out redundancies within. You should not have the same class, function, or piece of code in multiple files at any point as you will get some very strange behavior and conflicts.

Choose a file to put your class declaration into and ensure it is not in any of the others.

I hope this helps!

aylnon
  • 371
  • 2
  • 8