![]() |
{locale} placeholder in routes - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10) +--- Thread: {locale} placeholder in routes (/showthread.php?tid=82071) |
{locale} placeholder in routes - webdeveloper - 06-08-2022 Hi, I just want to share with you one of my changes I made to base router. Hopefully, it will save some time, to somebody who will search for the same solution like I did. I was wondering why my project takes URL like Code: www.domain.com/blah/users when I've never defined it? Why this works? What I found out: PHP Code: $routes->group('{locale}', function ($routes) { {locale} will take ANYTHING Why does all this URLs works? Code: www.domain.com/this/users {locale} should represent locale placeholder. According to me, it does not. It represent anything. Yes, it takes default locale value, if placeholder doesn't match any supported language. PHP Code: public $supportedLocales = ['en']; Only this should works. Code: www.domain.com/en/users Changes I have made: Create new file app/Services/Router.php PHP Code: <?php Check added part of code: PHP Code: /** Add this use to app/Config/Services.php PHP Code: use App\Services\Router; Add custom router: PHP Code: /** RE: {locale} placeholder in routes - includebeer - 06-08-2022 You are right, it doesn't make a lot of sense how unsupported locales are handled by the framework. Instead of a custom router, I made a filter : PHP Code: <?php RE: {locale} placeholder in routes - webdeveloper - 06-09-2022 Thank you for comment! Always happy to see any other solutions. Yep, it can be done like this too. I have never think about to create filter for this. Router should handle this "right". One important difference is that your locale have to be placed always on the first place of URL. This wont work for you: PHP Code: $routes->group('translations', function ($routes) { Second one difference is that router is called before filter. A lot of (possibly unimportant) code can be done until page not found exception is thrown. Why did you remove second part of locale? This is used in most cases for variants of language e.g. "en-US". I consider this language fallback behaviour as useful Quote:So, if you are using the locale fr-CA, then a localized message will first be sought in Language/fr/CA, then in Language/fr, and finally in Language/en. RE: {locale} placeholder in routes - includebeer - 06-09-2022 (06-09-2022, 01:56 AM)webdeveloper Wrote: One important difference is that your locale have to be placed always on the first place of URL. You're right, I didn't think it could be a problem as all my routes are configured with the locale as the first segment of the URL. (06-09-2022, 01:56 AM)webdeveloper Wrote: Why did you remove second part of locale? This is used in most cases for variants of language e.g. "en-US". I consider this language fallback behaviour as useful I remove the second part of the locale because it doesn't really make a difference. At least not in my case. My blog is in French and in English. So if the locale detected is en-US, en-UK or any other en-xx it redirects to the English version of the blog. Same thing for fr-FR, fr-CA, etc, it goes to the French version. Anything else goes to the default locale, with is English. RE: {locale} placeholder in routes - InsiteFX - 06-09-2022 It's probaliy best to use the language+region code for translations en-US etc. @includebeer, I have been working on your multi-language tutorial and have a working language drop down with country flags working, I just have to clean-up the flags because they are in language not country. Example: Japan is named jp in the flags css flie not by language code jp RE: {locale} placeholder in routes - includebeer - 06-10-2022 (06-09-2022, 11:57 PM)InsiteFX Wrote: It's probaliy best to use the language+region code for translations en-US etc. I guess it depends on the use case. For me it was the easiest way to redirect to the english or french articles. I should probably modify my filter to keep the real locale (en-US, en-UK...) but the only difference I can think of is how the dates are displayed. Otherwise, everything should be the same in en-US vs. en-UK, or fr-FR vs. fr-CA. (06-09-2022, 11:57 PM)InsiteFX Wrote: @includebeer, I have been working on your multi-language tutorial and have a working language drop down Cool! ![]() RE: {locale} placeholder in routes - kenjis - 06-10-2022 See this PR: https://github.com/codeigniter4/CodeIgniter4/pull/6073 RE: {locale} placeholder in routes - includebeer - 06-10-2022 (06-10-2022, 06:05 PM)kenjis Wrote: See this PR: https://github.com/codeigniter4/CodeIgniter4/pull/6073 I like the idea of a $useSupportedLocalesOnly option. No need to hack the default behaviour with a filter! RE: {locale} placeholder in routes - webdeveloper - 06-11-2022 (06-10-2022, 06:34 PM)includebeer Wrote:(06-10-2022, 06:05 PM)kenjis Wrote: See this PR: https://github.com/codeigniter4/CodeIgniter4/pull/6073 The main question is why you run into this problem, that you need to redirect with right locale? How does the wrong locale appear in your URL? Is it even possible to happen just so? @kenjis This makes the solution which I posted even better! Like that. DONE. Fallback behaviour implemented. Add to: app/Config/Routes.php PHP Code: $routes->useDefinedLocalesOnly(true); Create file: app/Services/Routes.php PHP Code: <?php Add to: app/Services/Router.php (check the code in first post to find the place where to put this) PHP Code: /** Add use to: app/Config/Services.php PHP Code: use App\Services\Router; and function PHP Code: /** |