diff --git a/docs/advanced-usage/disabling-translations.md b/docs/advanced-usage/disabling-translations.md new file mode 100644 index 0000000..bc56666 --- /dev/null +++ b/docs/advanced-usage/disabling-translations.md @@ -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, +]; + + +``` \ No newline at end of file diff --git a/src/HasTags.php b/src/HasTags.php index afbdb47..026eab9 100644 --- a/src/HasTags.php +++ b/src/HasTags.php @@ -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') diff --git a/src/Tag.php b/src/Tag.php index 9254963..bab1079 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -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)) { @@ -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) . '%']); } @@ -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) @@ -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) @@ -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); @@ -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); diff --git a/tests/CustomStaticLocaleTest.php b/tests/CustomStaticLocaleTest.php new file mode 100644 index 0000000..513d368 --- /dev/null +++ b/tests/CustomStaticLocaleTest.php @@ -0,0 +1,31 @@ +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()); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 5736d21..8e65148 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -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(); + }); } } diff --git a/tests/TestClasses/TestCustomTagStaticLocaleModel.php b/tests/TestClasses/TestCustomTagStaticLocaleModel.php new file mode 100644 index 0000000..f89b3e9 --- /dev/null +++ b/tests/TestClasses/TestCustomTagStaticLocaleModel.php @@ -0,0 +1,15 @@ +