MLKitTranslate Framework Reference

  • MLKTranslator is an Objective-C class that provides on-device translation capabilities.

  • It offers methods to translate text, download necessary model files, and manage download conditions.

  • The translatorWithOptions: method is used to obtain an instance of the translator with specified options.

  • translateText:completion: translates the given text from the source language to the target language.

  • downloadModelIfNeededWithCompletion: or downloadModelIfNeededWithConditions:completion: can be used to download the required language model files if they are not already available on the device.

MLKTranslator


@interface MLKTranslator : NSObject

A class that translates the given input text.

  • Gets a Translator instance for the specified options. This method is thread safe.

    Declaration

    Objective-C

    + (nonnull MLKTranslator *)translatorWithOptions:
        (nonnull MLKTranslatorOptions *)options;

    Parameters

    options

    The options for the translator.

    Return Value

    A Translator instance that provides translation with the given options.

  • Translates the given text from the source language into the target language.

    This method will return an error if the model files have not been downloaded.

    Declaration

    Objective-C

    - (void)translateText:(nonnull NSString *)text
               completion:(nonnull MLKTranslatorCallback)completion;

    Parameters

    text

    A string in the source language.

    completion

    Handler to call back on the main queue with the translation result or error.

  • Downloads the model files required for translation, if they are not already downloaded.

    Declaration

    Objective-C

    - (void)downloadModelIfNeededWithCompletion:
        (nonnull MLKTranslatorDownloadModelIfNeededCallback)completion;

    Parameters

    completion

    Handler to call back on the main queue with an error, if any.

  • Downloads the model files required for translation when the given conditions are met. If model has already been downloaded, completes without additional work.

    Declaration

    Objective-C

    - (void)
        downloadModelIfNeededWithConditions:
            (nonnull MLKModelDownloadConditions *)conditions
                                 completion:
                                     (nonnull
                                          MLKTranslatorDownloadModelIfNeededCallback)
                                         completion;

    Parameters

    conditions

    The downloading conditions for the translate model.

    completion

    Handler to call back on the main queue with an error, if any.

  • Unavailable.

    Declaration

    Objective-C

    - (nonnull instancetype)init;