# WebAppLocalizer
## Overview
WebAppLocalizer is a JavaScript class that simplifies the process of localizing web applications. It manages translations, persists language preferences, and provides an optional UI for language selection.
## Installation
Include the WebAppLocalizer script in your HTML file:
```html
```
## Usage
### Initialization
Create a new instance of WebAppLocalizer:
```javascript
const localizer = new WebAppLocalizer(translations, localStoragePrefix, languageSelector);
```
Parameters:
- `translations` (required): An object containing translations for different languages.
- `localStoragePrefix` (optional): A string prefix for localStorage keys. Default: 'webAppLocalizer_'.
- `languageSelector` (optional): A DOM element or its ID for the language selector UI.
### Translations Object Structure
```javascript
const translations = {
en: {
name: "English",
translations: {
"key1": "Translation 1",
"key2": "Translation 2"
}
},
fr: {
name: "Français",
translations: {
"key1": "Traduction 1",
"key2": "Traduction 2"
}
}
};
```
### Methods
#### setLanguage(lang)
Set the current language.
#### getCurrentLanguage()
Get the current language code.
#### getAvailableLanguages()
Get an array of available languages with their codes and names.
#### translate(key)
Get the translation for a specific key.
#### apply()
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