Skip to content

Commit

Permalink
Convenience method to return tags with already translated and decoded…
Browse files Browse the repository at this point in the history
… name and slug fields (#175)

* Return tags with translated name and slug fields

* add line before return

* final tweaks

* fix styling

* tests for tagsTranslated

* readme examples updated

* fix test style

* remove whitespace

* add void return type to test setUp method
  • Loading branch information
joebenson authored and freekmurze committed Mar 1, 2019
1 parent 4964bc4 commit b068c9b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
12 changes: 10 additions & 2 deletions README.md
Expand Up @@ -49,10 +49,18 @@ NewsItem::withAllTags(['tag1', 'tag2'])->get();

//translating a tag
$tag = Tag::findOrCreate('my tag');
$tag->setTranslation('fr', 'mon tag');
$tag->setTranslation('nl', 'mijn tag');
$tag->setTranslation('name', 'fr', 'mon tag');
$tag->setTranslation('name', 'nl', 'mijn tag');
$tag->save();

//getting translations
$tag->translate('name'); //returns my name
$tag->translate('name', 'fr'); //returns mon tag (optional locale param)

//convenient translations through taggable models
$newsItem->tagsTranslated();// returns tags with slug_translated and name_translated properties
$newsItem->tagsTranslated('fr');// returns tags with slug_translated and name_translated properties set for specified locale

//using tag types
$tag = Tag::findOrCreate('tag 1', 'my type');

Expand Down
15 changes: 15 additions & 0 deletions src/HasTags.php
Expand Up @@ -39,6 +39,21 @@ public function tags(): MorphToMany
->orderBy('order_column');
}

/**
* @param string $locale
*/
public function tagsTranslated($locale = null): MorphToMany
{
$locale = ! is_null($locale) ? $locale : app()->getLocale();

return $this
->morphToMany(self::getTagClassName(), 'taggable')
->select('*')
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(name, '$.\"{$locale}\"')) as name_translated")
->selectRaw("JSON_UNQUOTE(JSON_EXTRACT(slug, '$.\"{$locale}\"')) as slug_translated")
->orderBy('order_column');
}

/**
* @param string|array|\ArrayAccess|\Spatie\Tags\Tag $tags
*/
Expand Down
49 changes: 49 additions & 0 deletions tests/HasTagsTranslatedTest.php
@@ -0,0 +1,49 @@
<?php

namespace Spatie\Translatable\Test;

use Spatie\Tags\Tag;
use Spatie\Tags\Test\TestCase;
use Spatie\Tags\Test\TestModel;

class HasTagsTranslatedTest extends TestCase
{
/** @var \Spatie\Tags\Test\TestModel */
protected $testModel;

public function setUp(): void
{
parent::setUp();

$this->testModel = TestModel::create(['name' => 'default']);
}

/** @test */
public function it_provides_models_with_tag_name_and_slug_already_translated()
{
$this->testModel->attachTag('My Tag');

$locale = app()->getLocale();
$translated = $this->testModel->tagsTranslated->first()->toArray();

$this->assertEquals($translated['name_translated'], $translated['name'][$locale]);
$this->assertEquals($translated['slug_translated'], $translated['slug'][$locale]);
}

/** @test */
public function it_can_provide_models_with_tag_name_and_slug_translated_for_alternate_locales()
{
$this->testModel->attachTag('My Tag');

$locale = 'fr';

$tag = $this->testModel->tags->first();
$tag->setTranslation('name', $locale, 'Mon tag');
$tag->save();

$translated = $this->testModel->tagsTranslated($locale)->first()->toArray();

$this->assertEquals($translated['name_translated'], $translated['name'][$locale]);
$this->assertEquals($translated['slug_translated'], $translated['slug'][$locale]);
}
}

0 comments on commit b068c9b

Please sign in to comment.