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

how to add translations to models that were inserted into the database before #129

Closed
abbood opened this issue Feb 20, 2018 · 2 comments
Closed

Comments

@abbood
Copy link

abbood commented Feb 20, 2018

So I noticed that waavi creates translation entries for new models after save() is called on them. But for models that already exist in the database, or models that are created using this syntax:

    \DB::table('categories')->insert($categories);

the translation entry is not created.

Realizing that, I created this unit test:

/**
 * tests if we can save translations for legacy models 
 * ie models that have breen created before we implemented 
 * the translation system
 *
 * @access public
 * @return void
 */
public function testCanSaveTranslationForOldModles()
{
    $category = factory(App\Category::class)->create();
    $categories = (array (
        0 => 
        array (
            'ref' => 'fake category',
            'parent_id' => $category->id,
            'image' => 'CannedFood.png',
            'created_at' => '2016-01-27 20:55:43',
            'updated_at' => '2016-01-27 20:55:43',
            'top_parent_id' => $category->id,
            'priority' => 9,
            'deleted_at' => NULL,
        ),
    ));
        
    \DB::table('categories')->insert($categories);
    $savedCategory = Category::where('ref','fake category')->first();
    $savedCategory->addTranslationToField('ref', $this->arabic_text);


    $arTranslation = $this->translationRepository
                          ->findByLangCode('ar', $savedCategory->translationCodeFor('ref'));
    $this->assertEquals($this->arabic_text, $arTranslation->text);
}  

this calls this method:

/**
 * adds translation to specified field in model. 
 * See comments in BAC-306 for a thorough discussion
 *
 * @param string $field (same as attribute in the \Waavi\Translation world)
 * @param string $ar_translation
 * @access public
 * @return void
 */
public function addTranslationToField(string $field, string $ar_translation) 
{
    if (is_null($ar_translation) || $ar_translation == '') return;

    $translation = $this->translationRepository->findByLangCode('en',$this->translationCodeFor($field));
    if ($translation == NULL) {
        // this means that this model is a "legacy" model. 
        // ie it was already saved int the database before 
        // we implemented translation, or else added directly 
        // through raw sql, rather than Eloquent
        // so we do what is already done in 
        // \Waavi\Translation\Traits\TranslatableObserver
        $this->translationRepository->updateDefaultByCode($this->translationCodeFor($field), $this->getRawAttribute($field));
        $cacheRepository       = \App::make('translation.cache.repository');
        $cacheRepository->flush(config('app.locale'), 'translatable', '*');
        $translation = $this->translationRepository->findByLangCode('en',$this->translationCodeFor($field));
    }

    $translation = $this->translationRepository->create([
            'locale'    => 'ar',
            'namespace' => $translation->namespace,
            'group'     => $translation->group,
            'item'      => $translation->item, 
            'text'      => $ar_translation, 
    ]);
}

the problem is that this part:

        $this->translationRepository->updateDefaultByCode($this->translationCodeFor($field), $this->getRawAttribute($field));

which calls

/**
 *  Insert or Update entry by translation code for the default locale.
 *
 *  @param  string  $code
 *  @param  string  $text
 *  @return boolean
 */
public function updateDefaultByCode($code, $text)
{
    list($namespace, $group, $item) = $this->parseCode($code);
    $locale                         = $this->defaultLocale;
    $translation                    = $this->model->whereLocale($locale)->whereNamespace($namespace)->whereGroup($group)->whereItem($item)->first();
    if (!$translation) {
        return $this->create(compact('locale', 'namespace', 'group', 'item', 'text'));
    }
    return $this->update($translation->id, $text);
}

simply doesn't work.. ie this line

        return $this->create(compact('locale', 'namespace', 'group', 'item', 'text'));

isn't creating anything in the database, and so i get this error:

#1) TranslationTraitTest::testCanSaveTranslationForOldModles
ErrorException: Trying to get property of non-object

/Users/Shared/dev/php/toters-api/app/Traits/TranslationTrait.php:144
/Users/Shared/dev/php/toters-api/tests/Localization/TranslationTraitTest.php:62

what am I doing wrong? Why can't I create a new translator entry?

@abbood
Copy link
Author

abbood commented Feb 21, 2018

my answer: abbood@b19baed

@sildraug
Copy link
Contributor

@abbood You could add this as a PR and we could go through it. That way your contribution would be reflected in the project. THanks!

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

No branches or pull requests

2 participants