Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I maintain Language between 3 websites?

I have three websites setup. Lets call them User Portal, Admin Portal, Login Portal.

Both Admin and User Portal will use Login Portal to authenticate, however the two will serve different content. My problem is this. If I am on User Portal and I change language from English to Spanish, then when I visit either Admin Portal or Login Portal it should show me everything in spanish. Then if I switch my language to french while on Login Portal, then both Admin and User Portals should show french.

Basically no matter what website I am on and I switch language, I would like the other two websites to know about that change and act accordingly. Now I am using .NET Core and I'm using the native way to do localization. Meaning I have my resource files setup and I use a cookie to store the current language.

I know I can't edit cross domain cookies so I'm a little bit lost as to how can I achieve this. The solutions I thought of was that when you change a language on one website, you do a form post to the other two to keep them updated and that just feels rather messy. It would also get a lot worse if I were to add a 4th portal as well.

Second solution I thought of is to keep the value in the database and then write middle-ware that intercepts every request and checks the database and sets the language. This also feels very wrong as I'm adding more traffic to my database on every request ever sent.

Are there better ways I can do this?

like image 468
Bagzli Avatar asked Jan 26 '26 22:01

Bagzli


1 Answers

If all three portals are subdomains under the same base domain, then use cookies. The cookie values are accessible by all subdomains if it's created for the base domain. For example, read this: Share cookie between subdomain and domain

If the portals are on different domains altogether, then you need to use either a URL or a database approach. In my opinion, the approach differs based on what you can do with your use cases or technology.

My first preference is URL - make the language code part of the URL. e.g., many websites do this: www.domain.com/en-us/page.html. This URL has language and country codes in it. During the page load, these codes are honored and the page renders in the chosen locale. When you change language, redirect the user to the URL with the right locale in it. When going between the portals, keep the locale in the URL, assuming that all portals have a similar URL structure.

My less preferred approach is a database. This approach also comes with caveats that you need to watch out. Namely, avoid reading the database that holds the language selection from the foreign portal. i.e., if the language selection is managed by the user portal, the admin portal will need to ask user portal about this value instead of reading the database directly. I recommend thinking about this intricacy from the architecture standpoint (microservices, bounded contexts, you name it). So the database approach is more involved and thereby, less preferred due to such complications. I think I don't need to elaborate more than that for the database approach as you get the point.

like image 85
Tengiz Avatar answered Jan 29 '26 12:01

Tengiz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!