Created
April 18, 2017 18:15
-
-
Save neo22s/d81857a29abf982d6e5e7f372b3adb66 to your computer and use it in GitHub Desktop.
language selector koseven
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Front end controller for OC app | |
* | |
* @package OC | |
* @category Controller | |
* @author Chema <chema@open-classifieds.com> | |
* @copyright (c) 2009-2013 Open Classifieds Team | |
* @license GPL v3 | |
*/ | |
class Controller extends Kohana_Controller | |
{ | |
/** | |
* user if its loged in | |
* @var Model_User | |
*/ | |
public $user = NULL; | |
function __construct(Request $request, Response $response) | |
{ | |
$locale = NULL; | |
//setting the user | |
$this->user = Auth::instance()->get_user(); | |
//not any locale specified lets use user locale | |
if( $this->user!=FALSE AND (Request::current()->param('language') === '' OR Request::current()->param('language') === NULL) AND Core::get('language') === NULL ) | |
{ | |
$locale = $this->user->language; | |
} | |
else | |
{ | |
//we allow to choose lang from the url | |
if( ($lang = Request::current()->param('language')) !==NULL AND ($lang = Request::current()->param('language')) !=='') | |
{ | |
$locale = $lang; | |
} | |
elseif( ($lang = Core::get('language')) !==NULL) | |
{ | |
$locale = $lang; | |
} | |
elseif (Cookie::get('user_language')!==NULL) | |
{ | |
$locale = Cookie::get('user_language'); | |
} | |
} | |
//lets be sure the correct locale is used | |
$locale_exists = FALSE; | |
foreach (i18n::get_languages() as $language) | |
{ | |
//we check full locale name or only first | |
if ($locale == $language OR $locale == substr($language, 0,2)) | |
{ | |
//so we get the entire locale | |
$locale = $language; | |
$locale_exists = TRUE; | |
break; | |
} | |
} | |
//couldnt find the locale use a default | |
if ($locale_exists === FALSE) | |
$locale = i18n::$locale_default; | |
//in case user is logged in and he chose a different locale we update it | |
if($this->user != FALSE AND $locale !== NULL AND $this->user->language!=$locale) | |
{ | |
$this->user->language = $locale; | |
$this->user->save(); | |
} | |
//saves cookie for later | |
Cookie::set('user_language',$locale, Core::config('auth.lifetime')); | |
//initialize i18n | |
I18n::initialize($locale,Core::config('i18n.charset')); | |
parent::__construct($request,$response); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Home extends Controller { | |
public function action_index() | |
{ | |
//trying to access the home page directly /home.html | |
if ($this->request->param('seotitle')=='home') | |
{ | |
$this->redirect(Route::url('default')); | |
} | |
//if param lang == en redirect only with seoname | |
if ($this->request->param('language') == 'en') | |
{ | |
//we dont want duplicated content thats why if they write /en in the browser we redirect them to the home page | |
$this->redirect(Route::url('default').'?language=en'); | |
} | |
//if set by cookie ignore the keyboard | |
if (Cookie::get('user_language')!==NULL) | |
$locale = Cookie::get('user_language'); | |
//not keyboard then lets use the keyboard language nly first visit | |
else | |
$locale = key(Request::accept_lang()); | |
$key_lang = substr($locale,0,2); | |
//redirect him to proper language | |
if ($this->request->param('language')===NULL AND $key_lang != 'en' AND core::get('language')=== NULL) | |
{ | |
foreach (i18n::$languages as $lang=>$name) | |
{ | |
if (substr($lang,0,2) == $key_lang) | |
$this->redirect(Route::url('home',array('language'=>$key_lang))); | |
} | |
} | |
//get content of the home page not by seoname but by locale and controller | |
$page = new Model_Content(); | |
$page = $page | |
->where('type','=', 'page') | |
->where('locale','=', i18n::$locale) | |
->where('controller','=', 'home') | |
->where('action','=', 'index') | |
->where('status','=', 1) | |
->limit(1)->cached()->find(); | |
if ($page->loaded()) | |
{ | |
$this->template->title = $page->title; | |
$this->template->meta_description = $page->description; | |
$this->template->bind('content', $content); | |
$this->template->content = View::factory('pages/home',array('page'=>$page)); | |
} | |
//not found in DB | |
else | |
{ | |
//throw 404 | |
throw HTTP_Exception::factory(404,__('Page not found')); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* I18n class for php-gettext | |
* | |
* @package I18n | |
* @category Translations | |
* @author Chema <chema@open-classifieds.com> | |
* @copyright (c) 2009-2014 Open Classifieds Team | |
* @license GPL v3 | |
*/ | |
class I18n extends Kohana_I18n { | |
public static $locale; | |
public static $locale_default = 'en_US'; | |
public static $charset; | |
public static $domain; | |
/** | |
* forces to use the dropin | |
*/ | |
public static $dropin = FALSE; | |
/** | |
* array of allowed languages, used in dropdowns and routes.php | |
* @var array | |
*/ | |
public static $languages = array( | |
'en_US' => 'English', | |
'es_ES' => 'Español', | |
//'fr_FR' => 'Français', | |
//'ar' => 'العربية', | |
//'ru_RU' => 'Pусский', | |
); | |
/** | |
* | |
* Initializes the php-gettext | |
* Remember to load first php-gettext | |
* @param string $locale | |
* @param string $charset | |
* @param string $domain | |
*/ | |
public static function initialize($locale = NULL, $charset = 'utf-8', $domain = 'messages') | |
{ | |
if ($locale===NULL) | |
$locale = self::$locale_default; | |
/** | |
* setting the statics so later we can access them from anywhere | |
*/ | |
self::$lang = $locale;//used in i18n kohana | |
self::$locale = $locale; | |
self::$charset = $charset; | |
self::$domain = $domain; | |
//time zone set in the config | |
date_default_timezone_set(Kohana::$config->load('i18n')->timezone); | |
//Kohana core charset, used in the HTML templates as well | |
Kohana::$charset = self::$charset; | |
setlocale(LC_ALL, self::$locale.'.'.self::$charset); | |
bindtextdomain(self::$domain,APPPATH.'languages'); | |
bind_textdomain_codeset(self::$domain, self::$charset); | |
textdomain(self::$domain); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Controller_Page extends Controller { | |
/** | |
* | |
* Display single page | |
* @throws HTTP_Exception_404 | |
*/ | |
public function action_view() | |
{ | |
$seotitle = $this->request->param('seotitle',NULL); | |
$param_language = $this->request->param('language'); | |
if ($seotitle!==NULL) | |
{ | |
//if param lang == en redirect only with seoname | |
if ($param_language == 'en') | |
$this->redirect(Route::url('page',array('seotitle'=>$seotitle))); | |
elseif($param_language==NULL)//not any set then use english! | |
$param_language = 'en'; | |
//get the page content | |
$page = new Model_Content(); | |
$page = $page->where('seotitle','=', $seotitle) | |
->where('type','=', 'page') | |
->where('status','=', 1) | |
->where(DB::expr('LEFT(locale, 2)'),'=',$param_language) | |
->limit(1)->cached()->find(); | |
//so page loaded and language by uri the same | |
if ($page->loaded())// AND substr($page->locale,0,2) == $param_language ) | |
{ | |
Breadcrumbs::add(Breadcrumb::factory()->set_title(__('Home'))->set_url(Route::url('default'))); | |
Breadcrumbs::add(Breadcrumb::factory()->set_title($page->title)); | |
$this->template->title = $page->title; | |
$this->template->meta_description = $page->description; | |
//execute external controller if has any | |
if ($page->controller!==NULL AND $page->action!==NULL AND method_exists('Controller_'.$page->controller, 'action_'.$page->action) ) | |
{ | |
$action = 'action_'.$page->action; | |
$controller = 'Controller_'.$page->controller; | |
$controller = new $controller($this->request, $this->response); | |
$controller->$action($page,$this); | |
//set current controller & action same as external | |
$this->request->controller($page->controller); | |
$this->request->action($page->action); | |
} | |
else | |
{ | |
$this->template->bind('content', $content); | |
$this->template->content = View::factory('page',array('page'=>$page)); | |
} | |
} | |
//not found in DB | |
else | |
{ | |
//throw 404 | |
throw HTTP_Exception::factory(404,__('Page not found')); | |
} | |
} | |
else//this should never happen | |
{ | |
//throw 404 | |
throw HTTP_Exception::factory(404,__('Page not found')); | |
} | |
} | |
/** | |
* Landing page | |
*/ | |
public function action_landing(Model_Content $page = NULL, $controller = NULL) | |
{ | |
$controller->template->bind('content', $content); | |
$controller->template->content = View::factory('landing',array('page'=>$page)); | |
} | |
/** | |
* Landing page with no background | |
*/ | |
public function action_landing_no_bg(Model_Content $page = NULL, $controller = NULL) | |
{ | |
$controller->template->bind('content', $content); | |
$controller->template->content = View::factory('landing_no_bg',array('page'=>$page)); | |
} | |
} // End Page controller |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
/** | |
* home other languages | |
*/ | |
Route::set('home', '<language>', array('language' => $allowed_languages)) | |
->defaults(array( | |
'controller' => 'home', | |
'action' => 'index', | |
'language' => 'en', | |
)); | |
/** | |
* blog | |
*/ | |
Route::set('blog','(<language>/)blog(/<seotitle>.html)', array('language' => $allowed_languages)) | |
->defaults(array( | |
'controller' => 'blog', | |
'action' => 'index', | |
'language' => 'en', | |
)); | |
/** | |
* FAQ | |
*/ | |
Route::set('faq', '(<language>/)faq(/<seotitle>.html)', array('language' => $allowed_languages)) | |
->defaults(array( | |
'controller' => 'faq', | |
'action' => 'index', | |
'language' => 'en', | |
)); | |
/** | |
* page view public | |
*/ | |
Route::set('page','(<language>/)<seotitle>.html') | |
->defaults(array( | |
'controller' => 'page', | |
'action' => 'view', | |
'seotitle' => '', | |
)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment