Repository navigation
feat(Poll): render poll translations in the reader's language - #3340
Conversation
With automatic translation enabled, the API returns a poll's question, options and answers translated, in name_i18n, text_i18n and answer_text_i18n. The default poll components now render the entry for client.user.language and fall back to the original text. - The question and options are translated in the message, the results and the full option list, and answers in the answers dialog. So is the question in the channel list preview and a quoted poll, and so is the option text in the channel list's vote preview. - A poll message starts in the translated view. MessageTranslationIndicator shows on it when the question or an option is translated, and "View original" switches the whole poll back along with any message text. - The indicator reads the live poll from client.polls, so translations that arrive after the first render show it without a reload. - New export: usePollTextTranslation(), for custom poll components that should follow the same translation view.
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the βοΈ Run configuration
You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Size Change: +2.78 kB (+0.25%) Total Size: 1.13 MB π¦ View Changed
βΉοΈ View Unchanged
|
Codecov Reportβ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## release-v14 #3340 +/- ##
===============================================
+ Coverage 85.89% 86.84% +0.95%
===============================================
Files 524 526 +2
Lines 16649 16699 +50
Branches 5229 5251 +22
===============================================
+ Hits 14301 14503 +202
+ Misses 2348 2196 -152 β View full report in Codecov by Harness. π New features to boost your workflow:
|
## π― Goal The backend now auto-translates polls, and `stream-chat` exposes the translations as `name_i18n` on the poll, `text_i18n` on options and `answer_text_i18n` on comments. They're already in our `^9.54.4`, so no bump is needed. Until now the SDK always showed the original text. With this PR, polls show in the user's language, the same way auto-translated message text already does. React counterpart: GetStream/stream-chat-react#3340. Ticket: https://linear.app/stream/issue/REACT-1344/support-automatic-poll-translations-in-the-react-sdk ## π Implementation details The user's language is `client.user.language`, the same as for messages. Any text without a translation falls back to the original. Translated: - the question in the poll header and in the all-options and results dialogs - option text in the message and in the results, including the vote button's accessibility label - comments in the answers list - the poll previews in the channel list, quoted replies and the thread list ("created: β¦", "voted: β¦") There's no "View original" toggle. v9 has none for messages either, so polls always show the translation, like messages do. Editing your own comment still prefills the original text, not the translation. API changes, all additive: - New `usePollTextTranslation()` hook. It returns `translate(original, i18n)`, for custom poll components. - `usePollState()` also returns `nameI18n`. The `<lang>_text` lookup lives in one internal helper, `getTranslatedText`, and `useTranslatedMessage` now uses it too. One edge case changes: a message whose `i18n` has the user's language key set to `undefined` now shows the original text instead of nothing. Offline DB: - The `poll` table gets `name_i18n` and `description_i18n` columns. Option and comment translations were already stored, inside the options and answers JSON. The description isn't displayed anywhere yet; it's stored so the poll round-trips intact. - Both columns are always written, as `null` when the poll has no translation. The upsert skips `undefined` values, so leaving them out would keep a stale translation after the question is edited. - `dbVersion` goes from 16 to 17, so the offline DB is rebuilt once after upgrade, as with earlier schema changes. The cache is fetched again, and offline actions that haven't synced yet are dropped. ## π¨ UI Changes The layout is unchanged; translated text replaces the original. ## π§ͺ Testing Unit tests cover: - translated, partially translated and untranslated polls, in the message and in the dialogs - a translation arriving through `poll.updated` - the channel list and message previews - offline storage: kept, replaced, cleared, and a poll that never had translations On the iOS simulator I checked the poll message, the results and comments dialogs, and the channel preview, including a real vote. The demo app doesn't produce poll translations, so I injected `poll.updated` events with translations into the running client. Not checked on Android, and not checked with offline mode on a device. To try it, you need an app with auto-translation enabled and a user whose `language` differs from the poll's. ## βοΈ Checklist - [ ] I have signed the [Stream CLA](https://docs.google.com/forms/d/e/1FAIpQLScFKsKkAJI7mhCr7K9rEIOpqIDThrWxuvxnwUq2XkHyG154vQ/viewform) (required) - [x] PR targets the `develop` branch - [ ] Documentation is updated - [ ] New code is tested in main example apps, including all possible scenarios - [ ] SampleApp iOS and Android - [ ] Expo iOS and Android
π― Goal
The v14 counterpart of (
master), for the stable 14.x line.The Chat API now translates polls automatically (question, options and free-form
answers) on the shards where it's deployed, and
stream-chat9.54.0 exposes thetranslated fields (GetStream/stream-chat-js#1878). Until now the React SDK always
showed the original text. With this change, readers see a poll in their own
language, the same way auto-translated message text already works, with "View
original" covering the poll too.
Ticket:
π Implementation details
The same behaviour as the v15 PR, adapted to v14's conventions.
Data.
name_i18n, an option'stext_i18nand an answer'sanswer_text_i18nare v9's
TranslatedField, the same shape asmessage.i18n:<language>_textentries plus
language, the source language. The reader's language isclient.user.language. Anything without a translation falls back to theoriginal.
Translated UI.
PollHeader(title) andPollOptionSelector(options in the message)PollResults,PollOptionWithVotesHeaderandPollOptionsFullList(thequestion and options in the dialogs)
PollAnswerList(answers in the comments dialog)QuotedMessagePreview(a quoted poll's question)Shared logic.
Poll/utils/translations.ts(internal, not exported) is the only code thatknows the key format. It has
getPollTextTranslation,pollHasTranslation(question and options only; answers are only visible inside the comments dialog)
and
getPollSourceLanguage. The lookup casts the key the same way v14'sgetTranslatedMessageTextdoes.usePollTextTranslation()(new public export, throughPoll/hooks) returnstranslate(original, i18n). It follows the message's translation view. It readsuseMessageContext(), which in v14 returns an empty value outside a message, sothe poll parts gain no new requirement.
Translation view and indicator.
Messagestarts a poll message (poll_id) in the translated view. That's safebecause a text without a translation shows the original. The decision doesn't
use
message.poll, which is a snapshot that poll events never update.MessageTranslationIndicatornow shows on poll messages, including messagesthat are only a poll. It subscribes to the live poll (
client.pollsviauseChatContext,useStateStore), so translations that arrive later throughpoll.updatedshow it without a reload. "View original" switches the whole pollback along with any message text. v14's
language/<code>name lookup isunchanged.
i18n. No new
t()strings, so the translation catalog is unchanged.Dependency note. An edited question, or a language added later with
translateMessage, needs the stream-chat-js v9 fix in . Until that patch is required, the question keeps its old translation untila reload (option translations already update live). A follow-up will raise the
stream-chatminimum from^9.54.0once that patch is published.Testing
Poll/__tests__/PollTranslation.test.tsx(header, options and everydialog, translated, original view and fallback, with
axe),Poll/utils/__tests__/translations.test.tsandMessage/__tests__/MessageTranslationIndicator.test.tsx(poll-only message,toggle, no translation,
poll.updatedafter the first render; v14 had noindicator test).
Message.test.tsx(default view),QuotedMessage.test.tsx(quotedpoll) and
ChannelListItem/__tests__/utils.test.ts(created and voted previews).examples/viteagainst a test app with auto-translation on, with users and channels in en, es, de and it, running with the fixed stream-chat-js v9 build. Checklist - [x]yarn lintpassed (includesvalidate-translations) - [x]yarn testpassed (3086 tests)yarn typespassedyarn buildpassedusePollTextTranslation)