bitandblack / hyphenizer-bundle
Perfect hyphenation for your Website. This library integrates the Hyphenizer into your Symfony project.
Package info
github.com/BitAndBlack/hyphenizer-bundle
Type:symfony-bundle
pkg:composer/bitandblack/hyphenizer-bundle
Fund package maintenance!
Requires
- php: >=8.2
- bitandblack/hyphenizer-sdk-php: ^0
- kiwa/hyphenizer: ^1.0
- nyholm/psr7: ^1.0
- symfony/console: ^7.0 || ^8.0
- symfony/framework-bundle: ^7.0 || ^8.0
- symfony/http-client: ^7.0 || ^8.0
- twig/twig: ^3.0
Requires (Dev)
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^13.0
- rector/rector: ^2.0
- symplify/easy-coding-standard: ^13.0
README
Bit&Black Hyphenizer Bundle
Perfect hyphenation for your Website. This library integrates the Hyphenizer into your Symfony project.
If you don't run your website with Symfony, you can integrate the Hyphenizer SDK for PHP by your own.
Please note: The Hyphenizer API requires you to have a valid API token.
Installation
This library is made for the use with Composer. Add it to your project by running $ composer require bitandblack/hyphenizer-bundle.
Usage
Basic setup and usage of the Bit&Black Hyphenizer Bundle
The Bit&Black Hyphenizer bundle offers a smooth way to extract and hyphenate long words automatically. It implements the Hyphenizer SDK for PHP and allows to update the extracted words.
To use this library in your project, wrap your texts with the AutoHyphenation class. This will add soft hyphens to all words, that are long enough to be hyphenated:
<?php use BitAndBlack\HyphenizerBundle\AutoHyphenation; echo new AutoHyphenation('Wir suchen nach Bodenseefelchen.');
It's also possible to hyphenate using a Twig filter:
<p>{{ 'Wir suchen nach Bodenseefelchen.'|hyphenate }}</p>
Inside, the class extracts all words having more than 12 characters, and searches for a previously defined hyphenation.
You can change this value when initialising the class.
The hyphenations themselves are stored inside a JSON file using a pipe, for example
{
"Bodenseefelchen": "Bodensee|felchen"
}
whereas the pipe is getting replaced afterwards by the defined hyphenation character. This is a soft hyphen per default. You can change the hyphenation character when initialising the class.
The JSON file is getting stored under /hyphenation/words-hyphenated.json.
The list of extracted words will grow over time. You can add hyphenations by yourself, by modifying the JSON file, but it's also possibly to do this via script:
<?php use BitAndBlack\HyphenizerBundle\HyphenationLibraryFactory; HyphenationLibraryFactory::create()->setHyphenationWords([ 'Bodenseefelchen' => 'Bodensee|felchen', ]);
This is the manual approach. The approach that you’ll really enjoy is:
Using the Hyphenation API for automatic hyphenation
This library provides a command, to contact the Hyphenation API and ask for validated hyphenations:
$ bin/console hyphenation:list:hyphenate
The response will contain more information about every hyphenated word, for example a score and if the hyphenation has been approved.
The response will also be stored in the words-hyphenated.json file.
Extracting words manually
If you don't want to wait, until your hyphenation list contains all words from your project, you can manually scan your files and extract the words from there:
$ bin/console hyphenation:list:create
Using the hyphenations with JavaScript
If you want to use the hyphenations in JavaScript, you can use the Hyphenation library written in JavaScript to do that. In this case, you need to import your words-hyphenated.json before initialising the class:
import { Hyphenation } from "kiwa-hyphenizer"; import hyphenatedWords from "hyphenation/words-hyphenated.json"; const hyphenation = new Hyphenation(hyphenatedWords.words);
Help
If you have any questions, feel free to contact us under hello@bitandblack.com.
Further information about Bit&Black can be found under www.bitandblack.com.