Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multilingual website translations issue #71

Open
ShapesGraphicStudio opened this issue Dec 18, 2024 · 5 comments
Open

Multilingual website translations issue #71

ShapesGraphicStudio opened this issue Dec 18, 2024 · 5 comments

Comments

@ShapesGraphicStudio
Copy link

ShapesGraphicStudio commented Dec 18, 2024

Hi,
First, nice package, thanks!
I'm having a little issue thought as some texts seems to rely on APP_LOCALE setting in .env file.
I'm having a multilingual website and this should be set to current language.

Some strings are being translated when user changes language like:
_ 'more' => 'More details',
...
_ 'save' => 'Save settings',

But some are not like:
_ 'essentials' => [
'title' => 'Essential cookies',
'description' => 'There are some cookies that we have to include in order for certain web pages to function. For this reason, they do not require your consent.',
],

This is what I get when APP_LOCALE=fr and current language = en:
Capture d’écran 2024-12-18 à 13 38 42

With APP_LOCALE=en and current language = en everything is in english.
With APP_LOCALE=fr and current language = fr everything is in french.

Any idea on how I could fix this please?

@kiwo12345
Copy link

kiwo12345 commented Jan 30, 2025

@ShapesGraphicStudio u can switch it based on App::setlocale()

Route::get('/en', function () {
    App::setlocale('en');
    return view('english');
});
Route::get('/fr', function () {
    App::setlocale('fr');
    return view('french');
});

@tonyjoe-dev
Copy link
Contributor

I also have an issue with multilingual site.

The problem is that the \App::setLocale('xx') is executed after the translation.
I mean:

  1. CookieServiceProvider class instantiate CookieCategory
  2. CookieCategory make translations (in the constructor)
  3. Translated strings stay in memory
  4. After that, bootstrap go on and set \App::setLocale('xx') (in my case, in a middleware).

I temporary solved with a "re-translation" directly in blade file.

Are there any other solutions?


For details, see:

  1. Whitecube\LaravelCookieConsent\CookiesServiceProvider
<?php

namespace Whitecube\LaravelCookieConsent;

abstract class CookiesServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->booted(function () {
            $this->registerCookies();
        });
    }

    // ...
  1. Whitecube\LaravelCookieConsent\CookiesCategory
<?php

namespace Whitecube\LaravelCookieConsent;

class CookiesCategory
{
    // ...

    public function __construct(string $key)
    {
        $this->key = $key;
        $this->setAttribute('title', $this->translate('categories.' . $key . '.title', ucfirst($key)));
        $this->setAttribute('description', $this->translate('categories.' . $key . '.description'));
    }

    // ...

My temporary solution in the file resources/views/vendor/cookie-consent/cookies.blade.php

// ...
<div class="cookies__sections">
    @foreach ($cookies->getCategories() as $category)
    <div class="cookies__section">
        <label for="cookies-policy-check-{{ $category->key() }}" class="cookies__category">
            @if ($category->key() === 'essentials')
                <input type="hidden" name="categories[]" value="{{ $category->key() }}" />
                <input type="checkbox" name="categories[]" value="{{ $category->key() }}" id="cookies-policy-check-{{ $category->key() }}" checked="checked" disabled="disabled" />
            @else
                <input type="checkbox" name="categories[]" value="{{ $category->key() }}" id="cookies-policy-check-{{ $category->key() }}" />
            @endif
            <span class="cookies__box">
                <strong class="cookies__label">
                    @lang("cookieConsent::cookies.categories.{$category->key()}.title")
                </strong>
            </span>

            @php $description = __("cookieConsent::cookies.categories.{$category->key()}.description") @endphp
            @if (filled($description))
            <p class="cookies__info">
                {{ $description }}
            </p>
            @endif
        </label>

        <div class="cookies__expandable" id="cookies-policy-{{ $category->key() }}">
            <ul  #class="cookies__definitions">
                @foreach ($category->getCookies() as $cookie)
                <li class="cookies__cookie">
                    <p class="cookies__name">
                        {{ $cookie->name }}
                    </p>
                    <p class="cookies__duration">
                        {{ \Carbon\CarbonInterval::minutes($cookie->duration)->cascade() }}
                    </p>

                    @php 
                    $langkey = $cookie->name;
                    if ($category->key() === 'essentials') {
                        if (\Str::endsWith($langkey, '_consent')) {
                            $langkey = 'consent';
                        } else if (\Str::endsWith($langkey, '_session')) {
                            $langkey = 'session';
                        } else if (\Str::lower($langkey) === 'xsrf-token') {
                            $langkey = 'csrf';
                        }
                    } else if (\Str::startsWith($langkey, '_ga_')) {
                        $langkey = '_ga_ID';
                    }

                    $description = __("cookieConsent::cookies.defaults.{$langkey}")
                    @endphp
                    @if (filled($description))
                    <p class="cookies__description">
                        {{ $description }}
                    </p>
                    @endif
                </li>
                @endforeach
            </ul>
        </div>
        <a href="#cookies-policy-{{ $category->key() }}" class="cookies__details">
            @lang('cookieConsent::cookies.details.more')
        </a>
    </div>
    @endforeach
</div>
// ...

@toonvandenbos
Copy link
Member

The Cookie Consent Service Provider should execute after the Route Service Provider in order to get translations to work properly.

@tonyjoe-dev
Copy link
Contributor

The Cookie Consent Service Provider should execute after the Route Service Provider in order to get translations to work properly.

In my case, the language is set in a middleware. The middleware is executed always AFTER any provider.

@ShapesGraphicStudio
Copy link
Author

Same for me, the language is set in a middleware.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants