Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions packages/translations/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function AirtableExample() {
| -------------- | ------------------------------------------------------- |
| expirationTime | Time between translation refreshes in ms |
| loadPath | The endpoint from where the translations will be loaded |
| storagePrefix | Prefix for the location where the cache is persisted |

### Airtable table structure

Expand Down Expand Up @@ -69,7 +70,29 @@ export function GenericExample() {
}
```

## Refresh translations

If translations change commonly, consider updating the `storagePrefix` to make sure all users are using the latest translations. This prefix acts as a cache-busting mechanism.

```tsx
import { useTranslations } from '@bothrs/translations'
import i18next from 'i18next'

export function GenericExample() {
const ready = useTranslations({
// Increment manually
storagePrefix: 'i18n_3_',
// Refresh on every app update
storagePrefix: 'i18n_' + Constants.manifest?.version + '_',
})
return <div>{ready ? i18next.t('Ready') : 'Loading'}</div>
}
```

## All options

| Name | Explanation |
| -------------- | ------------------------------------------------------- |
| expirationTime | Time between translation refreshes in ms |
| loadPath | The endpoint from where the translations will be loaded |
| storagePrefix | Prefix for the location where the cache is persisted |
3 changes: 2 additions & 1 deletion packages/translations/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export function useTranslations(
}

export function initTranslations({
storagePrefix = 'i18n_',
expirationTime,
fetchOptions,
...options
Expand All @@ -54,7 +55,7 @@ export function initTranslations({
backend: {
backends: [StorageBackend, MultiloadAdapter],
backendOptions: [
{ prefix: 'i18n_', expirationTime },
{ prefix: storagePrefix, expirationTime },
{ backend: Fetch, backendOption: fetchOptions },
],
},
Expand Down
2 changes: 2 additions & 0 deletions packages/translations/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface TranslationRow {
}

export interface TranslationInitParameters extends InitOptions {
/** allows to bust the cache in a declarative way, warning: previous cached versions are kept in storage forever */
storagePrefix?: string
/** expirationTime time between between revalidation intervals, defaults to 1 week */
expirationTime?: number
/** Configuration for 'i18next-fetch-backend' */
Expand Down