astro-static-dict
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -91,19 +91,27 @@ /****************************************************************************** | ||
}); | ||
const handler = (event) => __awaiter(void 0, void 0, void 0, function* () { | ||
const handler = (newLanguage) => __awaiter(void 0, void 0, void 0, function* () { | ||
try { | ||
changeLanguageOnPage({ | ||
newDict: yield getDictionary(event.detail), | ||
newDict: yield getDictionary(newLanguage), | ||
keySeparator, | ||
keySuffix | ||
}); | ||
window.selectedLanguage = event.detail; | ||
window.selectedLanguage = newLanguage; | ||
} | ||
catch (error) { | ||
console.error(`Dictionary for language ${event.detail} not found`); | ||
console.error(`Dictionary for language ${newLanguage} not found`); | ||
} | ||
}); | ||
window.changeLanguage = (newLanguage) => window.dispatchEvent(new CustomEvent('changeLanguage', { detail: newLanguage })); | ||
window.addEventListener('changeLanguage', handler); | ||
window.addEventListener('changeLanguage', event => handler(event.detail)); | ||
document.addEventListener('astro:after-swap', () => { | ||
if (defaultLanguage === window.selectedLanguage) { | ||
return; | ||
} | ||
const selectedLanguage = window.selectedLanguage; | ||
window.selectedLanguage = defaultLanguage; | ||
handler(selectedLanguage); | ||
}); | ||
}; | ||
export { watchLanguageChange }; |
{ | ||
"author": "brentlok", | ||
"name": "astro-static-dict", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"main": "index.js", | ||
@@ -6,0 +6,0 @@ "repository": "https://github.com/Brentlok/astro-static-dict", |
@@ -88,8 +88,12 @@ # Astro static dict | ||
```astro | ||
<script> | ||
import { watchLanguageChange } from 'astro-static-dict/client' | ||
```ts | ||
import { watchLanguageChange } from 'astro-static-dict/client' | ||
import { enUs } from 'lib/locale' | ||
watchLanguageChange() | ||
</script> | ||
watchLanguageChange({ | ||
cachedDictionaries: { | ||
enUs | ||
}, | ||
defaultLanguage: 'enUs' | ||
}) | ||
``` | ||
@@ -99,8 +103,10 @@ | ||
#### watchLanguageChange config (optional) | ||
#### watchLanguageChange config | ||
| Property | Type | Description | | ||
|-----------------|----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------| | ||
| keySeparator | string | Default: `@@@` <br> Must be the same as initDictionary's keySeparator | | ||
| keySuffix | string | Default: `!!!` <br> Must be the same as initDictionary's keySuffix | | ||
| Property | Type | Description | | ||
|--------------------|------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------| | ||
| cachedDictionaries | Record<string, DictionaryBranch> | Dictionaries that will be injected into window.cachedDictionaries, you must include dictionary used in build process but more is up to you | | ||
| defaultLanguage | string | <br> Name of default language used on a page. Must be the same as dictionary used in build process | | ||
| keySeparator | string | Default: `@@@` <br> Must be the same as initDictionary's keySeparator | | ||
| keySuffix | string | Default: `!!!` <br> Must be the same as initDictionary's keySuffix | | ||
@@ -116,1 +122,11 @@ #### Trigger language change | ||
``` | ||
### Window modified properties | ||
```ts | ||
interface Window { | ||
changeLanguage(newLanguage: string): void, | ||
selectedLanguage: string, | ||
cachedDictionaries: Partial<Record<string, DictionaryBranch>> | ||
} | ||
``` |
Sorry, the diff of this file is too big to display
451263
8064
130