Skip to content

Instantly share code, notes, and snippets.

@chrisvogt
Last active December 16, 2015 10:49
Show Gist options
  • Select an option

  • Save chrisvogt/5423043 to your computer and use it in GitHub Desktop.

Select an option

Save chrisvogt/5423043 to your computer and use it in GitHub Desktop.
A stab at user language localization by `User.user_lang`, a passed query param, or a stored session variable. This belongs in your AppController and should be called from the beforeFilter() method.
<?php
/**
* Language Handler.
*
* Sets the app's configured language based on passed parameter or language
* configuration stored in session variable.
*
* @author C1V0
*/
protected function _langHandler() {
if (!$this->Auth->User('user_lang')) { // If user language preference not set...
if (isset($this->request->query['lang'])) { // Set session variable by query param
$this->Session->write('Config.language', $this->request->query['lang']);
}
} else { // Use the user's language preference
$this->Session->write('Config.language', $this->Auth->User('user_lang'));
}
// Set the language!
if ($this->Session->check('Config.language')) { // Localize based on session variable
Configure::write('Config.language', $this->Session->read('Config.language'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment