Skip to content

Commit

Permalink
Add more info on enabling rulesets for foreign languages
Browse files Browse the repository at this point in the history
  • Loading branch information
cviebrock committed Sep 25, 2024
1 parent b902f75 commit 0f501d5
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/.php_cs.cache
/.phpunit.cache
/.phpunit.result.cache
/.idea/
/build/
/vendor/
composer.lock
Expand Down
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,36 @@ for Slugify for what those options are. Also, look at
[customizeSlugEngine](#customizeslugengine) for other ways to customize Slugify
for slugging.
A common use for this is to turn on a different ruleset for a specific language.
For example the string `Jyväskylä` will slug to `jyvaeskylae` using the default settings.
In Finnish, it really should slug to `jyvaskyla`, so for that to work, you need to enable
the Finnish ruleset for the attribute you are slugging:
```php
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title',
'slugEngineOptions' => [
'ruleset' => 'finnish'
]
]
];
}
```
This can also be accomplished with the [customizeSlugEngine](#customizeslugengine) method
(which, unless you add custom logic, will apply to _all_ attributes on the model):
```php
public function customizeSlugEngine(Slugify $engine, string $attribute): \Cocur\Slugify\Slugify
{
$engine->activateRuleSet('finnish');
return $engine;
}
```
## Short Configuration
Expand Down
12 changes: 12 additions & 0 deletions tests/BaseTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cviebrock\EloquentSluggable\Tests\Models\PostWithCustomMethodArrayCall;
use Cviebrock\EloquentSluggable\Tests\Models\PostWithCustomSeparator;
use Cviebrock\EloquentSluggable\Tests\Models\PostWithCustomSource;
use Cviebrock\EloquentSluggable\Tests\Models\PostWithForeignRuleset2;
use Cviebrock\EloquentSluggable\Tests\Models\PostWithIdSource;
use Cviebrock\EloquentSluggable\Tests\Models\PostWithCustomSuffix;
use Cviebrock\EloquentSluggable\Tests\Models\PostWithEmptySeparator;
Expand Down Expand Up @@ -428,6 +429,17 @@ public function testForeignRuleset(): void
self::assertEquals('mia-unua-posxto', $post->slug);
}

/**
* Test using a custom Slugify ruleset.
*/
public function testForeignRuleset2(): void
{
$post = PostWithForeignRuleset2::create([
'title' => 'Jyväskylä'
]);
self::assertEquals('jyvaskyla', $post->slug);
}

/**
* Test if using an empty separator works.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Models/PostWithForeignRuleset2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace Cviebrock\EloquentSluggable\Tests\Models;

/**
* Class PostWithForeignRuleset
*
* A test model that customizes the Slugify engine with a foreign ruleset.
*
* @package Cviebrock\EloquentSluggable\Tests\Models
*/
class PostWithForeignRuleset2 extends Post
{

/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title',
'slugEngineOptions' => [
'ruleset' => 'finnish'
],
],
];
}
}

0 comments on commit 0f501d5

Please sign in to comment.