Instruction 2

The Translation API allows developers to add translation capabilities to various app components such as user-generated content, chat messages, or interface text. In contrast, the Translation App is standalone and provides translation services to end-users. It functions independently and is used for translating text, voice, and conversations directly within that app.

Using the Translation API, you can customize how translations are presented within your app. You can choose between different API configurations for simple overlay translations or inline translations for multiple text instances​. On the other hand, the Translation App only offers a predefined user interface and experience that developers can’t alter.

The Basics of Translation Framework

The Translation framework provides a built-in UI, which can pop up within your app for handling translations. This simplifies the translation flow by providing a native interface for users to translate text without needing to manually select languages or define translation options. Although, it lacks customization options.

To have a full control over the translation process, the Translation API offers TranslationSession. With TranslationSession, you can handle both batch and sequential translations easily, making it adaptable to different translation needs.

Below are some of the key methods you’ll use to add these capabilities to your app:

The translationPresentation API

translationPresentation API is a simple yet powerful API for translating a text displayed on screen using the system UI overlay. A person simply selects a text, and then a system UI displays along with that text to the view containing the selected text! The translationPresentation method provides a native look and feel that fits seamlessly into iOS apps. It eliminates the need for building custom translation UI components, which saves time and ensures consistency across apps.

The TranslationSession API

To gain more control over the translation experience, you can create your own in-app translation experiences using TranslationSession. It allows you to display your custom view to handle the translation.

  • This API allows you to handle batch translations (translating multiple text strings at once) using translations(from:).
  • If you want to translate multiple strings of text and display the results as they arrive, there’s a translate(batch:) function. You can combine both of them with a TranslationSession to handle concurrent translations.

The checkLanguageSupport API

This lets developers check whether the framework supports translation between a source and a target language before actually executing it. It then updates the display based on whether the translation pairing is supported. You can use this feature to proactively adjust the UI based on available languages. For instance, the app can enable or disable specific UI elements based on which languages are available, preventing users from encountering unsupported features.

The prepareTranslation API

Sometimes, the user’s preferred language isn’t locally available on the device for translation. In such cases, prepareTranslation allows the developer to download the language models before proceeding with the translation. It presents a view that requests permission to download the languages if they aren’t already downloaded. By preparing for translation, the app can check the user’s system language or other preferences and automatically adjust the source and target languages for translation. This personalization ensures that users receive translations in the language they are most comfortable with, without needing to manually set the language each time.

Framework Issues

While the iOS Translation APIs bring significant benefits for localizing your app, there are some potential drawbacks that you should be aware of. First, the built-in translation UI provided by the API, such as translationPresentation, while convenient, offers limited customization compared to fully custom-built interfaces.

Next, you may run into limited language support. Although the Translation API supports a wide range of languages, there may be certain niche languages or dialects that are not available. Additionally, since the translation models are stored locally, some devices may not have all languages pre-installed, requiring users to download language packs, which can be cumbersome for offline use or in regions with poor internet connectivity.

Along with limited language support, your users may experience language context issues. Although the Translation API uses machine learning models (via Core ML) for translations, it may not always fully grasp the context or nuance of the text. This could result in less accurate translations, especially for complex or idiomatic language. You may need to supplement the API with additional logic or external models for better contextual understanding.

The language models also don’t receive continuous updates. Since translations are processed locally on the device using pre-installed models, any improvements or updates to the machine learning models might require a system update or manual language pack download. This is different from cloud-based solutions where model updates are automatic and continuous, potentially resulting in outdated translation models for certain devices.

Finally, the translation experience is device-dependent. Because of this, users may experience inconsistencies in translation quality depending on the specific model available on their device. This could lead to variability in translation accuracy across different iPhone or iPad models, particularly between older and newer devices.

While the Translation APIs offer a convenient, privacy-focused, and efficient way to handle translations on-device, the trade-offs include limited UI customization, occasional performance issues on older devices, and lack of cloud-based features. Developers need to assess whether the benefits of real-time, offline translation outweigh the limitations, depending on their app’s target audience and use cases.

See forum comments
Download course materials from Github
Previous: Instruction 1 Next: Instruction 3