Upgraded localizer

This commit is contained in:
Saifeddine ALOUI 2024-09-17 21:18:18 +02:00
parent 538d22d750
commit 082b81e27d
3 changed files with 22 additions and 1 deletions

View File

@ -36,9 +36,15 @@ const localizer = new WebAppLocalizer(translations, localStoragePrefix, language
```
## HTML Usage
If your translation is plain text, then use:
```html
<element data-translate="key"></element>
```
If your translation contains html, then add data-translate-html entry:
```html
<element data-translate="key" data-translate-html></element>
```
Apply translations: `localizer.apply();`
Get the translation of a prompt with options:

View File

@ -63,6 +63,15 @@ Apply translations to all elements with the `data-translate` attribute.
### HTML Usage
Add the `data-translate` attribute to elements you want to localize:
If your translation is plain text, then use:
```html
<element data-translate="key"></element>
```
If your translation contains html, then add data-translate-html entry:
```html
<element data-translate="key" data-translate-html></element>
```
```html
<h1 data-translate="welcome-message"></h1>

View File

@ -56,7 +56,13 @@ class WebAppLocalizer {
const elements = document.querySelectorAll('[data-translate]');
elements.forEach(element => {
const key = element.getAttribute('data-translate');
const useHTML = element.hasAttribute('data-translate-html');
if (useHTML) {
element.innerHTML = this.translate(key);
} else {
element.textContent = this.translate(key);
}
});
}