https://github.com/jerboa88/tinyi18n
A tiny JavaScript library for internationalization and easy switching of languages on HTML pages
https://github.com/jerboa88/tinyi18n
html i18n i18n-js internationalization javascript javascript-library js l10n localization translation
Last synced: about 2 months ago
JSON representation
A tiny JavaScript library for internationalization and easy switching of languages on HTML pages
- Host: GitHub
- URL: https://github.com/jerboa88/tinyi18n
- Owner: jerboa88
- License: mpl-2.0
- Created: 2019-06-26T03:28:17.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-07-29T01:23:59.000Z (9 months ago)
- Last Synced: 2025-03-15T00:35:10.997Z (about 2 months ago)
- Topics: html, i18n, i18n-js, internationalization, javascript, javascript-library, js, l10n, localization, translation
- Language: JavaScript
- Homepage: https://johng.io/p/tinyi18n
- Size: 16.6 KB
- Stars: 7
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
tinyi18n
A tiny JavaScript library for internationalization and easy switching of languages on HTML pages
> [!NOTE]
> This is an experimental project at the moment. Use at your own risk## Usage
1. Include `tinyi18n.js` before the closing body tag in your page and call `tinyi18n.loadTranslations('translations.json')` with the name of your JSON file to load it. Put the `data-translatekey` attribute on any block of text you want to have multiple languages. The value of this attribute is essentially an id for the block of text so you can write translations for it. You can use the same value for multiple blocks of text. For non-text elements, also add the `data-translateattribute` to specify which attribute you want to have translations for.##### index.html
```html
tinyi18n.loadTranslations('translations.json')
```
2. Create your translations as shown below with multiple languages listed for each key. Make sure to include `default_language` and `languages` for the module to work properly.
##### translations.json
```json
{
"default_language": "es",
"languages": ["en", "es"],
"translations": {
"title": {
"en": "Title in English",
"es": "Título en Inglés"
},
"description": {
"en": "I like cats",
"es": "Me gustan los gatos"
},
"searchbox": {
"en": "search",
"es": "buscar"
}
}
}
```
##### index.html
3. Call `tinyi18n.translate` with the language you want to switch to and voila!```html
Translate to English
Traducir al inglés
tinyi18n.loadTranslations('translations.json')
```
## Functions
- `loadTranslations('translations.json')`: Loads JSON file with translations. Takes a filename as input
- `getLang()`: Get the current language of the page according to tinyi18n
- `setLang('en')`: Changes the page language to a specified language. Takes a language string as input## Attributes
- `data-translatekey="title"`: Use this on every text element you want to translate. Choose a different key for every element and add the translations for your keys in your JSON file
- `data-translateattribute="value"`: Use this on non-text elements where you need to have translated text for an attribute value (ex. value attribute for input elements)## Contributing
Contributions, issues, and forks are welcome but this is a hobby project so don't expect too much from it. [SemVer](http://semver.org/) is used for versioning.## License
This project is licensed under the Mozilla Public License 2.0. See [LICENSE](LICENSE.md) for details.