Skip to content

Commit 6f82bcd

Browse files
feat: clear translation cache (#39)
* feat: clear translation cache * feat: selective language clear cache * chore: ts docs * chore: readme update * chore: appropriate variable names
1 parent aceb800 commit 6f82bcd

File tree

4 files changed

+76
-5
lines changed

4 files changed

+76
-5
lines changed

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,32 @@ window.resetTranslation(
393393
394394
---
395395

396+
### 5. 🗑️ Clear Cache with `window.clearCache()`
397+
398+
Clear translation cache from localStorage. You can clear cache for all languages or selectively for specific languages:
399+
400+
#### Clear All Languages Cache
401+
402+
```js
403+
window.clearCache([], () => {
404+
console.log('All translation cache cleared successfully');
405+
}, (err) => {
406+
console.error('Error clearing cache:', err);
407+
}); // Clears all translation cache
408+
```
409+
410+
#### Clear Specific Languages Cache
411+
412+
```js
413+
window.clearCache(['es', 'fr', 'de'], () => {
414+
console.log('Cache cleared for Spanish, French, and German');
415+
}, (err) => {
416+
console.error('Error clearing cache:', err);
417+
}); // Clear cache for specific languages only
418+
```
419+
420+
----------
421+
396422
### ✅ Tips:
397423

398424
- Both `translate()` and `resetTranslation()` can be used to build your own custom language selector.
@@ -408,7 +434,7 @@ The widget determines which language to display using the following priority ord
408434
| 2 | User preference (selected language) | Language the user selects in the widget |
409435
| 3 | `pageLanguage` (default page language) | The default language set for the page |
410436

411-
### 5. Font Size Adjustment
437+
### 6. Font Size Adjustment
412438

413439
The translation widget automatically adjusts font sizes when translating text to prevent overflow issues. This is particularly useful when translating to languages that typically have longer text lengths. The font size adjustment works as follows:
414440

@@ -421,8 +447,10 @@ The translation widget automatically adjusts font sizes when translating text to
421447
- Original font sizes are preserved and restored when resetting translations
422448

423449
The font size adjustment is automatic and requires no additional configuration. It helps maintain readability while preventing text overflow in translated content.
450+
451+
452+
### 7. 🚀 Faster and More Accurate than Google Translate
424453

425-
### 6. 🚀 Faster and More Accurate than Google Translate
426454

427455
Our engine offers **contextual accuracy** and **lower latency**, especially for dynamic content.
428456

index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ <h2>Customer Stories</h2>
107107
</p>
108108
</article>
109109
</section>
110+
111+
<button onclick="window.translate('hi', (res)=>{
112+
console.log(res);
113+
}, (err)=>{
114+
console.log(err);
115+
})" class="tw-button notranslate">Translate to hindi</button>
116+
117+
<button onclick="window.resetTranslation('en', (res)=>{
118+
console.log(res);
119+
})">Reset Translio</button>
120+
<button onclick="window.clearCache([])" class="button button-secondary">Clear Cache</button>
121+
122+
110123
</main>
111124
<script defer src="http://localhost:5173/dist/index.min.js"></script>
112125
<script defer type="module">

src/lib/storage/localstorage.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,19 @@ export class LocalStorageWrapper {
108108
delete this.cache[key]; // Invalidate the cache for this key
109109
}
110110

111-
clear(): void {
111+
clear(lang_code: string[] = []): void {
112112
if (this.prefix) {
113113
for (let key in localStorage) {
114-
if (key.startsWith(this.prefix)) {
114+
if (
115+
key.startsWith(this.prefix) &&
116+
(!lang_code.length || lang_code.includes(key.split("--")[1]))
117+
) {
118+
localStorage.removeItem(key);
119+
}
120+
}
121+
} else if (lang_code && lang_code.length > 0) {
122+
for (let key in localStorage) {
123+
if (lang_code.includes(key.split("--")[1])) {
115124
localStorage.removeItem(key);
116125
delete this.cache[key];
117126
}

src/widget/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,7 @@ declare global {
11301130
onError?: (error: Error) => void
11311131
) => void;
11321132
translate: (langCode: string, onComplete?: (result: TranslationResult) => void, onError?: (error: Error) => void) => Promise<TranslationResult>;
1133+
clearCache: (lang_code:string[], onComplete?: () => void, onError?: (error: Error) => void) => void;
11331134
}
11341135
}
11351136

@@ -1139,7 +1140,7 @@ if (typeof window !== "undefined") {
11391140
* @param defaultLang The default language to reset to
11401141
* @param onComplete Callback function that will be called when the translation is complete
11411142
* @param onError Callback function that will be called if the translation fails
1142-
* @returns A promise that resolves to the translation result
1143+
* @returns void
11431144
*/
11441145
window.resetTranslation = (
11451146
defaultLang: string,
@@ -1199,4 +1200,24 @@ if (typeof window !== "undefined") {
11991200
};
12001201
}
12011202
};
1203+
1204+
/**
1205+
* @param langArr Array of language codes to clear cache for. Empty array clears all languages.
1206+
* @param onComplete Callback function that will be called when the translation is complete
1207+
* @param onError Callback function that will be called if the translation fails
1208+
* @returns void
1209+
*/
1210+
window.clearCache = (
1211+
lang_code: string[] = [],
1212+
onComplete?: () => void,
1213+
onError?: (error: Error) => void
1214+
) => {
1215+
const localStorageWrapper = new LocalStorageWrapper(CACHE_PREFIX);
1216+
try {
1217+
localStorageWrapper.clear(lang_code);
1218+
onComplete?.();
1219+
} catch (error) {
1220+
onError?.(error as Error);
1221+
}
1222+
};
12021223
}

0 commit comments

Comments
 (0)