mcamara / laravel-localization

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

laravel-localization and translated slug stored in database #512

Closed Jimmylet closed 5 years ago

Jimmylet commented 6 years ago

Hello all !

I try to make a multilingual laravel. To do this, I use :

My slugs are translated and stored in database as follows:

{
   "fr": "a-propos",
   "en":"about"
 }

And all my other translatable data are also like that. And to access one of my data, I use for example, $page->slug or $page->title.

My translations work well. But now, I'm trying to build a menu of languages with the right URLs.

I want, if I am on the page "about" have two links in the menu :

Here is my code :

<ul>
    @foreach(LaravelLocalization::getSupportedLocales() as $localeCode => $properties)
        <li>
            <a rel="alternate" hreflang="{{ $localeCode }}" href="{{ LaravelLocalization::getURLFromRouteNameTranslated($localeCode, $page->slug, [], true) }}">
                {{ $properties['native'] }}
            </a>
        </li>
    @endforeach
</ul>

Unfortunately my urls are bad. If I am on the page "about" in English I have in my menu :

And I would like http://myapp/fr/a-propos. The data is stored in my database.

Is there a way to make sure to collect data from other languages while in a different locale (here I am in EN and I would like to have the slug FR).

Can you help me ? Thank you very much !

ardiwirawan commented 6 years ago

@Jimmylet have you found the solution about this question? i also search the solution, im facing same issue with you.

martin-ro commented 6 years ago

I'd be interested in a solution as well.

simar88 commented 5 years ago

Any news about it? :)

indecenti commented 5 years ago

I'm also facing the problem, and I solve with escamotage but I do not find a correct and sensible way to proceed. Can someone help us?

iwasherefirst2 commented 5 years ago

Its already a build in feature. If you want instead of http://myapp/fr/about the translated route http://myapp/fr/a-propos then just define them in /resources/lang/fr/route.php as explained at https://github.com/mcamara/laravel-localization#translated-routes . If your translated routes are stored in DB or json doesn't matter, you can just iterate them whereever they come from with PHP and add to your array.

indecenti commented 5 years ago

in the routes.php file in the lang folder: return [      'prodotti_with_slug' => 'products / {category} / {slug}', ]; Slug is in the database table called products where I have the slug in one language. Category is in the category table where I have the slug of the category both in Italian and in English. How can I translate category when I'm in a route products / category_1 / product_1. the problem occurs when from the products/category_1/ product_1 route I pass to the italian version or vice versa and macamara translates prodotto/category_1/product_1

thank you

iwasherefirst2 commented 5 years ago

You have to write in /en/routes.php


return [
     'prodotti_with_slug' => 'products / {category} / {slug}',
];

And in in /it/routes.php


return [
     'prodotti_with_slug' => 'prodotto / {category} / {slug}',
];

In your App/route/web.php your need something like this:

Route::group(
[
    'prefix' => LaravelLocalization::setLocale(),
    'middleware' => [ 'localize' ] // Route translate middleware
],
function() {
    /** ADD ALL LOCALIZED ROUTES INSIDE THIS GROUP **/
        Route::get(LaravelLocalization::transRoute('prodotti_with_slug'), 'ProductCategoryController@index');
uaibo commented 4 years ago

@iwasherefirst2 that's ok for the fixed part of the url that in this case is "products" or "prodotti" but what about translating the {category} and {slug} as well? That's what I'm trying to achieve (and I think that's the original question here as well).

So for English we need to have /products/english-category-name/english-slug-name and for French will have /products/french-category-name/french-slug-name

translated category names will need to be saved in DB. The Eloquent query will take the translated slug from URL and perform the query. I've tried several ways with relative success but not a complete robust solution yet. Any suggestion?

uaibo commented 4 years ago

@iwasherefirst2 thanks, I hadn't noticed the video link and couldn't clearly understand how to implement those methods. After watching the video I was able to make translatable url slugs (stored in db) and also translatable content (other content fields) with the help of https://github.com/astrotomic/laravel-translatable

So it's perfectly fine now, using both packages. Thanks.

@uaibo the case for translating {category} and {slug} is working perfectly fine and is document in the docs here: https://github.com/mcamara/laravel-localization#translated-routes

At the end of the docs, I have even linked to a video where I explained step by step how to set it up: https://www.youtube.com/watch?v=B1AUqCdizgc&feature=youtu.be

iwasherefirst2 commented 4 years ago

Awesome!

Altin notifications@github.com schrieb am Di., 4. Feb. 2020, 03:11:

@iwasherefirst2 https://github.com/iwasherefirst2 thanks, I hadn't noticed the video link and couldn't clearly understand how to implement those methods. After watching the video I was able to make translatable url slugs (stored in db) and also translatable content (other content fields) with the help of https://github.com/astrotomic/laravel-translatable

So it's perfectly fine now. Thanks.

@uaibo https://github.com/uaibo the case for translating {category} and {slug} is working perfectly fine and is document in the docs here: https://github.com/mcamara/laravel-localization#translated-routes

At the end of the docs, I have even linked to a video where I explained step by step how to set it up: https://www.youtube.com/watch?v=B1AUqCdizgc&feature=youtu.be

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/mcamara/laravel-localization/issues/512?email_source=notifications&email_token=AANPBYQW427F3ZS5B3764ZTRBDFFHA5CNFSM4EGDAKB2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKWCYBQ#issuecomment-581708806, or unsubscribe https://github.com/notifications/unsubscribe-auth/AANPBYT3SF67ZYVZKQSYFDTRBDFFHANCNFSM4EGDAKBQ .