Which output should I expect from allTranslatedIn($lang) method?

  • I was wondering what is the correct output I'd to expect from allTranslatedIn($lang) method, as I'm a bit confused about the returned values.

    If I understand correctly, allTranslatedIn($lang) should return all content the variable $lang specifies?

    Or should it return all posts which are effectively translated in that current language? If so, which would be the best approach to create a method in the Eloquent{Module_name}Repository that returns only the content in a specified $lang ?

    Can anyone bring me out of confusion?

  • Global Moderator


    Well if your repository extends EloquentBaseRepository (which is the case if you scaffold module) then it's all translated content for specific Entity in that language.
    You would just call $this->allTranslatedIn($yourLang); in your Eloquent{Module_name}Repository to get all for specific Entity, for specific language.
    Is this what you are asking for?

  • Yes, that's it.

    But instead of that, I'm having a collection with all content, in the two languages I have set up, instead of the content just in the locale I pass to the $lang variable.

    My Repositories structure is the following:

    Eloquent > EloquentTestimonialRepository which Class is defined as:

    class EloquentTestimonialRepository extends EloquentBaseRepository implements TestimonialRepository

    And then TestimonialRepository which interface is defined as:

    interface TestimonialRepository extends BaseRepository

    I understand that if EloquentTestimonialRepository extends EloquentBaseRepository, the $this->allTranslatedIn($lang) should work as intended? Or perhaps I'm doing this wrong?

    My controller code (excerpt):

    public function index(Request $request)
        $testimonials = $this->testimonials->allTranslatedIn($request->lang)->all();
        return response()->json(compact('testimonials'));

    Still a bit confused about where to use allTranslatedIn....

  • Global Moderator


    Edit: I also tested this now and i see what you are talking about, it returns all translations for Entity even if specified language.
    The way it works now is returns you Post that has xx specified translation.
    It does not exclude all other translations, so it will return all of them where your requested one is presented.

    I ran some sql logs and heres how it's handeled on sql side.

    First it gets all from pages with translations where your translation is present:

    select * from `page__pages` where (select count(*) from `page__page_translations` where `page__page_translations`.`page_id` = `page__pages`.`id` and `locale` = ? and `title` != ?) >= 1 order by `created_at` desc

    Then it takes found translation page_id's and requests all pages with those id(including translations):

    select * from `page__page_translations` where `page__page_translations`.`page_id` in (?, ?)

    Can't really say that that would be incorrect, since it does what it says. Probably you would need to cook something up for yourself if you want to return only your language translations (maybe sort collections and remove ones you don't need).

  • Yes, that actually resolves my doubt. So it returns all translations for Entity. And the method, literally does what it says to do :smile:

    After driving myself into oblivion trying to make that request return what I was looking for, I finally found the way to get what I want:

    This would be the code for the index method (instead of returning a view, I return JSON responses, because my app's design).

    public function index(Request $request)
            if ($request->lang === null) {
                $request->lang = 'en';
            $testimonials = $this->testimonials->allTranslatedIn($request->lang);
            foreach ($testimonials as $testimonial) {        	
            	$langTestimonials[] = [
            	    'content'  => $testimonial->translations->where('locale', $request->lang)->first(),
            	    'media'    => [
            	        $this->file->findFileByZoneForEntity('video_poster_' . $request->lang, $testimonial),
            	        $this->file->findFileByZoneForEntity('video_' . $request->lang, $testimonial),
            return response()->json(compact('langTestimonials'));

    And yay, I got what I expected: the translated articles with its matching related media :dancer:

    Another alternative I thought (and I think that would be the straight way to go), would be to create a couple of methods in the TestimonialEloquentRepository, so with a single call within the controller you get similar results.

Log in to reply

Looks like your connection to AsgardCms was lost, please wait while we try to reconnect.