Search code examples
phplaraveltranslationmultilingual

How to translate german into english using Laravel (Localization?)


I have a german site and want it to translate it into english with Laravel (Localization?). There will be different links for each language, so it is not necessary to get their locale.

I've read through the docs and many articles but still am not sure how to do this.

What I need:

  • I have only two languages, german(main) and english(second)

  • I need the parameters en and de in the url, but I haven't found a tutorial doing this

  • I think I have to use translation Strings as keys since it makes the most sense with only two languages, therefore I need a json file

So my questions:

  1. I want to translate into english, but there is already an en-folder in the lang-folder, so what am I supposed to do? Just put the en.json-file in there?

  2. Is it necessary to use middleware? Or is there another, idiot-safe way to achieve this for someone with a really small amount of experience?

  3. How can I set and get the language-parameter in the url and translate the site based on it?

  4. Do I only need one en-file to translate even though I have about 3 different routes/sites?


Solution

  • You have already the structure/functionality of the Localization which is in Laravel super easy:

    In folder

    resources/lang
    

    you need to have a per lang folder like

    resources/lang/en -> English
    resources/lang/de -> German/Deutsch
    

    in each lang forlder you create a blade for example the app translation:

    app.blade.php
    

    you will have the following structure:

    resources/lang/en/app.blade.php -> English
    resources/lang/de/app.blade.php -> German/Deutsch
    

    Where you put for example

    <?php
    
    return [
    
        /*
        |--------------------------------------------------------------------------
        | Application Language Lines
        |--------------------------------------------------------------------------
        |
        | The following language lines are used by the app library to build
        | the simple pagination links. You are free to change them to anything
        | you want to customize your views to better match your application.
        |
        */
    
        'greets' => 'Welcome',
    ];
    

    in the German app.blade.php you put:

    <?php
    
        return [
    
            /*
            |--------------------------------------------------------------------------
            | Application Language Lines
            |--------------------------------------------------------------------------
            |
            | The following language lines are used by the app library to build
            | the simple pagination links. You are free to change them to anything
            | you want to customize your views to better match your application.
            |
            */
    
            'greets' => 'Wilkommen',
        ];
    

    Then you can call this in you blade with

    @lang('app.greets')
    

    The default language is set in config/app.php the locale constant where you can create the logic to switch it using for example App::setLocale('de'); see the used locale is just using the App::getLocale();