Expand description
§translation-rs
Safe Rust bindings for Apple’s Translation.framework on macOS, plus typed language helpers for canonical language identifiers, language pairs, translation configuration state, translation responses, translation errors, and NaturalLanguage-backed language recognition.
Status: v0.3.0 covers all public
Translation.frameworksymbols in the current macOS SDK and adds a Tier-1async_apimodule for Future-based translation and availability workflows.
§Quick start
use translation::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let source = Language::new("en").canonicalized()?;
let target = Language::new("es").canonicalized()?;
let pair = LanguagePair::between(source.clone(), target.clone()).canonicalized()?;
let availability = LanguageAvailability::new()?;
println!(
"{pair:?} status: {:?}",
availability.status_for_language_pair(&pair)?
);
let mut configuration = TranslationConfiguration::new()
.with_source(source)
.with_target(target);
configuration.invalidate();
let session = TranslationSession::from_translation_configuration(&configuration)?;
match session.translate("hello world") {
Ok(response) => println!("{}", response.target_text()),
Err(error) => println!("translation unavailable: {error}"),
}
println!("recognized: {:?}", recognize_language("hello world")?);
Ok(())
}§Highlights
LanguageandLanguagePairwrappers that canonicalizeLocale.Languageidentifiers through the Swift bridge.LanguageAvailabilityaccess tosupportedLanguages, pair status, and text status, including optional target-language queries.TranslationConfigurationfor mutable source/target configuration state and invalidation version tracking.TranslationSessionwithcan_request_downloads,is_ready,cancel, eager batch translation, and streamingtranslate(batch:)iteration.TranslationRequestsetters plusTranslationResponseconstructors, serde support, and typed language accessors.TranslationErrorvariants that preserve botherrorDescriptionandfailureReasonfromTranslation.frameworkerrors.recognize_language/detect_languagefallback powered byNLLanguageRecognizer.
§Availability
- The crate weak-links
Translation.framework, so the typedLanguage,LanguagePair,TranslationConfiguration,TranslationResponse, andLanguageRecognitionhelpers work on macOS 14+. LanguageAvailabilityrequires macOS 15+.- Manual
TranslationSessionconstruction is currently available on macOS 26+ throughTranslationSession(installedSource:target:); the crate surfaces those APIs and returns structuredTranslationErrorvalues on older systems.
§Async API
Enable the async feature to use executor-agnostic futures backed by Swift Task thunks and doom-fish-utils completion helpers:
translation-rs = { version = "0.3", features = ["async"] }The async surface currently includes:
AsyncTranslationSession::translateAsyncTranslationSession::translationsAsyncTranslationSession::prepare_translationAsyncLanguageAvailability::statusAsyncLanguageAvailability::supported_languages
See examples/10_async_translate.rs and examples/11_async_availability.rs for end-to-end usage with pollster::block_on.
§Examples
The crate ships numbered examples for every logical area plus an end-to-end framework smoke test:
01_language_roundtrip02_language_pair_roundtrip03_translation_error_messages04_language_availability_smoke05_translation_configuration_lifecycle06_translation_response_roundtrip07_language_recognition_smoke08_translation_session_smoke09_framework_smoke10_async_translate(requires--features async)11_async_availability(requires--features async)
Run the sync examples with:
for ex in 01_language_roundtrip 02_language_pair_roundtrip 03_translation_error_messages 04_language_availability_smoke 05_translation_configuration_lifecycle 06_translation_response_roundtrip 07_language_recognition_smoke 08_translation_session_smoke 09_framework_smoke; do
cargo run --example "$ex"
doneRun the async examples with:
cargo run --features async --example 10_async_translate
cargo run --features async --example 11_async_availability§Coverage
See COVERAGE.md for the API-by-API audit against the active macOS SDK.
§License
Licensed under either Apache-2.0 or MIT at your option.
Re-exports§
pub use language::Language;pub use language_availability::LanguageAvailability;pub use language_availability::LanguageAvailabilityStatus;pub use language_pair::LanguagePair;pub use language_recognition::detect_language;pub use language_recognition::recognize_language;pub use translation_attributes::SkipTranslationAttribute;pub use translation_attributes::SkipTranslationAttributeValue;pub use translation_attributes::TranslationAttributedRun;pub use translation_attributes::TranslationAttributedString;pub use translation_attributes::TranslationAttributes;pub use translation_attributes::TranslationAttributesDecodingConfiguration;pub use translation_attributes::TranslationAttributesEncodingConfiguration;pub use translation_configuration::TranslationConfiguration;pub use translation_error::TranslationError;pub use translation_response::TranslationResponse;pub use translation_session::TranslationBatchResponse;pub use translation_session::TranslationRequest;pub use translation_session::TranslationSession;pub use translation_session::TranslationSessionConfiguration;pub use translation_session::TranslationStrategy;
Modules§
- async_
api async - Async wrappers for Translation.framework operations.
Async API for
Translation - availability
- Availability APIs mirroring Translation.framework’s
LanguageAvailability. - detection
- Language recognition helpers used alongside Translation.framework.
- error
- Error re-exports for Translation.framework operations.
- ffi
- language
- Language identifier types used by Translation.framework.
- language_
availability LanguageAvailabilitybindings for Translation.framework.- language_
pair - Source/target language pair types for Translation.framework.
- language_
recognition - NaturalLanguage-backed detection helpers for Translation workflows.
- prelude
- Common Translation.framework wrapper types for glob imports.
- session
- Session re-exports for Translation.framework workflows.
- translation_
attributes - Translation attribute helpers for attributed text workflows.
- translation_
configuration - Mutable configuration helpers for Translation.framework sessions.
- translation_
error - Error types for Translation.framework operations.
- translation_
response - Response types returned by Translation.framework translations.
- translation_
session - Core
TranslationSessionwrappers for Translation.framework.