Skip to content

Commit

Permalink
Adding a static function for current locale to the tag (#368)
Browse files Browse the repository at this point in the history
* Adding a static function for current locale to the tag

* Adding documentation for locale override
  • Loading branch information
leonidlezner authored Nov 17, 2021
1 parent 7a41445 commit 3ec6151
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 6 deletions.
45 changes: 45 additions & 0 deletions docs/advanced-usage/disabling-translations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: Disabling translations
weight: 2
---

In some cases, like setting tags by page visitors in a multi-language environment, the translations are not needed and can even cause "empty" tags. Tag's information like name and description shall contain the same information regardless the current locale, during creating and displaying the tag.

To achieve this behavior you can force the Tag class to use a specific locale rather than getting the current application's locale. First create an own Tag's model and override the function `getLocale()`.

```php

namespace App\Models;

use Spatie\Tags\Tag as SpatieTag;

class YourTag extends SpatieTag
{
public static function getLocale()
{
return 'en';
}
}

```

Then don't forget to change the tag class in tags config (config/tags.php):

```php

return [

/*
* The given function generates a URL friendly "slug" from the tag name property before saving it.
* Defaults to Str::slug (https://laravel.com/docs/master/helpers#method-str-slug)
*/
'slugger' => null,

/*
* The fully qualified class name of the tag model.
*/
'tag_model' => App\Models\YourTag::class,
];


```
2 changes: 1 addition & 1 deletion src/HasTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function tags(): MorphToMany

public function tagsTranslated(string | null $locale = null): MorphToMany
{
$locale = ! is_null($locale) ? $locale : app()->getLocale();
$locale = ! is_null($locale) ? $locale : self::getTagClassName()::getLocale();

return $this
->morphToMany(self::getTagClassName(), 'taggable')
Expand Down
15 changes: 10 additions & 5 deletions src/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class Tag extends Model implements Sortable

public $guarded = [];

public static function getLocale()
{
return app()->getLocale();
}

public function scopeWithType(Builder $query, string $type = null): Builder
{
if (is_null($type)) {
Expand All @@ -34,7 +39,7 @@ public function scopeWithType(Builder $query, string $type = null): Builder

public function scopeContaining(Builder $query, string $name, $locale = null): Builder
{
$locale = $locale ?? app()->getLocale();
$locale = $locale ?? static::getLocale();

return $query->whereRaw('lower(' . $this->getQuery()->getGrammar()->wrap('name->' . $locale) . ') like ?', ['%' . mb_strtolower($name) . '%']);
}
Expand Down Expand Up @@ -62,7 +67,7 @@ public static function getWithType(string $type): DbCollection

public static function findFromString(string $name, string $type = null, string $locale = null)
{
$locale = $locale ?? app()->getLocale();
$locale = $locale ?? static::getLocale();

return static::query()
->where("name->{$locale}", $name)
Expand All @@ -72,7 +77,7 @@ public static function findFromString(string $name, string $type = null, string

public static function findFromStringOfAnyType(string $name, string $locale = null)
{
$locale = $locale ?? app()->getLocale();
$locale = $locale ?? static::getLocale();

return static::query()
->where("name->{$locale}", $name)
Expand All @@ -81,7 +86,7 @@ public static function findFromStringOfAnyType(string $name, string $locale = nu

protected static function findOrCreateFromString(string $name, string $type = null, string $locale = null)
{
$locale = $locale ?? app()->getLocale();
$locale = $locale ?? static::getLocale();

$tag = static::findFromString($name, $type, $locale);

Expand All @@ -103,7 +108,7 @@ public static function getTypes(): Collection
public function setAttribute($key, $value)
{
if (in_array($key, $this->translatable) && ! is_array($value)) {
return $this->setTranslation($key, app()->getLocale(), $value);
return $this->setTranslation($key, static::getLocale(), $value);
}

return parent::setAttribute($key, $value);
Expand Down
31 changes: 31 additions & 0 deletions tests/CustomStaticLocaleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spatie\Translatable\Test;

use Spatie\Tags\Test\TestCase;
use Spatie\Tags\Test\TestClasses\TestCustomTagStaticLocaleModel;

class CustomStaticLocaleTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

$this->assertCount(0, TestCustomTagStaticLocaleModel::all());
}

/** @test */
public function it_can_use_static_locale()
{
app()->setLocale('es');

$tag = TestCustomTagStaticLocaleModel::findOrCreateFromString('string');

$staticLocale = 'en';

$translated = TestCustomTagStaticLocaleModel::where('name', 'LIKE', '%' . $staticLocale . '%')->first()->toArray();

$this->assertEquals('string', $translated['name'][$staticLocale]);
$this->assertCount(1, TestCustomTagStaticLocaleModel::all());
}
}
9 changes: 9 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,14 @@ protected function setUpDatabase($app)
$table->integer('order_column')->nullable();
$table->timestamps();
});

Schema::create('custom_tags_static_locale', function (Blueprint $table) {
$table->id();
$table->json('name');
$table->json('slug');
$table->string('type')->nullable();
$table->integer('order_column')->nullable();
$table->timestamps();
});
}
}
15 changes: 15 additions & 0 deletions tests/TestClasses/TestCustomTagStaticLocaleModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Spatie\Tags\Test\TestClasses;

use Spatie\Tags\Tag;

class TestCustomTagStaticLocaleModel extends Tag
{
public $table = 'custom_tags_static_locale';

public static function getLocale()
{
return 'en';
}
}

0 comments on commit 3ec6151

Please sign in to comment.