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

Navbar issue with the route bindings #19

Closed
ndberg opened this issue Dec 12, 2015 · 9 comments
Closed

Navbar issue with the route bindings #19

ndberg opened this issue Dec 12, 2015 · 9 comments
Assignees
Labels

Comments

@ndberg
Copy link

ndberg commented Dec 12, 2015

I could try out your package as I said.

I like some stuff that you cleaned a bit up from mcamara/laravel-localization like the transGet, transPost and also the localizedGroup.

But my main Problem still exists.. but a bit different. If I use model binding with named parameter, the localsNavbar returns the Hole Model Object Data in the URL:

<ul class="navbar-locales">
    <li class="active">
        <a href="http://torebuild1.local/de/firma/abdeckung/{"id":2,"name":"<j\u00f6","strasse":"575 Wiegand View Suite 800","plz":"999.99","ort":"Howeville","telefon":"053.534.4705x49474","email":"[email protected]","created_at":"2015-12-05 14:24:19","updated_at":"2015-12-10 12:58:49","bereiche":[{"id":1,"name":"umzug","aktiv":1,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00","pivot":{"firma_id":2,"bereich_id":1,"created_at":"2015-12-08 10:22:17","updated_at":"2015-12-08 10:22:17"}}]}" rel="alternate" hreflang="de"> 
            Deutsch 
        </a>
    </li> 
    <li class="">
        <a href="http://torebuild1.local/en/company/coverageEN/{"id":2,"name":"<j\u00f6","strasse":"575 Wiegand View Suite 800","plz":"999.99","ort":"Howeville","telefon":"053.534.4705x49474","email":"[email protected]","created_at":"2015-12-05 14:24:19","updated_at":"2015-12-10 12:58:49","bereiche":[{"id":1,"name":"umzug","aktiv":1,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00","pivot":{"firma_id":2,"bereich_id":1,"created_at":"2015-12-08 10:22:17","updated_at":"2015-12-08 10:22:17"}}]}" rel="alternate" hreflang="en"> 
            English 
        </a> 
    </li> 
    <li class="">
        <a href="http://torebuild1.local/fr/entreprise/abdeckungFR/{"id":2,"name":"<j\u00f6","strasse":"575 Wiegand View Suite 800","plz":"999.99","ort":"Howeville","telefon":"053.534.4705x49474","email":"[email protected]","created_at":"2015-12-05 14:24:19","updated_at":"2015-12-10 12:58:49","bereiche":[{"id":1,"name":"umzug","aktiv":1,"created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00","pivot":{"firma_id":2,"bereich_id":1,"created_at":"2015-12-08 10:22:17","updated_at":"2015-12-08 10:22:17"}}]}" rel="alternate" hreflang="fr"> 
            Français 
        </a> 
    </li> 
</ul> 

EDIT :
But the translation of the Route is right!!
So if you can fix that, I will never go back to the other package ;-)

@ndberg ndberg changed the title navbar navbar route-translation Bug Dec 12, 2015
@ndberg
Copy link
Author

ndberg commented Dec 12, 2015

If I do it like this:

@foreach(Localization::getSupportedLocales()->toArray() as $locale => $properties)
    <li>
        <a href="{{ Localization::getLocalizedURL($locale, null, ['firma' => $firma->id]) }}">
            {{ $locale }}
        </a>
    </li>
@endforeach

It works.. but it's very unpractically for a language switcher to deliver a variable name.

@arcanedev-maroc
Copy link
Member

Hi @JimLfe, thanks for your feedback.

Of course i'm going to fix that. 😄

I'm going to check and test the Localization::localesNavbar() and Localization::getLocalizedURL() behaviors with the route model bindings.

Stay tuned.

Related : mcamara/laravel-localization#268

@arcanedev-maroc
Copy link
Member

After reading the code (From github repo, i'm far away from my workstation for the time being, i need to test this in real laravel project).

So, i found this:

The RouteTranslator class use attributes stored in route collection by using $this->request()->route()->parameters(), so i need to check if the attributes is a simple array (attribute-key => attribute-value) or an object (Eloquent object for example).

Source : https://github.com/ARCANEDEV/Localization/blob/master/src/Localization.php#L322-L332

Or, i can change the Url utility class behavior because its role is to substitute the request attributes into the route wildcards:

Source :

The problem is, how can we grab the correct value from an object to substitute the wildcard when we're using the route binding ?

For example:

// resources/lang/en/routes.php
'users-show' => 'users/{user}',
// app/Providers/RouteServiceProvider.php
$router->model('user', \App\User::class); 

// Note: It can be a model (Eloquent) or a class by using $router->bind()
// app/Http/routes.php
Route::transGet('routes.users-show', ['uses' => 'UsersController@show']);

The wildcard in users/{user} is not always the id of a user, it can be a username or a hashed id ...

My suggestions :

  • Using an interface to grab the correct wildcard value from the object (Not only for Eloquent objects).
  • Using a wildcard name convention (instead of using {user}, you need to specify a public property for the object like this {user_username} =>$user->username).

I'm open if there is any suggestions or ideas to improve this.

PS : 🇫🇷 French speaker here and i'm not sorry 😜

@arcanedev-maroc
Copy link
Member

I've a good news for you @JimLfe,

I fixed the issue by adding a new contract (RouteBindable interface) to tell the Url Utility class to use the correct value for the route wildcard.

Now you need to make a few changes:

1st : Update the arcanedev/localization package to the latest version >= 0.6.5.

2nd : Implement the new interface to your model (Or any bindable class in routes), in your case:

<?php namespace App;

use Arcanedev\Localization\Contracts\RouteBindable;

class Firma extends \Eloquent implements RouteBindable {
    //...

    /**
     * Get the wildcard value from the class.
     *
     * @return int|string
     */
    public function getWildcardValue()
    {
        // You can return an id or whatever you want.
        // 'slug', 'ref', 'hashed id' or a translated 'slug' for example.

        return $this->id; 
    }

    //...
}

That's it, you're ready to go.

Let me know if this fix your issue. 👍

@arcanedev-maroc arcanedev-maroc changed the title navbar route-translation Bug Navbar issue with the route bindings Dec 13, 2015
@ndberg
Copy link
Author

ndberg commented Dec 14, 2015

Thanks, this works great. I like the way you fixed it with the interface.

@arcanedev-maroc
Copy link
Member

You're welcome 👍

@ionutVizitiu
Copy link

ionutVizitiu commented Sep 27, 2016

Hi @arcanedev-maroc ,

Would you help me please in a similar situation?
I'm using your package for the first time and I didn't manage to connect the dots after reading your comments.

routes.php :

Route::localizedGroup(function () {
    Route::transGet('routes.services', [
        'as'   => 'services', 
        'uses' => 'ServicesController@showList',
    ]);

    Route::transGet('routes.service', [
        'as'   => 'services.show', 
        'uses' => 'ServicesController@showService',
    ]);
});

lang/en/routes.php :

<?php

return [
    'services' => 'services',
    'service'  => 'services/{slug}',
    //...
];

lang/fr/routes.php :

<?php

return [
    'services' => 'services',
    'service'  => 'services/{slug}',
    //...
];

What I want to know is how to get the slug translation.

  • Expected:
    From /fr/services/cours-privés-ou-en-groupe to /en/services/private-or-group-lessons
  • Actual:
    From /fr/services/cours-privés-ou-en-groupe, i get /en/services/cours-privés-ou-en-groupe

Thank you very much!

@arcanedev-maroc
Copy link
Member

Hi @ionutVizitiu,

There is another way to translate your wildcards, check the events section.

Check this issue: #27 (comment)

BTW, your issue is unrelated to this one 😄.

Bonne chance 👍

@ionutVizitiu
Copy link

Thank you very much!

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

No branches or pull requests

3 participants