Advertisement
Guest User

Igor Hlina

a guest
Feb 4th, 2009
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.79 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Localize.php
  4.  * this file is contains Localize class definition
  5.  *
  6.  * @copyright Copyright (c) 2009 Igor Hlina
  7.  * @license read LICENCE.txt
  8.  *
  9.  */
  10.  
  11.  
  12. /**
  13.  * Localize class
  14.  * This is only example of class, which provides localized messages.
  15.  * Localized messages is in this example stored in simple array.
  16.  * Feel free to implement custom storage engine.
  17.  *
  18.  */
  19. class Localize
  20. {
  21.  
  22.     /**
  23.      * Associative array with translations
  24.      *
  25.      * @var array
  26.      */
  27.     private $translations = array(
  28.         'sk' => array(
  29.             'Error404' => 'Stránka sa nenašla',
  30.             'ErrorDatabase' => 'Databázová chyba',
  31.             'TitleIndex' => 'Vitajte',
  32.             'TitleNews' => 'Novinky',
  33.             'TitleTable' => 'Príklad tabulky',
  34.         ),
  35.  
  36.         'en' => array(
  37.             'Error404' => 'Page not found',
  38.             'ErrorDatabase' => 'Database Error',
  39.             'TitleIndex' => 'Welcome',
  40.             'TitleNews' => 'News',
  41.             'TitleTable' => 'Table example',
  42.         )
  43.     );
  44.  
  45.  
  46.     /**
  47.      * Constructor
  48.      *
  49.      */
  50.     public function __construct()
  51.     {
  52.        
  53.     }
  54.  
  55.  
  56.     /**
  57.      * Return localized message identified by given name
  58.      *
  59.      * @param string $name
  60.      * @param string $lang
  61.      * @return string
  62.      */
  63.     public function getLocalizedMessage($name, $lang)
  64.     {
  65.         if (array_key_exists($lang, $this->translations)) {
  66.  
  67.             if (array_key_exists($name, $this->translations[$lang])) {
  68.                 return $this->translations[$lang][$name];
  69.  
  70.             } else {
  71.                 throw new Exception("Localized message '$name' not found!");
  72.             }
  73.  
  74.         } else {
  75.             throw new Exception("Translations for language '$lang' not found!");
  76.         }
  77.     }
  78.  
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement