Astrotomic / laravel-translatable

A Laravel package for multilingual models
https://docs.astrotomic.info/laravel-translatable/
MIT License
1.25k stars 157 forks source link

how to translate all the rows at one time #22

Closed MohammedYAmmar closed 5 years ago

MohammedYAmmar commented 5 years ago
$categories = Category::all();

I want it to return

{
    "success":true,
    "data":[
        {
            "id":21,
            "category_id":2,
            "locale":"en",
            "name":"cat1",
            "details":"detials1",
            "image":"image1"
        }
    ]
}

for all the rows not like this

{
    "success":true,
    "data":[
        {
            "id":2,
            "created_at":null,
            "updated_at":null,
            "deleted_at":null,
            "name":"cat1",
            "translations":[
                {
                    "id":21,
                    "category_id":2,
                    "locale":"en",
                    "name":"cat1",
                    "details":"detials1",
                    "image":"image1"
                }
            ]
        }
    ]
}

with out translation object

Gummibeer commented 5 years ago

Hey,

you should hide the key via one of the described ways in the laravel docs like following:

Category::all()->makeHidden('translations');
MohammedYAmmar commented 5 years ago

thanks it works But Is there any way to shorten this code

$categories = Category::all()->makeHidden('translations');
foreach ($categories as $category) {
    $category->setDefaultLocale($request->header('lang'));
}
Gummibeer commented 5 years ago

https://github.com/Astrotomic/laravel-translatable/blob/8ec38419ef538e1e973dace59503299462dfca05/src/Translatable/Locales.php#L46-L49

You can set the translatable.locale config or change the app locale. These will set the locale for all translation models for the current runtime. In your case I would set the app locale in a middleware. This will remove this logic from the controllers and run it for all requests.

MohammedYAmmar commented 5 years ago

i have used this

 App::setLocale($request->header('lang'));

but it's not work

Gummibeer commented 5 years ago

What means "not work"? What's in your $request->header('lang')? What does app('translatable.locales')->current() return? Do you call it before the category query (you have to do so) or after it?

MohammedYAmmar commented 5 years ago

it not translate to the locale i send from the header

MohammedYAmmar commented 5 years ago

and i have done this but it is a still in the return the default locale in the config file

<?php

namespace App\Http\Middleware;

use Closure;

class SetLocale
{
     /**
      * Handle an incoming request.
      *
      * @param  \Illuminate\Http\Request  $request
      * @param  \Closure  $next
      * @return mixed
      */
    public function handle($request, Closure $next)
    {
        \App::setLocale($request->header('lang'));
        return $next($request);
    }
}
Gummibeer commented 5 years ago

If you've set a locale in config translatable.locale this won't be overridden. But you don't have to set it. It's just if you want to handle this packages locale separated from the app. In this case you can use config('translatable.locale', $request->header('lang')) to change it.

As long as the config returns something we won't read the translator/app locale to get the current one. https://github.com/Astrotomic/laravel-translatable/blob/8ec38419ef538e1e973dace59503299462dfca05/src/Translatable/Locales.php#L46-L49

MohammedYAmmar commented 5 years ago

thank you so much it work now