Guest User

Untitled

a guest
May 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. # app/app_controller.php
  2. uses ( 'L10n' );
  3.  
  4. class AppController extends Controller {
  5. public function beforeFilter() {
  6. /**
  7. * Derive the desired locale by reading the subdomain from
  8. * the HTTP_HOST server variable. Locale subdomains can use
  9. * either the 2 or 3 character ISO code. Information on locale
  10. * ISO codes is at http://www.loc.gov/standards/iso639-2/php/code_list.php.
  11. */
  12. $this->L10n = new L10n();
  13.  
  14. /** Auto-detect the request language settings */
  15. $this->L10n->get();
  16.  
  17. /**
  18. * Set the default "domain" for translations. The domain is the
  19. * same as the po file name in a given locale directory. e.g.
  20. * __d( 'homepage', 'message_id' ) would look for the
  21. * message_id key in homepage.po. Using the __() convenience
  22. * function will always look in default.po.
  23. */
  24. $this->set( 'domain', 'default' );
  25. }
  26.  
  27. # The rest of your AppController code
  28. }
  29.  
  30. $routes->connect('/locale', ['controller' => ' Users', 'action' => 'languageChange']);
  31.  
  32. use CakeHttpSession;
  33. use CakeI18nI18n;
  34.  
  35. //Before filter in users controller
  36. public function beforeFilter(CakeEventEvent $event)
  37. {
  38. parent::beforeFilter($event);
  39. }
  40.  
  41.  
  42. /**
  43. * Change language
  44. */
  45. public function languageChange()
  46. {
  47. if ($this->request->is('post')) {
  48. $session = $this->getRequest()->getSession();
  49. if (!empty($this->request->getData('locale'))) {
  50. $session->write('Config.language', $this->request->getData('locale'));
  51. $this->redirect($this->referer());
  52. } else {
  53. $session->write('Config.language', I18n::getLocale());
  54. $this->redirect($this->referer());
  55. }
  56. }
  57. }
  58.  
  59. <?php
  60. echo $this->Form->create("Localizations", array('url' => '/locale'));
  61. echo $this->Form->radio("locale", [
  62. ['value' => 'en_US', 'text' => 'English'],
  63. ['value' => 'de_DE', 'text' => 'German'],
  64. ['value' => 'fr_FR', 'text' => 'French'],
  65. ]);
  66. echo $this->Form->button('Change Language');
  67. echo $this->Form->end();
  68. ?>
  69. <h2><?php echo __('Hello'); ?></h2>
  70. <h2><?php echo __('How are you?'); ?></h2>
  71. <h2><?php echo __('Wel Come'); ?></h2>
  72. <h2><?php echo __('Good Job'); ?></h2>
  73.  
  74. use CakeI18nI18n;
  75. use CakeHttpSession;
  76. use CakeCoreConfigure;
  77.  
  78.  
  79. public function beforeFilter(Event $event)
  80. {
  81. $session = $this->getRequest()->getSession();
  82.  
  83. if ($session->check('Config.language')) {
  84. I18n::setLocale($session->read('Config.language'));
  85. } else {
  86. $session->write('Config.language', I18n::getLocale());
  87. }
  88. }
  89.  
  90. msgid "Hello"
  91. msgstr "Hello"
  92.  
  93. msgid "How are you?"
  94. msgstr "How are you?"
  95.  
  96. msgid "Wel Come"
  97. msgstr "Wel Come"
  98.  
  99. msgid "Good Job"
  100. msgstr "Good Job"
  101.  
  102. msgid "Hello"
  103. msgstr "Hallo"
  104.  
  105. msgid "How are you?"
  106. msgstr "Hoe gaat het met je?"
  107.  
  108. msgid "Wel Come"
  109. msgstr "Wel kom"
  110.  
  111. msgid "Good Job"
  112. msgstr "Goed gedaan"
Add Comment
Please, Sign In to add comment