Welcome Guest, Not a member yet? Register   Sign In
How to translate url
#1

Hi.



I am creating a web page where I have various controllers and functions in CI4. I need to be able to translate the url of each page in several languages.


I had thought of creating a table in the database with all the urls, in which language they are hosted and to which file (controller) to call. I don't know if it is the best solution, but I need to achieve it in a clean way.


This solution works for me but it can be long and tedious to maintain due to the number of urls and languages. Besides I also use a lot of ajax calls, and these don't need to be translated.


Can you help me by giving me examples or suggestions?


Thank you.
Reply
#2

(This post was last modified: 09-12-2020, 08:42 AM by Gary.)

I've never played with language variants, but, if it's only text that's changing per page, then it may be more sensible to keep the same URL and simply replace the bits of text within the page as appropriate (?)... you've seen: https://codeigniter.com/user_guide/outgo...calization ?... though, like it says: "full localization of an application is a complex subject".
Reply
#3

Yes, I have read all the documentation. But I need my urls to be translated as well, why does it make more sense to do so. My urls tell you where you are and what you are doing. What I want to do are friendly urls in each language. At the moment what I do is save each URL in the database with its language. All my urls go through the same controller, which is the one who calls the database and knows which page to display in the correct language. It is a solution, although I do not know if it is the best.
Reply
#4

(This post was last modified: 09-14-2020, 06:42 AM by Gary.)

I've not spent a lot of time contemplating it... and there may well be some sense in doing it how you propose... maybe... under some fringe cases (?).

To me, your comment about it being "long and tedious to maintain due to the number of urls and languages" is what I suspect would make it soon become a kidney stone to manage if the site grew in complexity.

I'd suggest considering not having the url stored in the database... if it's part of the URL in the browser, then it is already known/set on the client side... so using a segment of the URL as an input that the controller can digest and simply call the desired View page once it's done processing would seem simpler (each language will have its personalized copy of each View page containing the language-customized content)... of course, there may be language-related validation and more complexities that need to be weighed up too.  Doing it this way would result in using a URL something like:  https://myshyte.com/reservation/en

I believe PHP's website does something similar... https://www.php.net/manual/en/

Or you could have the language set via a drop-down or a few flag buttons in the top corner that just end up setting a $_SESSION variable when clicked (?).

One could probably also use custom language words for the controllers:  like 'reservation' , 'reservación', 'prenotazione' using Routes (and have the language parameter passed in as a parameter during the (re-)routing, so the (single) controller knew which language the routing came from (?)... so https://myshyte.com/reservación would be routed to the Reservations controller, which (depending on how you did the routing, you could have a language-dependent parameter (say 'ES') passed in with the call (?)).

Just suggestions...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB