blip-cards 3.56.0 → 3.57.0-translation.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +101 -0
- package/dist/blip-cards.css +1 -1
- package/dist/blip-cards.js +1 -1
- package/package.json +11 -1
package/README.md
CHANGED
|
@@ -112,6 +112,107 @@ Is possible to group your cards using any rule you want. For that use the <blip-
|
|
|
112
112
|
| readonly | Currently being used only in the quick reply. Could be implemented in other components too. This is used to avoid click actions in the quick reply options. If this attribute is true, than the <em>editable</em> and <em>deletable</em> should be false (OPTIONAL) | Boolean |
|
|
113
113
|
| hide-options | Used only only in the quick reply. This is used to hide the quick reply options (OPTIONAL) | Boolean |
|
|
114
114
|
| disable-link | Used only in the plain text. This is used to do not render tag 'a' (OPTIONAL) | Boolean |
|
|
115
|
+
| translation | Renders host-provided translations in the bubbles, with a toggle on the timestamp row. See [Message translation](#message-translation) (OPTIONAL) | Object |
|
|
116
|
+
|
|
117
|
+
## Message translation
|
|
118
|
+
`<blip-group-card>` and `<blip-card>` accept an optional `translation` prop that renders translated content inside the bubble and a small translation control below it, on the same row as the message time:
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
10:12 · [icon] Ver original em espanhol (received message, left aligned)
|
|
122
|
+
10:17 · [icon] Ver envio em espanhol (sent message, right aligned)
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
The library never translates anything by itself: the host owns the translation requests and the cache, and the cards only render what `getTranslation` returns.
|
|
126
|
+
|
|
127
|
+
When the prop is absent or `enabled` is not `true`, the rendered markup is identical to the version without this feature (no extra elements, classes, comment nodes or callbacks).
|
|
128
|
+
|
|
129
|
+
```html
|
|
130
|
+
<blip-group-card :documents="documents" :translations="translations" :translation="translation" />
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
```js
|
|
134
|
+
translation: {
|
|
135
|
+
enabled: true,
|
|
136
|
+
// Global default. Each bubble may override it locally through the toggle
|
|
137
|
+
// (requesting a translation through "Traduzir" also shows it once it is done);
|
|
138
|
+
// changing this value resets every local override.
|
|
139
|
+
showOriginal: false,
|
|
140
|
+
// Must be synchronous and read reactive state (e.g. Vuex getters) so the cards re-render on updates.
|
|
141
|
+
getTranslation: (fullDocument) => ({
|
|
142
|
+
status: 'done', // 'pending' | 'done' | 'error'
|
|
143
|
+
sourceLanguage: 'espanhol', // customer language, human-readable and already in the case to display
|
|
144
|
+
targetLanguage: 'português', // attendant language, human-readable and already in the case to display
|
|
145
|
+
text: 'Olá, tudo bem?', // optional
|
|
146
|
+
title: undefined, // optional
|
|
147
|
+
options: undefined, // optional, string[]
|
|
148
|
+
direction: 'received' // optional, 'received' | 'sent'
|
|
149
|
+
}), // or null when there is no translation for the message
|
|
150
|
+
onTranslate: (fullDocument) => {}, // optional, may return a Promise; called by the "Traduzir"/"Tentar novamente" actions
|
|
151
|
+
canTranslate: (fullDocument) => true // optional, defaults to `position === 'left'` (received messages)
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
`fullDocument` is the original message envelope (`id`, `type`, `content`, ...).
|
|
156
|
+
|
|
157
|
+
| Field returned by `getTranslation` | Replaces (while the translation is shown) |
|
|
158
|
+
|---|---|
|
|
159
|
+
| `text` | Plain text (`text/plain`) content, select header text, media caption (image and file `text`) and the text of a received reply (`application/vnd.lime.reply+json` with a `text/plain` replied value) |
|
|
160
|
+
| `title` | Image title |
|
|
161
|
+
| `options[i]` | Displayed label of the select option `i`. Display only: the payload sent when an option is selected is always the original one |
|
|
162
|
+
|
|
163
|
+
**Direction.** `direction` from `getTranslation` when it is `'received'` or `'sent'`; otherwise `fullDocument.direction` when it is one of these values; otherwise `'sent'` for `position === 'right'` and `'received'` for any other position.
|
|
164
|
+
|
|
165
|
+
- **Received** messages: `text` is the customer message translated to the attendant language. The bubble shows it by default and the toggle reads "Ver original em {sourceLanguage}"; showing the original switches it to "Ver em {targetLanguage}".
|
|
166
|
+
- **Sent** messages: `text` is the attendant's original text (before it was translated and sent). The bubble shows it by default and the toggle reads "Ver envio em {sourceLanguage}"; showing the content actually sent (`document`) switches it to "Ver em {targetLanguage}". Sent messages are only evaluated when `canTranslate` is provided (the default only allows `position === 'left'`).
|
|
167
|
+
|
|
168
|
+
Toggle states (rendered after the time, or after the group time for the last bubble of a `<blip-group-card>` group):
|
|
169
|
+
|
|
170
|
+
| State | When | Toggle |
|
|
171
|
+
|---|---|---|
|
|
172
|
+
| none | prop absent, `enabled !== true`, `canTranslate` false, card being edited, `getTranslation` throws, or no translation and no `onTranslate` | not rendered |
|
|
173
|
+
| translate | no translation (or `done` without content) and `onTranslate` provided | "Traduzir" |
|
|
174
|
+
| pending | `status: 'pending'` | "Traduzindo..." as plain text (the button stays focusable with `aria-disabled="true"`) |
|
|
175
|
+
| error | `status: 'error'` | "Não foi possível traduzir · Tentar novamente" (retry only with `onTranslate`) |
|
|
176
|
+
| done | `status: 'done'` with a non-empty `text`, `title` or option | "Ver original em {language}" / "Ver envio em {language}" / "Ver em {language}" (see Direction); "Ver original" / "Ver envio" / "Ver tradução" when the language is missing |
|
|
177
|
+
|
|
178
|
+
The control becomes an `aria-live="polite"` region right after it is mounted, so bubbles rendered on load are not announced but later state changes (e.g. "Traduzindo..." → "Ver original em espanhol") are. The icon is `aria-hidden`.
|
|
179
|
+
|
|
180
|
+
Placement:
|
|
181
|
+
- Standalone cards render the control in their own time row (`10:12 · ...`). Without `date`, the control is rendered without the leading `·`.
|
|
182
|
+
- In `<blip-group-card>`, only the last bubble of a group has a time row (the group one), so its control is rendered there; the other bubbles of the group render the control on a row of their own below the bubble, without the time. A message is matched to its group row by `fullDocument.id`; messages without `id`, or whose `id` is repeated in `documents`, always use their own row.
|
|
183
|
+
- List menus (non-`immediate` selects) have no time row and always use their own row.
|
|
184
|
+
- The link color defaults to `#006e78`, a teal darker than the design one (`#00838F`) so that small text keeps a WCAG AA contrast of at least 4.5:1 on white (6.0:1) and on light-gray chat backgrounds such as `#ededed` (5.1:1) or `#e7e7e7` (4.9:1). Hosts can override it with the `--blip-cards-translation-link-color` CSS custom property; check the contrast against their own background when doing so.
|
|
185
|
+
|
|
186
|
+
Supported cards: `text/plain` (the `#blip.payload.text` metadata variant is not translated), `application/vnd.lime.select+json`, `application/vnd.blip.rich-select+json`, image and file `application/vnd.lime.media-link+json` and text replies. Other cards ignore the prop.
|
|
187
|
+
|
|
188
|
+
### Labels
|
|
189
|
+
Labels come from the existing `translations` prop under `messageTranslation`. Missing keys fall back to the pt-BR defaults below. `{language}` is replaced by the language name exactly as returned by the host (the library never changes its case, so pass e.g. "espanhol" for pt-BR sentences). When the language is missing or empty, the matching `*NoLanguage` label is used instead (if a host sets it to an empty string, the `{language}` placeholder is dropped from the regular label). Hosts that translate the regular labels should also translate the `*NoLanguage` ones, otherwise the pt-BR defaults are shown.
|
|
190
|
+
|
|
191
|
+
```js
|
|
192
|
+
translations: {
|
|
193
|
+
messageTranslation: {
|
|
194
|
+
viewOriginal: 'Ver original em {language}', // received, translation shown; {language} = sourceLanguage
|
|
195
|
+
viewTranslation: 'Ver em {language}', // original shown; {language} = targetLanguage
|
|
196
|
+
viewSent: 'Ver envio em {language}', // sent, attendant text shown; {language} = sourceLanguage
|
|
197
|
+
viewOriginalNoLanguage: 'Ver original', // viewOriginal without sourceLanguage
|
|
198
|
+
viewTranslationNoLanguage: 'Ver tradução', // viewTranslation without targetLanguage
|
|
199
|
+
viewSentNoLanguage: 'Ver envio', // viewSent without sourceLanguage
|
|
200
|
+
translate: 'Traduzir',
|
|
201
|
+
translating: 'Traduzindo...',
|
|
202
|
+
failed: 'Não foi possível traduzir',
|
|
203
|
+
retry: 'Tentar novamente'
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Rules for hosts
|
|
209
|
+
- **Do not swap the message content.** Never replace `document.content` (or any field of the envelope) with the translated text. Pass the original messages in `documents` and provide the translation only through `getTranslation`. Reply, edit and selection payloads always use the original document.
|
|
210
|
+
- `getTranslation` must be cheap and side-effect free: it runs on every render of a supported card. Trigger requests only from `onTranslate`.
|
|
211
|
+
- Report failures through `getTranslation` (`status: 'error'`). Errors thrown or promises rejected by `onTranslate` are swallowed by the cards.
|
|
212
|
+
|
|
213
|
+
### Security notes
|
|
214
|
+
- Translated strings are treated as untrusted input and go through the same sanitization pipeline as the original field of each card. Footer labels and languages are rendered with text interpolation only (never `v-html`).
|
|
215
|
+
- Translating sends message content to the host's translation provider. The host is responsible for consent, data-protection rules and for not sending content that must not leave the platform.
|
|
115
216
|
|
|
116
217
|
## Native Form Card
|
|
117
218
|
A structured form (several fields answered in a single interaction, optionally split in steps) is rendered when the bot sends an `application/vnd.blip.form+json` message.
|