https://github.com/gobwas/lexicon.js
https://github.com/gobwas/lexicon.js
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/gobwas/lexicon.js
- Owner: gobwas
- Created: 2014-04-22T10:09:54.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2014-11-12T10:58:46.000Z (over 10 years ago)
- Last Synced: 2025-02-24T14:43:02.516Z (2 months ago)
- Language: JavaScript
- Size: 168 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# Lexicon.[js](https://developer.mozilla.org/en/docs/JavaScript)
> Fast and simple storage of your phrases.
## Getting started
+ Install with [npm](https://npmjs.org/package/lexicon-js);
+ Download [zip](https://github.com/gobwas/lexicon.js/archive/master.zip).## Example
Lexicon.js brings you ability to resolve message in your vocabulary tree by path. If there no key of path chain, Lexicon tries
to resolve the default one.```javascript
var vocabulary, lexicon;vocabulary = {
greetings: {
hi: "Hi, wassup?"
hello: "Hello, my dear <%= friend %>!",
goodbye: "Goodbye, my dear!"
_: "Some greetings to you!"
}
};lexicon = new Lexicon();
console.log(lexicon.getMessage(vocabulary, ["greetings", "hi"])); // Hi, wassup?
console.log(lexicon.getMessage(vocabulary, ["greetings", "hello"], { data: {friend: "Alf"} })); // Hello, my dear Alf!
console.log(lexicon.getMessage(vocabulary, ["greetings", "not-existing-key"])); // Some greetings to you!
```## Options
By default there are few reserved keys:
```javascript
Lexicon.DEFAULTS = {
key: {
_: '_',
info: 'info',
error: 'error'
}
};
```But you can redefine them by given options object. For example:
```javascript
var lexicon = new Lexicon({ key: { _: "_default" } });
```Lexicon uses underscore template notation. So if you want to render message dynamically, just declare your message with underscore
template syntax, and then pass options to the #getMessage method:```javascript
lexicon.getMessage(vocabulary, ["greetings", "hello"], { data: { friend: "Alf" } });
```Enjoy =)