abeliani/translated-slug-helper

Helps to get a valid slug translated or transliterated by a driver collection

v1.0.0 2024-05-08 21:48 UTC

This package is auto-updated.

Last update: 2024-11-08 23:02:27 UTC


README

The package uses libraries StringTranslator and SlugHelper.

Installation

composer require abeliani/translated-slug-helper

Examples

Example of offline driver

Blog posts often require a slug to generate a url. We can use a transliteration driver which simply transliteration the symbol table for example: u-у and vice versa.

$slug = new TranslatedSlugHelper(
    new Settings('ru'),
    new TranslatedBy(Translit::class)
);

print $slug->from('Привет мир!', 'en'); // privet-mir

Example of online driver

Online drivers perform translate a text.

$slug = new TranslatedSlugHelper(
    new Settings('ru', ['a', 'an']), // to remove words from slug (eg. a, an) we can pass them by array
    new TranslatedBy(MyMemory::class, ['apiKey' => 'someapikey'])
);

print $slug->from('Привет мир!', 'en'); // hello-world

Example of driver chain

An online driver will suddenly be unavailable (for example), in this case we can pass a string for translation from one driver to another along the chain.

$chain = [
   new TranslatedBy(MyMemory::class, ['apiKey' => 'someapikey']),
   new TranslatedBy(Translit::class),
];

$slug = new TranslatedSlugHelper(
    new Settings('ru'),
    ...$chain,
);

print $slug->from('Привет мир!', 'en'); // if MyMemory service is available: hello-world | otherwise by transilt driver: privet-mir

If you need to change the word separator. you can pass it through settings object

// Passing new divider +
new Settings('ru', [], '+')