php - Zend framework 2 - How to make a language switcher -


I am developing a Zend Framework 2 application and now I want to implement a language switcher where the guest / registered user The language they want, the thing that I can not understand, is done in the Zend Framework 2 (using no URL), after selecting once, guest's favorite language in storage I want to keep it, and the registrar For the average user, I can retrieve favorite cookie / database and it can re-use with storage. But how should I start this and how? Thank you in advance.

Set up your locale in your global .config .php :

  'Locale' = & gt; Array ('default' = & gt; 'en_US', 'available' => array ('de_DE' => gt; 'Deutsch', 'nl_NL' = & gt; 'Dutch', 'en_US' = & gt; ; 'English' 'Fr_FR' = & gt; 'French',),),   

Then you can add a method to your Application \ Module.php The default that sets jand \ translator \ translator :

  class module {public function on bootstrap (MvcEvent $ e) {$ applicaton = $ e- & gt; ; GetApplication (); $ Service manager = $ application- & gt; GetServiceManager (); // Just call a translator, nothing special! $ ServiceManager- & gt; Mill ('translator'); $ This- & gt; Inittranslator ($ E); // etc, more of your bootstrap function. } Secure function initTranslator (MvcEvent $ event) {$ serviceManager = $ event- & gt; GetApplication () - & gt; GetServiceManager (); // z \ session \ container $ session = new container ('language'); $ Translator = $ service manager- & gt; Receive ('translator'); $ Translator - & gt; SatoCell ($ session-> language) - & gt; Set block block ('en_US'); }}   

The default locale is now en_US because there is no available space in the session. To change the locale you need to catch users and validate the available locale provided in your global.config.php . So to change this you may need to add a controller action which captures the user's input and sets a new locale, without the use of any type of operation of the controller!

  Changed public function () {// New container will get that language session if session manager already knows the language session. $ Session = new container ('language'); $ Language = $ this- & gt; GetRequest () - & gt; Received post () - & gt; Language; $ Config = $ this- & gt; Service provider- & gt; Get ('config'); If (isset ($ config ['locale'] ['available'] [$ language]) {$ session- & gt; language = $ language; $ this-> servicing-> get ('translator' ) -> SetLocale ($ session-> language);}}   

Sessions allows users to change their locale and remember it until the session ends, It will not need to change, they come back after a while, hope it will help you and you can save your application for your registered users. A few can write code.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -