Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace LanguageSwitcher;
- /**
- * @usage:
- * $lang = isset($_GET['lang'])?$_GET['lang']:'ru';
- * (new \LanguageSwitcher\LanguageFileSwitcher(__DIR__ .'/languages/'))->includeLanguage($lang);
- */
- class LanguageFileSwitcher{
- const DEFAULT_CODE = 'en';
- const DEFAULT_EXT = '.php';
- protected $sourcePath;
- protected $extention ;
- public function __construct($sourcePath, $extention = self::DEFAULT_EXT){
- $this->sourcePath = $sourcePath;
- $this->extention = $extention;
- }
- public function inludeLanguage($code = self::DEFAULT_CODE){
- $filename = $this->getFilenameByCode($code);
- if(in_array($filename, $this->getAvailableLanguagesFiles())){
- include $filename;
- return;
- }else{
- $defaultSource = $this->getFilenameByCode(self::DEFAULT_CODE);
- if(file_exists($defaultSource)){
- include $defaultSource;
- return;
- }else{
- throw new Exception("Not found default language file! ");
- }
- }
- }
- protected function getFilenameByCode($code){
- return $this->sourcePath . $code . $this->extention;
- }
- protected function getAvailableLanguagesFiles(){
- return glob($this->sourcePath . '*' . $this->extention);
- }
- }
- class Exception extends \RuntimeException {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement