Translated Routes and the Language Switcher

  • Hey there,

    Wondering if anyone out there has been able to successfully setup an Asgard site, wherein they use translated routing through language files (routes.php file with translated routes) within a custom module? For example, a Careers module may have the following routes leading to the same page:


    I'm looking for a working language switcher. I can have it swap the prefix en/fr just fine, but I can't seem to retrieve the other translated portion of the slug. getLocalizedURL should be all that is needed, by my understanding. It works great for the 'Pages' module, but the 'Pages' module also does not use the routes.php files.

    What is in place:

    1. Middleware and Kernel adjustments are perfectly fine (adding LaravelLocalizationRoutes, and grouping routes with this)
    2. Routing and reading of language files (routes.php) working great when you enter the URL directly into the browser for each enabled language. It just can't seem to figure out alternative locale URL equivalents. So I have no href to populate in my switcher

    Any help would be appreciated.


  • Hey Letarion!

    I faced the same problem and I did a quick solution:

    1. I set up a new translator element for module: like url-slug and set default value for fallback locale :

           'settings' => [
             'url-slug' => 'careers',
    2. set translation for your desired languages at the backend

    3. you can use it on your frontend routing in our module router file:

    $router->get(trans('careers::settings.url-slug') ,[
       'uses' => 'PublicController1',
        'as' => 'careers', //or trans('careers::settings.url-slug' if you want translated route() alias 
       // 'middleware' => config(''),//if you want middleware

    ^This do the magic you want, I think....

    I hope it helps, cheers:


  • @hgy Thanks for that! We actually ended up needing to go a completely different route for this, abandoned the routes files.. it actually worked out very nicely, your use of settings is one of the pieces of our solution too for the grouping and control of the base path. Good old translatable settings hahaha. We also built a Trait, which we add to any controllers we want to get customized slugs from, it exposes a function we call from the language switcher to pull alternative locales.

    I also wanted to share this language switcher file, which works pretty well with custom slugs on a default build using the Blog and Pages modules. This is likely not the best way to do it, but it's a good starting point. Hope this helps someone else!

        // Initialize optional variables
        $post = empty($post) ? null: $post;
        $page = empty($page) ? null: $page;
        $links = array();
        foreach (LaravelLocalization::getSupportedLocales() as $locale => $language) {
            if ($post) {
                $url = LaravelLocalization::getLocalizedURL($locale, $post->translate($locale)->slug);
            elseif ($page) {
                $url = LaravelLocalization::getLocalizedURL($locale, $page->translate($locale)->slug);
            else {
                $url = LaravelLocalization::getLocalizedURL($locale);
            $links[$locale] = array(
                'url' => $url,
                'language' => $language
    @foreach ($links as $link)
        <a href="{{ $link['url'] }}">{{ $link['language']['name'] }}</a> |

Log in to reply

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.