Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # app/app_controller.php
- uses ( 'L10n' );
- class AppController extends Controller {
- public function beforeFilter() {
- /**
- * Derive the desired locale by reading the subdomain from
- * the HTTP_HOST server variable. Locale subdomains can use
- * either the 2 or 3 character ISO code. Information on locale
- * ISO codes is at http://www.loc.gov/standards/iso639-2/php/code_list.php.
- */
- $this->L10n = new L10n();
- /** Auto-detect the request language settings */
- $this->L10n->get();
- /**
- * Set the default "domain" for translations. The domain is the
- * same as the po file name in a given locale directory. e.g.
- * __d( 'homepage', 'message_id' ) would look for the
- * message_id key in homepage.po. Using the __() convenience
- * function will always look in default.po.
- */
- $this->set( 'domain', 'default' );
- }
- # The rest of your AppController code
- }
- $routes->connect('/locale', ['controller' => ' Users', 'action' => 'languageChange']);
- use CakeHttpSession;
- use CakeI18nI18n;
- //Before filter in users controller
- public function beforeFilter(CakeEventEvent $event)
- {
- parent::beforeFilter($event);
- }
- /**
- * Change language
- */
- public function languageChange()
- {
- if ($this->request->is('post')) {
- $session = $this->getRequest()->getSession();
- if (!empty($this->request->getData('locale'))) {
- $session->write('Config.language', $this->request->getData('locale'));
- $this->redirect($this->referer());
- } else {
- $session->write('Config.language', I18n::getLocale());
- $this->redirect($this->referer());
- }
- }
- }
- <?php
- echo $this->Form->create("Localizations", array('url' => '/locale'));
- echo $this->Form->radio("locale", [
- ['value' => 'en_US', 'text' => 'English'],
- ['value' => 'de_DE', 'text' => 'German'],
- ['value' => 'fr_FR', 'text' => 'French'],
- ]);
- echo $this->Form->button('Change Language');
- echo $this->Form->end();
- ?>
- <h2><?php echo __('Hello'); ?></h2>
- <h2><?php echo __('How are you?'); ?></h2>
- <h2><?php echo __('Wel Come'); ?></h2>
- <h2><?php echo __('Good Job'); ?></h2>
- use CakeI18nI18n;
- use CakeHttpSession;
- use CakeCoreConfigure;
- public function beforeFilter(Event $event)
- {
- $session = $this->getRequest()->getSession();
- if ($session->check('Config.language')) {
- I18n::setLocale($session->read('Config.language'));
- } else {
- $session->write('Config.language', I18n::getLocale());
- }
- }
- msgid "Hello"
- msgstr "Hello"
- msgid "How are you?"
- msgstr "How are you?"
- msgid "Wel Come"
- msgstr "Wel Come"
- msgid "Good Job"
- msgstr "Good Job"
- msgid "Hello"
- msgstr "Hallo"
- msgid "How are you?"
- msgstr "Hoe gaat het met je?"
- msgid "Wel Come"
- msgstr "Wel kom"
- msgid "Good Job"
- msgstr "Goed gedaan"
Add Comment
Please, Sign In to add comment