Skip to content

Commit dfba6bd

Browse files
committed
feat: add option to clear cache in @bothrs/translations
1 parent 271fe04 commit dfba6bd

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

packages/translations/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export function AirtableExample() {
3434
| -------------- | ------------------------------------------------------- |
3535
| expirationTime | Time between translation refreshes in ms |
3636
| loadPath | The endpoint from where the translations will be loaded |
37+
| storagePrefix | Prefix for the location where the cache is persisted |
3738

3839
### Airtable table structure
3940

@@ -69,7 +70,29 @@ export function GenericExample() {
6970
}
7071
```
7172

73+
## Refresh translations
74+
75+
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.
76+
77+
```tsx
78+
import { useTranslations } from '@bothrs/translations'
79+
import i18next from 'i18next'
80+
81+
export function GenericExample() {
82+
const ready = useTranslations({
83+
// Increment manually
84+
storagePrefix: 'i18n_3_',
85+
// Refresh on every app update
86+
storagePrefix: 'i18n_' + Constants.manifest?.version + '_',
87+
})
88+
return <div>{ready ? i18next.t('Ready') : 'Loading'}</div>
89+
}
90+
```
91+
92+
## All options
93+
7294
| Name | Explanation |
7395
| -------------- | ------------------------------------------------------- |
7496
| expirationTime | Time between translation refreshes in ms |
7597
| loadPath | The endpoint from where the translations will be loaded |
98+
| storagePrefix | Prefix for the location where the cache is persisted |

packages/translations/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function useTranslations(
3636
}
3737

3838
export function initTranslations({
39+
storagePrefix = 'i18n_',
3940
expirationTime,
4041
fetchOptions,
4142
...options
@@ -54,7 +55,7 @@ export function initTranslations({
5455
backend: {
5556
backends: [StorageBackend, MultiloadAdapter],
5657
backendOptions: [
57-
{ prefix: 'i18n_', expirationTime },
58+
{ prefix: storagePrefix, expirationTime },
5859
{ backend: Fetch, backendOption: fetchOptions },
5960
],
6061
},

packages/translations/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export interface TranslationRow {
3737
}
3838

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

0 commit comments

Comments
 (0)