Waavi/translation

using model attribute translations doesn't add an entry to the translations db

abbood opened this issue · 1 comments

I was going over this unit test

    /**
     * @test
     */
    public function it_saves_translations()
    {
        $dummy        = new Dummy;
        $dummy->title = 'Dummy title';
        $dummy->text  = 'Dummy text';
        $saved        = $dummy->save() ? true : false;
        $this->assertTrue($saved);
        $this->assertEquals(1, Dummy::count());
        $this->assertEquals('slug', $dummy->slug);
        // Check that there is a language entry in the database:
        $titleTranslation = $this->translationRepository->findByLangCode('en', $dummy->translationCodeFor('title'));
        $this->assertEquals('Dummy title', $titleTranslation->text);
        $this->assertEquals('Dummy title', $dummy->title);
        $textTranslation = $this->translationRepository->findByLangCode('en', $dummy->translationCodeFor('text'));
        $this->assertEquals('Dummy text', $textTranslation->text);
        $this->assertEquals('Dummy text', $dummy->text);
        // Delete it:
        $deleted = $dummy->delete();
        $this->assertTrue($deleted);
        $this->assertEquals(0, Dummy::count());
        $this->assertEquals(0, $this->translationRepository->count());
    }

and it shows that If i save a model that uses the translatable trait, then an entry in the translations database should show (i'm using laravel 5.3)

however this doesn't work in my code:

my store model class:

<?php

namespace App;

..
use \Waavi\Translation\Traits\Translatable;

class Store extends Model
{
    use Translatable;
    ..
    protected $translatableAttributes = ['ref'];

see tinker commands:

>>> $s2 = Store::find(2)
=> App\Store {#1314
     id: 2,
     ref: "Test Abd el Wahab",
     ..
     ref_translation: null,
>>> $s2->ref_translation="حمرا"
=> "حمرا"
>>> $s2->save();
=> true
>>> $refTranslation = $tr->findByLangCode('en', $s2->translationCodeFor('ref'));
=> null
>>> $s2->isTranslated('ref');
=> true
>>> $s2->translate('ref');
=> "حمرا"

>>> $s2
=> App\Store {#1314
     id: 2,
     ref: "Test Abd el Wahab",
     ..
     ref_translation: "حمرا",
   }

so my problem is with this line:

$refTranslation = $tr->findByLangCode('en', $s2->translationCodeFor('ref'))

why is it returning null? also i couldn't find an entry in the database for the above (ie SELECT * FROM translator_translations;)

please tell me what i'm doing wrong

@abbood I know this is a bit late, but make sure you have a Language with the 'en' locale in the Languages table before adding the translation. Hope this helps!