-
-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a static function for current locale to the tag (#368)
* Adding a static function for current locale to the tag * Adding documentation for locale override
- Loading branch information
1 parent
7a41445
commit 3ec6151
Showing
6 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |