mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.38k stars 515 forks source link

Language selector active page translate url #673

Closed tariksisbacak closed 5 years ago

tariksisbacak commented 5 years ago

Hi,

When the Selector is changed, I want it to go into the translation of the current page. I have dynamic menu and url structures. Any idea? What kind of path should I follow?

{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}

When I use it this way, it gets the current url and the structure breaks down.

Home

/en /de /

Contact page

/en/contact /de/contact /contact

The url structure remains constant when you are on the contact page.

Reference : #117

iwasherefirst2 commented 5 years ago

Simply use the snipped from the readme. This already translates the url.

tariksisbacak commented 5 years ago

I used it like in the documentation. But when I switch to any page other than the homepage, the selector does not translate

iwasherefirst2 commented 5 years ago

What do you mean with the selector does not translate?

Could you give an example with expected and actual output? Please also add url, settings etc.

tariksisbacak commented 5 years ago

I mean, the language selector works but does not translate the current page in other languages

Language Selector


@foreach(LaravelLocalization::getURLFromRouteNameTranslated() as $localeCode => $properties)
                                            <li class="option">
                                                <a rel="alternate" hreflang="{{ $localeCode }}"
                                                   href="{{ LaravelLocalization::getLocalizedURL($localeCode, null, [], true) }}>
                                                    {!! flag_img_svg($localeCode)  !!} {{ $properties['native'] }}
                                                </a>
                                            </li>
@endforeach

routes/web.php:


Route::group(['namespace' => 'Frontend', 'prefix' => LaravelLocalization::setLocale(), 'middleware' => ['web', 'localize', 'localeSessionRedirect', 'localizationRedirect', 'localeViewPath']], function () {

    Route::get(LaravelLocalization::transRoute('routes.home'), [
        'as' => 'frontend.home',
        'uses' => 'Home\HomeController@index'
    ]);

    Route::get(LaravelLocalization::transRoute('routes.contact'), [
        'as' => 'frontend.contact',
        'uses' => 'Contact\ContactController@index'
    ]);

resources/lang/tr/routes.php:


<?php

return [

     'home'      => '',
     'company'   => 'kurumsal/{slug}',
     'contact'   => 'iletisim',
];

resources/lang/en/routes.php:

<?php

return [

     'home'      => '',
     'company'   => 'company/{slug}',
     'contact'   => 'contact',
];

resources/lang/de/routes.php:

<?php

return [

    'home'      => '',
     'company'   => 'uber/{slug}',
     'contact'   => 'kontakt',
];

Language Selector Urls Output


example.test/iletisim -> tr
example.test/en/iletisim ->  en
example.test/de/iletisim ->   de

But I want

example.test/iletisim -> tr
example.test/en/contact ->  en
example.test/de/kontakt ->   de
iwasherefirst2 commented 5 years ago

Why are you using getURLFromRouteNameTranslated ?

I have used your settings and the snippet from readme

<ul style="text-align:left">
    @foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
    <li class="option">{{ $localeCode}}
        {{ LaravelLocalization::getLocalizedURL($localeCode) }}
        <a rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getLocalizedURL($localeCode) }}">
            {{ $properties['native'] }}
        </a>
    </li>
    @endforeach
</ul>

Gives me

grafik

tariksisbacak commented 5 years ago

I am so sorrry. i typed incorrectly.

I used it, but the result is the same

<ul style="text-align:left">
    @foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
    <li class="option">{{ $localeCode}}
        {{ LaravelLocalization::getLocalizedURL($localeCode) }}
        <a rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getLocalizedURL($localeCode) }}">
            {{ $properties['native'] }}
        </a>
    </li>
    @endforeach
</ul>

I see the url structures with route: trans:list, everything is ok.

actually everything is fine but url translations don't work

Http/kernel.php

 protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,

        //entrust
        'role' => \Zizaco\Entrust\Middleware\EntrustRole::class,
        'permission' => \Zizaco\Entrust\Middleware\EntrustPermission::class,
        'ability' => \Zizaco\Entrust\Middleware\EntrustAbility::class,

        // localization
        'localize' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRoutes::class,
        'localizationRedirect' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationRedirectFilter::class,
        'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
        'localeViewPath' => \Mcamara\LaravelLocalization\Middleware\LaravelLocalizationViewPath::class

    ];
iwasherefirst2 commented 5 years ago

Did you double check that the path to routes.php is correct? If the app is not finding resources/lang/de/routes.php it will use the default language translation instead.

grafik

tariksisbacak commented 5 years ago

I checked. The url structure is working. The only problem is the selector

Screen Shot 2019-10-24 at 13 02 22

// resources/lang/tr/routes.php


<?php

return [

     'home'      => '',
     'company'   => 'kurumsal/{slug}',
     'contact'   => 'iletisim',

];

// resources/lang/en/routes.php

<?php

return [

     'home'      => '',
     'company'   => 'company/{slug}',
     'contact'   => 'contact',

];

// resources/lang/de/routes.php


<?php

return [

     'home'      => '',
     'company'   => 'uber/{slug}',
     'contact'   => 'kontakt',

];
iwasherefirst2 commented 5 years ago

You still got the issue? Maybe you could create a new app and try to create translated routes from scratch and see if the error happens again. I cannot reproduces it. Maybe also post your config file.

Also, which version of the package and which version from Laravel are you using?

tariksisbacak commented 5 years ago

The problem still does. I'm thinking of testing it on a new project. I appreciate your advice and help. Thanks.

composer.json

"laravel/framework": "^5.8.0",
"mcamara/laravel-localization": "^1.4",

config

<?php

return [

    'supportedLocales' => [

        'de'          => ['name' => 'German',                 'script' => 'Latn', 'native' => 'Deutsch', 'regional' => 'de_DE'],
        'en'          => ['name' => 'English',                'script' => 'Latn', 'native' => 'English', 'regional' => 'en_GB'],
        'tr'          => ['name' => 'Turkish',                'script' => 'Latn', 'native' => 'Türkçe', 'regional' => 'tr_TR'],
    ],

    'useAcceptLanguageHeader' => false,

    'hideDefaultLocaleInURL' => true,

    'localesOrder' => [],

    'localesMapping' => [],

    'utf8suffix' => env('LARAVELLOCALIZATION_UTF8SUFFIX', '.UTF-8'),

    'urlsIgnored' => ['/skipped'],

];
iwasherefirst2 commented 5 years ago

Which Laravel-Localization Version do you have installed? Please show composer.lock

tariksisbacak commented 5 years ago
{
            "name": "mcamara/laravel-localization",
            "version": "1.4.6",
            "source": {
                "type": "git",
                "url": "https://github.com/mcamara/laravel-localization.git",
                "reference": "72a22855d4617ad667eec2565540da2e7158c2c5"
            },
            "dist": {
                "type": "zip",
                "url": "https://api.github.com/repos/mcamara/laravel-localization/zipball/72a22855d4617ad667eec2565540da2e7158c2c5",
                "reference": "72a22855d4617ad667eec2565540da2e7158c2c5",
                "shasum": ""
            },
            "require": {
                "laravel/framework": "~5.2.0||~5.3.0||~5.4.0||~5.5.0||~5.6.0||~5.7.0||~5.8.0||^6.0",
                "php": ">=7.1.0"
            },
            "require-dev": {
                "orchestra/testbench-browser-kit": "~3.4|~3.8|~4.0",
                "phpunit/phpunit": "6.0.*|8.0.*"
            },
            "suggest": {
                "ext-intl": "*"
            },
            "type": "library",
            "extra": {
                "laravel": {
                    "providers": [
                        "Mcamara\\LaravelLocalization\\LaravelLocalizationServiceProvider"
                    ],
                    "aliases": {
                        "LaravelLocalization": "Mcamara\\LaravelLocalization\\Facades\\LaravelLocalization"
                    }
                }
            },
            "autoload": {
                "classmap": [],
                "psr-0": {
                    "Mcamara\\LaravelLocalization": "src/"
                }
            },
            "notification-url": "https://packagist.org/downloads/",
            "license": [
                "MIT"
            ],
            "authors": [
                {
                    "name": "Marc Cámara",
                    "email": "mcamara88@gmail.com",
                    "role": "Developer"
                }
            ],
            "description": "Easy localization for Laravel",
            "homepage": "https://github.com/mcamara/laravel-localization",
            "keywords": [
                "laravel",
                "localization",
                "php"
            ],
            "time": "2019-10-25T08:31:04+00:00"
        },
iwasherefirst2 commented 5 years ago

Closing this as its not reproducible. If you can reproduce the issue from a plain laravel installation, please create a new issue and explain every step that you did to create the problem