I am developing a Zend Framework 2 Application and now I want to implement a language switcher from where guest/registered user can choose the language they want, the thing I can't understand is how is it made in Zend Framework 2 using the storage ( not from urls ), I want to keep the preffered language of guest in the storage once he selects one, and for the registered users I can retrieve the preffered one from cookie/database and reuse it with storage. But where and how should I start/implement this? Thank you in advance.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
I am not sure, my approach will work or not. Please try:
We can have 3 params for
translate
method.The $locale parameter is taken from the locale, set in the translator and that's why we usually not set manually in the code. So, you can use like below :
You can have a key value pair - key can be user selected language and value can be any one of the language.
Setup your
Locales
in yourglobal.config.php
:So in your
Application\Module.php
you can add a method which sets the defaultZend\Translator\Translator
:So now the default Locale is en_US as the session has no Locale available. For changing the locale you need to catch the users input and validate the available locales you support, provided in your
global.config.php
. So in order to change it you might need to add a controller action which catches the input of the user and sets the new locale. Example of the controller action without any form usage!The session allows the users to change their locale and remember it until the session ends, so they won't need to change it when they get back after a while. Hope this will help you and can help you to write some code to save it for your registered users on your application.