Translator API: on-device translation made easy
By Chrome for Developers
Key Concepts
- Translator API: A browser-based tool for performing private, on-device language translation.
- Language Pack: The downloadable data required for the API to perform translations between specific language pairs.
- Monitor Callback: A function used to track the progress of background downloads (e.g., for UI progress bars).
- Translate Streaming: A method for translating long-form text sentence-by-sentence.
- Language Detector API: A utility to identify the source language of a text automatically.
- Abort Signal: A mechanism to cancel ongoing translation requests.
Implementation Process: Using the Translator API
The video outlines a technical workflow for integrating on-device translation into web applications, specifically focusing on an English-to-Chinese use case.
1. Checking Language Availability
Before initiating a translation, developers must verify if the specific language pair is supported by the browser. This is done by calling the translator.availability method. The response will fall into one of three states:
readily: The language pair is immediately supported.downloadable: The translation is supported, but the language pack must be downloaded first.after-download: The language pack is currently in the process of downloading.- Unsupported: The browser does not support the requested pair.
2. Initializing the Translator
Once the status is confirmed as downloadable, the developer initializes the translator object using the create method, passing the desired language pair (e.g., English to Chinese) as an argument.
3. Handling Downloads and Progress Tracking
Because language packs are not always pre-installed, the API allows for a monitor callback. This function is invoked whenever a download progress event is fired. This is critical for user experience, as it allows developers to display real-time progress bars while the necessary data is fetched.
4. Executing Translation
After the initialization and download phases are complete, the translate method is called on the translator object to perform the actual text conversion. The video emphasizes that this process is "lightning fast" because it occurs locally on the device rather than via a remote server.
Advanced Features and Best Practices
The video suggests several additional capabilities for developers looking to expand their implementation:
- Translate Streaming: For longer texts, developers should utilize the
translate streamingmethod, which processes text sentence-by-sentence rather than waiting for the entire block to be translated. - Abort Signal: To optimize performance and resource management, the
abort signaloption can be used to cancel translations that are no longer needed (e.g., if a user navigates away from a page). - Language Detector API: In scenarios involving user-generated content where the source language is unknown, the Language Detector API can be used to identify the input language before passing it to the translator.
Conclusion
The Translator API provides a robust, private, and efficient solution for developers to integrate multilingual support directly into web applications. By leveraging on-device processing, developers can ensure user privacy and high performance without the need for external API keys or server-side dependencies. The workflow—checking availability, managing downloads via callbacks, and executing translations—offers a streamlined framework for building localized user experiences.
Chat with this Video
AI-PoweredLoad the transcript when you're ready to chat so the initial page stays lighter.