Keep the Original in View: Reliable Translation for a Tencent RTC Project-Critique Community
Building a project showcase is easier than it used to be. Building a community where people can understand and critique that work across languages is still difficult. That distinction matters. A polished chat demo may translate one sample message correctly, but a real community must answer harder questions: What happens when translation fails? Can a late result overwrite a newer message? Does the…
Creating a community where members can critique a project in various languages presents unique challenges. While a polished chat demo might accurately translate a single message, a real-world community must navigate more complex scenarios. This article explores those difficulties and presents a solution using Tencent RTC Social Messaging and TUIChat's on-demand message translation.
Key challenges include handling translation failures, ensuring users retain access to the original author's words, offering control over unsupported content, and providing a user-friendly way to report source messages instead of altered renderings. The primary engineering goal is not merely creating another chat screen but preserving authorship and user control, even as asynchronous features succeed, fail, or race against each other.
To tackle these challenges, we will build an application-state for a multilingual portfolio and project-critique community using Tencent RTC Social Messaging and TUIChat's translation feature. Tencent RTC's Social Messaging solution covers group discussions, large communities, rich media, and interest-based social experiences. For translation-specific implementation, refer to TUIChat's message translation documentation, which outlines supported content types, languages, and edition limits.
It's crucial to check these current constraints for your target platform rather than assuming the constraints from this article.
When a community member reviews a developer's project, the author posts a text description in a group. A reader selects "Translate" and chooses a target language. The UI displays the progress without hiding the source message. The translated text appears as a secondary view, allowing the reader to reveal the original, retry a failure, or report the source message. If the message changes while translation is running, the old result is discarded.
The core principle is that a translation is a reader-specific rendering of a message, never a replacement for the original message. This distinction leads to a practical state model and a set of defined behaviors for various translation states:
1. Idle state: The original message and "Translate" control are visible, allowing the user to initiate translation.
2. Request translation state: The UI shows a loading progress indicator, keeping the original message visible while the translation process runs.
3. Ready state: The original message is displayed alongside a labeled translation, which can be revealed or changed.
4. Failed state: The original message remains visible, accompanied by an explanatory error message. The user can choose to retry or continue with the original message.
5. Unsupported state: The original message is shown, along with an explanatory message indicating that the content cannot be translated. No retry is possible until the content or capabilities change.
A crucial aspect of this solution is that there is no state in which the original message disappears. This ensures that users always have access to the original author's words, preserving the integrity of the content being shared and critiqued.
To demonstrate this state model, we have created a reproducible TypeScript project that tests race conditions without requiring a network connection or production community. The project includes a `translation.ts` file with the following types and interfaces:
- `CommunityMessage`: Represents a message in the community, including its unique ID, revision number, author ID, type (text, image, file, or custom), and optional text content.
- `TranslationView`: Defines the various states a translated message can be in, including idle, loading, ready, failed, and unsupported.
- `FailureReason`: Enumerates possible reasons for translation failures, such as temporary unavailability, invalid responses, or lack of entitlement.
- `TranslationInput`: Represents the input required for initiating a translation, including the message ID, revision number, source text, and target language.
- `TranslationGateway`: An interface that defines a custom "translate" method for interacting with the translation service.
- `TranslationPolicy`: Encapsulates the policy governing translation behavior, including whether translation is enabled and which target languages are supported.
- `cacheKey()`: A function that generates a unique cache key based on the message ID, revision number, and target language.
- `TranslationCoordinator`: The main class responsible for managing the translation state, handling state transitions, and coordinating with the translation gateway.
The `TranslationCoordinator` class maintains a `Map` of `translationViews` keyed by a cache key generated from the message ID and target language. It also keeps track of the latest revision number for each message. The constructor takes a `TranslationGateway` instance and a `TranslationPolicy` object. The `sourceChanged()` method is used to update the translation state when the source message changes.
By following this approach, developers can build a robust multilingual community that preserves authorship and user control, even in the face of translation challenges.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.