41

Images and translations

Create site3 by copying site2.

  1. /cms
    1. ...
    2. site2
    3. site3

In this chapter, we are going to program the content of the home page and its translation.

To test the result online, enter http://www.frasq.org/cms/site3 in the address bar of your navigator. Try the addresses http://www.frasq.org/cms/site3/en and http://www.frasq.org/cms/site3/fr.

Create the folder images in site3.

  1. /cms/site3
    1. images

Add the icon for Ubuntu in images.

  1. /cms/site3
    1. images
      1. ubuntu.png

Add a footer with the Ubuntu icon to home.phtml. In English in views/en:

  1. <h3>Welcome</h3>
  2. <div id="footer">
  3. <p>&copy;2010-2011 frasq.org - All rights reserved - <a href="http://www.frasq.org">www.frasq.org</a></p>
  4. <p><img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16"/>&nbsp;<a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
  5. </div>

Then in French in views/fr:

  1. <h3>Bienvenue</h3>
  2. <div id="footer">
  3. <p>&copy;2010-2011 frasq.org - Tous droits r&eacute;serv&eacute;s - <a href="http://www.frasq.org">www.frasq.org</a></p>
  4. <p><img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16" />&nbsp;<a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
  5. </div>

If you enter http://localhost/cms/site3/en in the address bar of your navigator, the icon isn't displayed. Remember that the file .htaccess orders Apache to redirect all requests to index.php. index.php will return a "HTTP/1.0 404 Not Found" document. Install the extension Live HTTP Headers of Firefox. Choose Live HTTP headers from the Tools menu. Reload the page http://localhost/cms/site3/en to display the header of the document returned by the site.

GET /cms/site3/images/ubuntu.png HTTP/1.1
Host: localhost
...

HTTP/1.0 404 Not Found
...

Edit .htaccess and add the following lines to let the files with the extensions .gif, .png, .jpg, .jpeg or .ico and .css for style sheets and .js for sources in JavaScript pass through.

  1. RewriteCond %{REQUEST_FILENAME} -f
  2. RewriteRule /*\.(css|js|gif|png|jpe?g|ico)$ - [NC,L]

The first line imposes that the URL actually points to an existing file. Otherwise, the request is redirected to index.php. This condition is important so the site can capture addressing errors or manage downloading files whose names have extensions which could be confusing. The next rule indicates that all the URL ending with one of the given extensions mustn't be rewritten.

Check that the icon is properly displayed by entering http://localhost/cms/site3/fr and http://localhost/cms/site3/en in the address bar of your navigator.

Add the file strings.inc in includes with the following content:

  1. /cms/site3
    1. includes
      1. strings.inc
  1. global $strings;
  2.  
  3. $strings = array(
  4.     array('title'               => 'frasq.org',
  5.     ),
  6.     'fr' => array(
  7.         'home:title'            => 'Accueil',
  8.     ),
  9.     'en' => array(
  10.         'home:title'            => 'Home',
  11.     ),
  12. );

strings.inc defines the global variable $strings. $strings holds a table which associates for each language managed by the program a list of character strings and their translations. The array of strings which are not language dependent is placed at position 0.

Add the file translate.php in library with the following content:

  1. global $strings;
  2.  
  3. $strings = array();
  4.  
  5. @include 'strings.inc';
  6.  
  7. function translate($s, $lang) {
  8.     global $strings;
  9.  
  10.     if ($s) {
  11.         if (array_key_exists($lang, $strings) && array_key_exists($s, $strings[$lang])) {
  12.             return $strings[$lang][$s];
  13.         }
  14.         if (array_key_exists(0, $strings) && array_key_exists($s, $strings[0])) {
  15.             return $strings[0][$s];
  16.         }
  17.     }
  18.  
  19.     return false;
  20. }

Modify the English version of the home page:

  1. <?php
  2. require_once 'translate.php';
  3.  
  4. $title=translate('home:title', 'en');
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <title><?php echo $title; ?></title>
  10. </head>
  11. <body>
  12. <h3>Welcome</h3>
  13. <div id="footer">
  14. <p>&copy;2010-2011 frasq.org - All rights reserved - <a href="http://www.frasq.org">www.frasq.org</a></p>
  15. <p><img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16"/>&nbsp;<a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
  16. </div>
  17. </body>
  18. </html>

The title of the page is now generated dynamically according to the language of the page which is displayed.

In the French version, 'fr' is passed in argument to translate.

  1. <?php
  2. require_once 'translate.php';
  3.  
  4. $title=translate('home:title', 'fr');
  5. ?>
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  7. <html xmlns="http://www.w3.org/1999/xhtml">
  8. <head>
  9. <title><?php echo $title; ?></title>
  10. </head>
  11. <body>
  12. <h3>Bienvenue</h3>
  13. <div id="footer">
  14. <p>&copy;2010-2011 frasq.org - Tous droits r&eacute;serv&eacute;s - <a href="http://www.frasq.org">www.frasq.org</a></p>
  15. <p><img src="<?php echo $base_path; ?>/images/ubuntu.png" alt="" width="16" height="16" />&nbsp;<a href="http://www.ubuntu.com" target="_blank">Ubuntu</a></p>
  16. </div>
  17. </body>
  18. </html>

Enter http://localhost/cms/site3/en then http://localhost/cms/site3/fr in the address bar of your navigator to validate the home page in English and in French.

Comments

To add a comment, click here.