Hyphen
Hyphen is a cross-platform Flutter plugin that provides high-quality word hyphenation.
It uses the hunspell/hyphen C library under the hood (via FFI)
on native platforms, and a WebAssembly/JS runtime on the Web.
With Hyphen, you can split words into their hyphenation parts according to language-specific rules. The API returns a List
❗️ Breaking change in v0.2.0
- See Changelog for details
✨ Features
- Works on all Flutter platforms: Android, iOS, macOS, Windows, Linux, Web
- Uses battle-tested hyphen dictionaries
- Combines hunspell/hyphen's two hyphenation APIs
hnj_hyphen_hyphenate2
andhnj_hyphen_hyphenate3
into a singlehyphenate
function - Unified API for all platforms – always use the same
Hyphen
class, no matter the platform
📦 Installing
Add to your pubspec.yaml
:
dependencies:
hyphen: ^0.2.0
Then run:
flutter pub get
📚 Dictionaries
Hyphen
requires a .dic
file for the language you want to hyphenate.
These are not bundled due to licensing reasons.
👉 You need to generate or obtain these .dic
files yourself.
Where to get dictionary files
Hyphenation dictionaries are created from TeX hyphenation pattern files (commonly available on CTAN and other TeX distribution sources).
🔨 Step-by-step Example: English (US)
-
Download the pattern file
hyph-en-us.tex
-
Run the
substrings.pl
script (from hunspell/hyphen):perl substrings.pl hyph-en-us.tex hyph_en_US.dic UTF-8
This generates a file called
hyph_en_US.dic
. -
Add the file to your Flutter project:
assets/hyph_en_US.dic
-
Declare it in
pubspec.yaml
:flutter: assets: - assets/hyph_en_US.dic
🚀 Usage
import 'package:hyphen/hyphen.dart';
Future<void> main() async {
// Load a dictionary from assets
final hyphen = await Hyphen.fromDictionaryPath('assets/hyph_en_US.dic');
// Hyphenate a word
final result = hyphen.hyphenate('hyphenation');
print(result); // ["hy", "phen", "ation"]
// Using the additional parameters to define a minimum distance from the start/end of the word
// to the first break
final result2 = hyphen.hyphenate(
'hyphenation',
lhmin: 3,
rhmin: 3,
);
print(result2); // ["hyphen", "ation"]
}
🖥 Platform Notes
- Android/iOS/macOS/Linux/Windows: Uses the native hyphen lib via FFI.
- Web: Uses a WASM build of the hyphen lib via
hyphen.js
. - On all platforms, you must provide your own
.dic
file.
⚠️ License
This package is dual-licensed:
- Plugin code (Dart, FFI bindings and wrappers): licensed under MIT.
- Hyphenation engine: incorporates code from Hunspell/Hyphen, which is licensed under the Mozilla Public License (MPL).
Hyphenation dictionaries come with their own licenses – check the
hunspell/hyphen repo before redistributing.
🤝 Contributing
Issues and pull requests are welcome!