Skip to content
 
 

Repository files navigation

TranslationKit

TranslationKit is a wrapper for the Swift Translation API, enabling offline translation and making translation functionality available outside of SwiftUI views.

Platform support

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.

🛠 Features

✅ 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.


📦 Installation

Swift Package Manager (SPM)

  1. Open Xcode and go to File > Swift Packages > Add Package Dependency.
  2. Enter the repository URL containing this package.
  3. Click Add Package and import it into your project:
    import TranslationKit

🚨 Important Notes

  • 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.

🚀 Usage

Initialize the Service

You can initialize TranslationKit with the default settings:

let translationService = TranslationService()

This uses Apple’s built-in language recognition and translation session provider.


🌍 Translating Text

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.


🔗 Contributing

Contributions are welcome! Feel free to submit a pull request or open an issue.


📜 License

TranslationKit is available under the MIT License. See the LICENSE file for more details.

About

A wrapper for the Swift Translation API, enabling offline translation and making translation functionality available outside of SwiftUI views.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages