TranslationKit is a wrapper for the Swift Translation API, enabling offline translation and making translation functionality available outside of SwiftUI views.
| Platform | Minimum deployment target | Behavior |
|---|---|---|
| iOS | 13.0 (translation API: 18.0) | On-device translation is available when called from an active app window. |
| macOS | 14.0 (translation API: 15.0) | On-device translation is available on macOS 15 or later when called from an active app window. |
✅ Use Outside of SwiftUI – Enables translation in any Swift-based app or service.
✅ Offline Translation – Works without an internet connection using Apple's on-device translation API.
✅ Automatic Language Detection – Detects the source language if not explicitly provided.
✅ Swift Concurrency Support – Built with async/await for modern, non-blocking execution.
✅ Modular & Extendable – Use default services or provide custom implementations.
- Open Xcode and go to File > Swift Packages > Add Package Dependency.
- Enter the repository URL containing this package.
- Click Add Package and import it into your project:
import TranslationKit
- Active Window Required on iOS and macOS: TranslationKit needs an active app window and root/content view controller to host the SwiftUI translation task. This makes it unsuitable for background-only environments.
- Not Compatible with Background Tasks: Since it relies on window scene access, it may not function correctly in scenarios where the app isn't in the foreground or doesn't have an active UI context.
You can initialize TranslationKit with the default settings:
let translationService = TranslationService()This uses Apple’s built-in language recognition and translation session provider.
To translate text, call the translate function asynchronously:
let sourceText = "Hello, how are you?"
let targetLanguage: Locale.Language = .french
do {
let response = try await translationService.translate(sourceText: sourceText, from: nil, to: targetLanguage)
print("Translated Text: \(response.result)")
} catch {
print("Translation failed: \(error)")
}If from is nil, TranslationKit will automatically detect the source language.
Contributions are welcome! Feel free to submit a pull request or open an issue.
TranslationKit is available under the MIT License. See the LICENSE file for more details.