0

Here is my problem i've read already a lot of thing, I am actually following this tutorial

  1. https://5balloons.info/localization-laravel-multi-language-language-switcher/
  2. https://www.youtube.com/watch?v=po4Xt1G3gcc

But I can't get it working, I always get error and I've been stuck on this for the past 4 hours. Everyone who is using this technic seems to be able to make it works but not me.

I've already looked all these posts

  1. https://laravel.com/docs/9.x/blade
  2. Laravel - htmlspecialchars() expects parameter 1 to be string, object given
  3. Laravel Get Config Variable
  4. https://laracasts.com/discuss/channels/laravel/problem-in-retrieving-the-data-in-viewblade-laravel
  5. https://www.educative.io/answers/how-to-access-config-file-values-in-laravel-using-config
  6. Converting an array of arrays from Laravel's collection into an object with an array in json
  7. Laravel Blade loop through config array variable
  8. https://www.w3schools.com/php/php_arrays_multidimensional.asp
  9. https://laravel.com/docs/8.x/configuration
  10. Laravel: how to get direct config value in blade template?

And more links are missing. The thing is whatever I do I either get the error it needs an array not a string (The third line is creating this error): Error#1

<li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        {{ config('languages')[App::getLocale()] }}
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            @foreach (config('languages') as $lang => $language)
                    @if ($lang != App::getLocale())
                        <a class="dropdown-item" href="{{ route('lang.switch', $lang) }}"> {{$language}}</a>
                    @endif
            @endforeach
        </div>

    </li>

Or I get the error telling me that my array is empty (The third line is creating the error): Error2

    <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
        {{ config('languages.*')[App::getLocale()] }}
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            @foreach (config('languages') as $lang => $language)
                    @if ($lang != App::getLocale())
                        <a class="dropdown-item" href="{{ route('lang.switch', $lang) }}"> {{$language}}</a>
                    @endif
            @endforeach
        </div>

    </li>

Or I get the error telling me it needs a string not an array:

Here is how my config is made: config/languages.php

    <?php

return [
        'en' => [
            'display' => 'English',
            'flag-icon' => 'us',
        ],

        'fr' => [
            'display' => 'Français',
            'flag-icon' => 'fr',
        ],

        'es' => [
            'display' => 'Spanish',
            'flag-icon' => 'es',
        ],
];

I also tried this and tried to access it with config(languages.languages):

    <?php

return [
    'languages' => [
        'en' => [
            'display' => 'English',
            'flag-icon' => 'us',
        ],

        'fr' => [
            'display' => 'Français',
            'flag-icon' => 'fr',
        ],

        'es' => [
            'display' => 'Spanish',
            'flag-icon' => 'es',
        ],
    ],
];
Dolotboy
  • 74
  • 7

1 Answers1

0

I simply copied and pasted everything from the tutorial for the 10th time and it worked, IDK why, tho I changed Config::get by config()

Dolotboy
  • 74
  • 7