Advertisement
Guest User

Untitled

a guest
Jan 11th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.20 KB | None | 0 0
  1. <?php
  2. namespace LanguageSwitcher;
  3. /**
  4. * @usage:
  5. *   $lang = isset($_GET['lang'])?$_GET['lang']:'ru';
  6. *   (new \LanguageSwitcher\LanguageFileSwitcher(__DIR__ .'/languages/'))->includeLanguage($lang);
  7. */
  8. class LanguageFileSwitcher{
  9.  
  10.     const DEFAULT_CODE = 'en';
  11.     const DEFAULT_EXT = '.php';
  12.  
  13.     protected $sourcePath;
  14.     protected $extention ;
  15.  
  16.     public function __construct($sourcePath, $extention = self::DEFAULT_EXT){
  17.         $this->sourcePath = $sourcePath;
  18.         $this->extention = $extention;
  19.     }
  20.  
  21.     public function inludeLanguage($code = self::DEFAULT_CODE){
  22.         $filename = $this->getFilenameByCode($code);
  23.         if(in_array($filename, $this->getAvailableLanguagesFiles())){
  24.             include $filename;
  25.             return;
  26.         }else{
  27.             $defaultSource = $this->getFilenameByCode(self::DEFAULT_CODE);
  28.             if(file_exists($defaultSource)){
  29.                 include $defaultSource;
  30.                 return;
  31.             }else{
  32.                 throw new Exception("Not found default language file! ");
  33.             }
  34.         }
  35.     }
  36.  
  37.     protected function getFilenameByCode($code){
  38.         return $this->sourcePath . $code . $this->extention;
  39.     }
  40.  
  41.     protected function getAvailableLanguagesFiles(){
  42.         return glob($this->sourcePath . '*' . $this->extention);
  43.     }
  44. }
  45.  
  46. class Exception extends \RuntimeException {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement