Islyngten
Islyngten
is a tiny JavaScript library supplying I18N translation support for Node.js and the browser.
It provides simple, yet powerful solutions for multiple plural forms and interpolation.
Islyngten
has no dependencies.
Features
- Context support
- Simple plural form
- Multiple plural forms
- Interpolation
Getting started
Node.js
Install islyngten
using npm.
npm install islyngten --save
Then require it into any module.
const Islyngten = ;const i18n = ;
Browser
Islyngten
has no dependencies, which makes it easy to include in a browser.
You can download the latest release from the repository
islyngten.js
unminified, including commentsislyngten.min.js
minified version
Use a script tag to directly add Islyngten
to the global scope.
Usage
const i18n = ; // Prepare i18n by adding translation resources for different languages.// Note: The first parameter to `resources()` serves as a `context`. // This allows you to structure your translation tables to your needs.// You are free to choose whatever string you like.i18n; i18n; // Translation is not necessary for locale 'en', so original english text will be supplied.var rb = i18n;var translation = rb; // -> returns "Do you really want to delete the file test.txt?" // German translation will be supplied for locale 'de'.rb = i18n;translation = rb; // -> returns "Wollen Sie die Datei test.txt wirklich löschen?" // French translation will be supplied for locale 'fr'.rb = i18n;translation = rb; // -> returns "Voulez-vous vraiment supprimer le fichier test.txt?"
Simple example
const i18n = ; // Prepare i18n by adding translation resources for different languages.i18n; i18n; // To access the translations, get a resource bundle for the desired language ...var rb = i18n; // ... and request the text.rb; // -> returns "Hund"rb; // -> returns "Hund"rb; // -> returns "Hund" // Note: t() and _() are aliases for the get() method. // Select a different language.var rbfr = i18n; rbfr; // -> returns "chien" // If no translation was found the original text is returned.rbfr; // -> returns "elephant"
More examples
Simple plural
const i18n = ; i18n; var rb = i18n; // Note: tt() and __() are aliases for the nget() method.var fileCount = 1;var translation = rb;// translation -> "Selektierte Datei entfernen?" fileCount = 4;translation = rb;// translation -> "Selektierte Dateien entfernen?"
Simple plural with variables (interpolation)
const i18n = ; i18n; var rb = i18n; // Note: tt() and __() are aliases for the nget() method.var count = 0;var translation = rb;// translation -> "Die Liste enthält 0 Einträge" count = 1;translation = rb;// translation -> "Die Liste enthält 1 Eintrag" count = 12;translation = rb;// translation -> "Die Liste enthält 12 Einträge
Multiple plural forms
The previous examle showed an easy way to support simple plural forms by using the nget()
[__()
, tt()
] methods.
But what, if you need to translate in other languages where multiple plural forms exist?
For example, in Polish, the word "file" pluralises like this:
- 1 plik
- 2,3,4 pliki
- 5-21 plików
- 22-24 pliki
- 25-31 plików
(More details here)
const i18n = ; i18n; const rb = i18n; var translation = rb;// translation -> "plik" translation = rb;// translation -> "pliki" translation = rb;// translation -> "pliki" translation = rb;// translation -> "pliki" translation = rb;// translation -> "plików" translation = rb;// translation -> "plików" translation = rb;// translation -> "plików" translation = rb;// translation -> "pliki" translation = rb;// translation -> "plików"
Interpolation
const i18n = ; i18n; const rb = i18n; // There are no specification on how to name your variables. Choose whatever format you like. const translation = rb; // translation -> "Höhe muss innerhalb von 120 und 250 cm liegen."
Even more examples
Please refer to the test spec for more examples.
Testing
We use
- JSHint for static code analysis.
- Jasmine testing framework for testing.
- Karma test runner for testing in the browser.
- Istanbul test coverage framework for tracking test coverage.
Steps to be taken
- Clone or download the repository.
- Change into the project directory.
- Use
npm install
to install all development dependencies. - Use
npm runt lint
to run static code analysis. - Use
npm test
to run the tests. - Use
npm run coverage
to track test coverage. - The output should display successful execution results and a code coverage map.
Build
- Clone or download the repository.
- Change into project directory.
- Use
npm run build
in project directory to buildislyngten.min.js
fromislyngten.js
.
Contribution
Please use Github issues for requests.
Pull requests are welcome.
Issues
We use GitHub issues to track bugs. Please ensure your bug description is clear and has sufficient instructions to be able to reproduce the issue.
The absolute best way to report a bug is to submit a pull request including a new failing test which describes the bug. When the bug is fixed, your pull request can then be merged.
The next best way to report a bug is to provide a reduced test case on jsFiddle or jsBin or produce exact code inline in the issue which will reproduce the bug.
Support
- Send us an email: support@belexos.com
- Follow us on Twitter: @belexos
Changelog
v1.1.0
- Update npm modules.
- Update and extend test environment.
- Add static code analysis tool JSHint.
- Add Karma test runner.
- Fix JSHint issues.
- Replace uglify-js by terser for minification.
- Update README.
v1.0.3
- Update npm modules.
v1.0.2
- Update npm modules.
v1.0.1
- Update npm modules.
v1.0.0
- Initial public release
License
Copyright (c) 2013-present, Belexos. Islyngten
is licensed under the MIT License.