SEO friendly localised urls |
Hi,
I am trying to improve my multilingual website, building on https://includebeer.com/en/blog/creating...r-4-part-1. I would like to have my urls in the language of the page. For example, for a research page, have /en/research for english content and /fr/recherche for french. My routes are defined as such: Code: $routes->get('/{locale}/research', 'PagesController::show/research', ['as' => 'research']); I thought I could do this inside routes.php: Code: $routes->get('/en/research', 'PagesController::show/research', ['as' => 'enResearch']); And modify my menu to call for the right alias like that Code: <?= route_to($locale."Research") ?> But then I loose the locale, so if I am on the english version of the page and click on the link to view the page in French, I still see the english version (with the French url). What would be the best approach to make SEO friendly urls in this case?
Sounds like it is now updating the html, I do it like this using the Session.
PHP Code: /** I also have created a Bootstrap 5.3.3 NavBar Language Selection DropDown that handles all CodeIgniter 4 Translations. I' am still working on it but so far everything is working. The language is changed in the BaseController. BaseController: PHP Code: /** What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Thanks for sharing your approach.
I can make the view work when I keep the {locale} placeholder inside my routes like that: Code: $routes->get('/{locale}/research', 'PagesController::show/research', ['as' => 'enResearch']); and then calling the correct alias inside my menu with the locale passed from the BaseController to the view Code: <?= route_to($locale."Research") ?> But it does not feel entirely clean? Or is it?
Did some searching and found this on Google, watch the video also takes about a half hour read.
Tell Google about localized versions of your page What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
Hi,
thanks for your suggestion, I already watched those and am following google's requirements for the alternate urls. My question is about the best coding approach to localise my urls and adjust the changing language menu in a way that works. I am looking for feedback on the codeigniter part of things. Is my approach to routes okay? If so, then I can move on to the next 'challenge' ![]()
Not sure if your codes are ok but I' m now questioning this part on Google.
Maybe @kenjis can clarify this for us. Google says: Supported language and region codes The hreflang attribute's value is comprised of one or optionally two values, separated by a dash. For example, en-US. The first code of the hreflang attribute is the language code (in ISO 639-1 format) followed by an optional second code that represents the region code (in ISO 3166-1 Alpha 2 format) of an alternate URL. Warning: You can't specify the country code by itself. The first code stands for the language and Google doesn't automatically derive the language from a country code. @kenjis, Doe's this mean that Google is ignoring the 2 code language code? Should we be using the 5 code? en-GB etc? h What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
@InsiteFX
> Doe's this mean that Google is ignoring the 2 code language code? No. Read it carefully. > Warning: You can't specify the country code You can't specify country code. You can specify language code. You see hreflang="en" in the sample code. Code: <head>
If my approach to routes are not raising any concerns, then I am moving forward y thinking, starting with the best way to think about the language change:
I find the user experience of switching the language that results in a redirect to the home page in a different language not ideal. I currently use a helper to keep the user on the same view - just in a different language. Things are simple as long as only the locale has to change within the URL. But if I want to 'translate' them, then it is getting more complex. Here's the current language switch function I am using: Code: function localizeNav() If I adapt my URLs to be translated, then this does not work anymore. Is there a way to get the alias of the route we are currently using maybe? If I use for example enResearch and frResearch as aliases, and can build the correct 'corresponding' link it would be nice. But I don't think it is possible? Any ideas on how to best approach this? I'd like to avoid having to store in a database the mapping of my routes. |
Welcome Guest, Not a member yet? Register Sign In |