R
R
Roman2020-09-11 12:54:08
Laravel
Roman, 2020-09-11 12:54:08

Multilingual site on Laravel 7, how to implement?

Implemented the language change itself.

Middleware checks if the URL contains the language directory. If it is, then App::setLocale($locale) is set and the page is loaded.

When changing the language, I edit the URL, at the beginning of the link I substitute the directory corresponding to the selected language.
When changing the language, a cookie is written with the language value.

All Route combined into a group.

I stopped at the moment when the language value in the cookie is not equal to the directory language value (this can happen when the user changed the language, but later returned to the site using the old link corresponding to another language).

I wanted to do a redirect if the cookie and language were different in the url. But the redirect will not save setLocale and an infinite loop is obtained. Maybe in $next($request) it is somehow possible to pass other URL values? But most likely this is my sick fantasy)

How to implement a language change if the language in the cookies and the language in the url do not match?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
barma001, 2020-09-11
@barma001

here is an example for the five, but the general principle is described there and there is an answer to your question
if displaying the language in the url is unprincipled, then it can be simpler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question