![]() ![]() For others you need toĮxpose each application language to the world. For intranet applications andĪpplications for which search engine indexing isn’t important, it is already enough. So far we’ve found a way to detect language, select it manually and store it. $preferredLanguage = $app->user->language $preferredLanguage = isset($app->request->cookies) ? (string)$app->request->cookies : null Now we can improve LanguageSelector a bit: namespace app\components But it could be, for example, database: $user = Yii::$app->user We’re using cookie to store the language. Yii::$app->response->cookies->add($languageCookie) A good place to do it is SiteController::actionLanguage: $language = Yii::$app->request->post('language') ![]() Language, ) ?>įorm handling should be done in controller. Overall it’s a simple select widget pre-filled with an array of Let’s start with language selector widget. What was selected and uses it for the application further. These problems could be solved by providing visible enough language selector that somehow remembers User will get language he doesn’t know, user may know many languages but prefer, for example, English for informationĪbout travelling. While it sounds like a great idea to always detect language, it’s usually not enough. 'supportedLanguages' => ,Īs was mentioned above, it could be implemented in custom UrlManager, custom UrlRule or controller's / module's beforeAction() instead. 'class' => 'app\components\LanguageSelector', In order to use the component you should specify it in the application config like the following: return [ $preferredLanguage = $app->request->getPreferredLanguage($this->supportedLanguages) Note that language should be set prior to controller action so it’s a good idea to create language selection component:Ĭlass LanguageSelector implements BootstrapInterface $languages = Yii::$app->request->getPreferredLanguage($supportedLanguages) Your application supports: $supportedLanguages = App language switcher code#The following code shows selecting a language using information supplied by user’s browser and a list of languages Detecting language automaticallyĭetecting language automatically could help your application to conquer international markets if done properly. ![]() Good places to consider are custom UrlManager, custom UrlRule, controller's or module's beforeAction() Note that it should be done every request before any output in order for outputted content to be affected. Or via application config such as config/main.php: return [ It can be done either via code like the following: Yii::$app->language = 'ru_RU' Setting application language is pretty simple. In this recipe we’ll describe some typical cases of language selection and provide ideas and code snippets so you’llīe able to pick what’s required and implement it in your project. Yii has built in solution for handling message translations but doesn’t provide anything about selecting a languageīecause implementation depends of requirements. When developing applications or websites for global marked, supporting multiple languages is always a requirement. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |