Multilingual URLs in Laravel 4

I am trying to implement multilingual urls. So I want to have urls like:
/de/ueber-uns/kontakt and /en/about-us/contact
So far so good, I use App::before() in filters.php to check the locale. I think I need a route in routes.php for every controller action in every language.

So, I thought about dynamically creating the routes.php file. All I need for this is to know how I can access all available controllers or get all registered routes in code (for example, artisan routes, but not with the CLI).

So the questions are:

  • is a generic approach for multilingual urls right?
  • Is it possible to access all the controllers in order to somehow extract the methods?
  • How can I get the RouteCollection that is used in \ Illuminate \ Routing \ Router.php?

Thank you in advance!

+4
source share
1 answer

I ended up doing the following:

1) Routes in route.php are dynamically created using the special artisan command. It analyzes all controllers and creates routes for each action in all supported languages. The language string is processed by routes such as Route :: get ('{lang} / customer / login', 'CustomerController @getLogin') β†’ where ('lang', '[az] {2}'). This way, users can simply change the language string and the site will load in the correct language (if supported). Routes for different languages ​​lead to the same controller action. For these languages, besides English, I need translations (routes.php in / app / lang).

2) before the filter for those controllers whose actions are translated, set in the constructor. It basically checks the correctness of the language string and replaces it if not. The selected language will be installed in the session.

I hope someone can use it :)

+4
source

Source: https://habr.com/ru/post/1483350/


All Articles