- Kotlin 96.8%
- Swift 2.6%
- Shell 0.6%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
Napier discards logs until a base is installed, so the library fail-open paths (provider unavailable, request timed out, malformed results) were silent and a dead provider looked identical to clean text. Add initLogging() in commonMain, called from the Android, desktop, and iOS entry points, and document the fail-open behavior in the README. |
||
| .github/workflows | ||
| docs | ||
| exampleApp | ||
| gradle | ||
| iosApp | ||
| PlatformSpellChecker | ||
| .gitignore | ||
| build.gradle.kts | ||
| CLAUDE.md | ||
| gradle.properties | ||
| gradlew | ||
| gradlew.bat | ||
| LICENSE | ||
| README.md | ||
| settings.gradle.kts | ||
Platform Spell Checker Kt
This Kotlin Multiplatform library wraps the OS's native spell checker on each supported platform. It then provides a Kotlin friendly API.
The big advantage here is that nothing needs to be bundled with your program. Especially dictionaries. Most likely, what ever the user's language is, they will already have the proper dictionary on their device for the system level Spell Checker.
Note: Each platform can and will give different results for the same word. This library does not attempt to correct these results.
Documentation
Full API reference and recipes: darkrock-studios.github.io/PlatformSpellCheckerKt.
Dependency
implementation("com.darkrockstudios:platform-spellcheckerkt:1.0.0")
Usage
Create a Spell Checker
// Android's PlatformSpellCheckerFactory requires a context, thus you will need to construct it
// in platform-specific code and pass it down into common code.
val factory = PlatformSpellCheckerFactory(context)
val factory = PlatformSpellCheckerFactory()
// Use the user's current/system language
val checkerDefault = factory.createSpellChecker()
// Or request a specific locale (validated). Country is optional.
val checkerUkUa = factory.createSpellChecker(SpLocale.UK_UA)
// Check before you create! Creating a Spell Checker for a locale that isn't supported will throw an exception
if (factory.hasLanguage(SpLocale.EN_GB)) {
val gbChecker = factory.createSpellChecker(SpLocale.EN_GB)
}
// You can also create a checker manually
val checker = PlatformSpellChecker(SpLocale.EN_US)
Use a Spell Checker
// Check a single word for spelling errors
val result = checker.checkWord("mispelledWord")
if (isMisspelled(result)) {
result.suggestions.forEach { println(it) }
}
// Sentence level correct is also supported
val misspellings = checker.checkMultiword("This is a sentence with a mispelled word.")
misspellings.forEach { println("${it.misspelledWord} at ${it.startIndex} with length ${it.length}") }
Utilities
val factory = PlatformSpellCheckerFactory()
// Is a spell checker available on this platform at runtime?
val available = factory.isAvailable()
// What locale does the system currently use?
val systemLocale: SpLocale = factory.currentSystemLocale()
// A best-effort list of available Spell Checker locales on this device
val locales: List<SpLocale> = factory.availableLocales()
Logging
Spell check calls fail open. If the platform provider is unavailable, does not respond within the operation timeout, or returns something malformed, the check degrades to "correctly spelled" rather than throwing. This keeps a flaky system service from crashing your app, but it also means a broken provider looks exactly like a document with no typos.
Every one of those paths logs a warning through Napier, which discards logs until a base is installed. Install one during startup or you will get no warning at all:
import io.github.aakira.napier.DebugAntilog
import io.github.aakira.napier.Napier
Napier.base(DebugAntilog())
Worth knowing when something reports no spelling errors for anything: Android emulator images often have a spell checker service that binds but never responds, so every check hits the timeout. With logging installed that shows up as:
W PlatformSpellChecker$checkWord: checkWord('boooks') timed out after 3s
— spell-checker provider did not respond
Test on a physical device before concluding the library is at fault.
Supported Platforms
| Platform | Supported | Implementation Details |
|---|---|---|
| Android | ✅ | Wraps TextServicesManager's SpellCheckerSession with full featured support. |
| iOS | ✅ | Wraps UITextChecker for full featured support. |
| JVM - Windows | ✅ | Wraps the Win32 API COM Object for ISpellCheckerFactory for full featured support. |
| JVM - Linux | ✅ | Linux has no native spell checker API, so this library looks to see if hunspell is installed, and uses it, otherwise it reports Spell Checking is not supported. Single word spell checking is a first class citizen, but sentence level correct is really just decomposing the sentence into individual single word checks, so is not as robust as other platforms. |
| JVM - macOS | ⚠️ | Wraps NSSpellChecker for partial support.Known issues: • There is an issue with JNA calling into certain platform APIs, so sentence level correct is hacked together right now. • MacOS has a bit of a weird spell checker, the words it considers "correct" are not always what I would expect, but who am I to question the wisdom of Tim Apple. |
| K/N - Winx86 | ❓ | Support is certainly possible, but there aren't any examples I've found for binding COM objects in K/N, so would take some exploration. |
| WASM-JS | ❌ | Can't work I don't think because there is no standard browser spell checker API as far as I have found. |