Add README.md for lib/translation (#10005)

This commit is contained in:
DS Boyce
2026-03-05 10:53:25 -08:00
committed by GitHub
parent 8b8acb7bd1
commit fe8e8ce7de
+59
View File
@@ -0,0 +1,59 @@
# Translation
A hook for translating text on-device for inline display.
## Translating text
```tsx
const langPrefs = useLanguagePrefs()
const {translate} = useTranslate({key: post.uri})
// ...
void translate({
text: record.text,
targetLangCode: langPrefs.primaryLanguage,
})
```
## Clearing/hiding a translation
```tsx
const {clearTranslation} = useTranslate({key: post.uri})
// ...
clearTranslation()
```
## Rendering a translation
```tsx
const {translationState} = useTranslate({key: post.uri})
// ...
switch (translationState.status) {
case 'idle':
// Default state; render a link that calls `translate`.
break;
case 'loading':
// On-device translation is in progress; render a loading spinner.
break;
case 'success':
// Translation complete; render `translationState.translatedText` and a link
// that calls `clearTranslation`.
break;
case 'error':
// On-device translation failed; render `translationState.message` and a
// link to `translate` from `useGoogleTranslate` as a fallback.
break;
}
```
## Notes
* Android only supports two-letter language codes.
* For example, this means it doesnt differentiate between `pt-BR` and `pt-PT`.
* Android and iOS only support a subset of the language options we offer (iOS supports fewer than Android).
* Individual language packs must be downloaded on iOS.