Last Updated: February 25, 2016
·
5.025K
· mabasic

Helper for creating language anchors in Laravel

This little helper gets the current URL and then applies the desired language to it.

Let's say you are located on http://domain.com/news/europe and that website is on English and you want to enable your users to click on a link, let's call it "translate to croatian" and then they would get the same page, but on Croatian language http://domain.com/hr/news/europe.

I hope that you understood what I meant to say :)

Put this helper in: app/helpers. Include that folder in composer.json. Run composer dump-autoload. In your view (layout) just create a simple <a href="{{ L18n::currentUrl('en') }}">translate to english</a> or <a href="{{ L18n::currentUrl() }}">translate to default website language</a>.

Filename: L18n.php

<?php

Class L18n{

    /*
    |
    | Gets the URL of the current page and applies the desired language to it.
    | If `lang` is set to "en" then the URL for the route `trips` should look
    | like `http://domain.com/en/trips`. If the `lang` is set to "" then the URL
    | should look like `http://domain.com/trips`.
    |
    */
    static function currentUrl($lang = "")
    {
        // if the desired language is found in the supported_locales variable
        if( in_array($lang, Config::get('locales.supported_locales')) )
        {
            // if the first part of the URL is found in the supported_locales
            if( in_array(Request::segment(1), Config::get('locales.supported_locales')) )
            {
                // remove the first two characters from the `path`; DELETE (en)
                $cleanPath = substr(Request::path(), 2);
                // replace current `path` with the modified `lang` + `cleanPath`; MERGE (de/path)
                $translated = str_replace(Request::path(), $lang.$cleanPath, Request::url());
            }
            // else if the first part of the URL is null; DOES NOT EXIST (there is no language set)
            elseif (Request::segment(1)==null)
            {
                // append `lang` to the end of the  current URL
                $translated = Request::url().'/'.$lang;
            }
            // else if the first part of the URL is something, but not a language then
            else{
                // save current path
                $path = Request::path();
                // append `lang` and `path` to `domain`
                $translated = Config::get('app.url').'/'.$lang.'/'.$path;
            }

            return $translated;
        }
        // if the desired language is not found in the supported_locales variable; (default to app.locale)
        else{
            // if the first part  of the URL is found in the supported_locales (the user is currently viewing the translated page)
            if( in_array(Request::segment(1), Config::get('locales.supported_locales')) )
            {
                // remove the first three characters from `path`; DELETE (en/)
                $cleanPath = substr(Request::path(), 3);
                // replace current `path` with `cleanPath`
                $default = str_replace(Request::path(), $cleanPath, Request::url());
            }
            // the URL has no language set
            else{
                // just return the current URL
                $default = Request::url();
            }

            return $default;
        }
    }

}

Here is the Pro tip that helped me to configure my routes for multi-lingual website: https://coderwall.com/p/czumyq

1 Response
Add your response

To works with /public/ or whatever subdirectory, change

$path = Request::path();
$translated = Config::get('app.url').'/'.$lang.'/'.$path;

With:

$path = Request::path();
$translated = str_replace($path, $lang.'/'.$path, Request::url());

Alek

over 1 year ago ·