Localization in Routes |
While trying out CI4 one of the first new features i wanted to check out was the localization and the handling of it through the routing.
We often develop multilingual websites and this is an important feature for us that we need to enable/disable based on the configuration. In CI3 we accomplished this using our own version of MY_Lang, so we're very excited to see it natively supported in ci4. However we notice strange behaviour when configuring 2 locales and 1 default locale. When accessing the root of the site (without specifying a locale in the url) CI4 gives us its 404 page. While I was expecting that a request to www.mywebsite.com would result in a redirect to www.mywebsite.com/defaultLocale Am I missing something ? Is this indeed the intended behaviour ? My setup is this: App.php: PHP Code: public $defaultLocale = 'locale_1'; Routes.php: PHP Code: $routes->add('{locale}/', 'Home::index');
Why u wan't push locale into url's?
Better solution is store locale in session/cookie and allow user change-language select. In own project I make routes based on locale but like this: PHP Code: $routes->add(lang('Url.user_login'), 'Home::index'); Then in my Languages/pl/Url.php (polish) have: PHP Code: return [ or other example Languages/en/Url.php (english) have: PHP Code: return [ (11-06-2018, 02:01 AM)Przem4S Wrote: Why u wan't push locale into url's? Because from search engine point of view, if you have URL www.site.com/anycontent that always defaults to lets say English, it would never index it for any other language, but www.site.com/en/samepage and www.site.com/fr/samepage are two different pages indexed, with proper language keywords etc. More here: https://support.google.com/webmasters/an...92?hl=en#1 I get your point that URL login in different language might be better to also have the url part translated, but it's not always viable to add that extra logic and it's easier to go - here's a locale but we still use auto-routing to specific controller.
Hi, thanks for your comments !
I think you both have valuable arguments, storing the language in a cookie is indeed a valid solution to this problem, however it is not our preferred approach. To answer my own question, I found that I could implement the desired functionality through the creation of a Filter (see code below) application\Filters\Localization.php : PHP Code: <?php namespace App\Filters; application\Config\Filters.php: PHP Code: <?php namespace Config; |
Welcome Guest, Not a member yet? Register Sign In |