Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
Core overhauled.
100% tested.
API simplified.
Removed JSON layer.
Memory cache only reindex on syncing.
Improved storages.
Added more examples.
  • Loading branch information
masylum committed Mar 19, 2011
1 parent cadeaec commit 71caac8
Show file tree
Hide file tree
Showing 16 changed files with 617 additions and 1,024 deletions.
57 changes: 47 additions & 10 deletions Readme.md
Expand Up @@ -8,7 +8,7 @@
`Wbmd"MML..JMML.`Moo9^Yo..JMML.`Mbmmd' YMbmd' `Mbmo


Dialect is the painless nodejs module that deals with i18n.
Dialect is a painless nodejs module to manage your translations.

## Install

Expand All @@ -17,24 +17,45 @@ Dialect is the painless nodejs module that deals with i18n.
## Philosphy

* Scalable: The translations should be available to any number of machines.
* Fast: Getting translations from memory if possible.
* Fast: Getting translations from memory.
* Reliable: Translations should be always available on a central repository/database.
* Flexible: You should be able to use your favorite storage solution.

## Example

var dialect = require('dialect').dialect({current_locale: 'es', store: 'mongodb'});

dialect.sync({interval:3600}, function (err, foo) {
d.get('Hello World!'); // => Hola mundo
// connects to the store
dialect.connect(function () {

// syncs the memory dictionaries with the store
dialect.sync({interval:3600}, function (err, foo) {
d.get('Hello World!'); // => Hola mundo
});
});

## Options

* `current_locale`: Current locale used on your application.
* `base_locale`: Base locale. Serves as keys on the dictionaries.
* `locales`: Which locales are available on your application.
* `store`: Object containing the store and their options
* `mongodb`: mongoDB storage.
* database
* host
* port
* collection
* `sqlite`: SQLite storage.
* database
* table

## API

* `config (key, value)`: Exposes configuration values.
* `get (query)`: Gets a translation cached in memory.
* `set (query, translation, callback)`: Sets a translation on the store.
* `sync (locale, repeat, callback)`: Syncs the store with the memory cache.
* `connect (callback)`: Connects to the database store.

### Plurals

Expand All @@ -56,6 +77,8 @@ which plural form to use.
| Piva | Beers |
+---------------+-------------+

You have an examle using plural forms in `examples/plurals.js`


### Contexts

Expand All @@ -76,6 +99,7 @@ diferent translations depending on the context.
| Mis amigas | My friends |
+---------------+-------------+

You have an examle using contexts in `examples/contexts.js`

### String interpolation

Expand All @@ -102,6 +126,8 @@ meaning although they can also be used with interpolations.
| Tienes 2 amigos que se llaman Anna | You have 2 friends called Anna |
+---------------------------------------+-----------------------------------------+

You have an examle using contexts in `examples/interpolation.js`

### Store translations

To store a new translation, use the method `set`.
Expand All @@ -111,12 +137,11 @@ To store a new translation, use the method `set`.
'Me encanta el gazpacho'
);

## express-dialect
## dialect-http

Do you have an express application and you to deal with i18n? Do you want to see
how dialect works in a real app?
Do you need a nice environment for your translators?

Try [express-dialect](http://www.github.com/masylum/express-dialect)
Dialect http is an amazing web application to manage your translations.

## Test

Expand All @@ -129,6 +154,18 @@ Dialect is heavily tested using [testosterone](http://www.github.com/masylum/tes
Dialect should not add an overhead to your application on getting translations.
Please run/add benchmarks to ensure that this module performance rocks.

node benchmakrs/app.js
node benchmakrs/hello_world.js


## License

(The MIT License)

Copyright (c) 2010-2011 Pau Ramon <masylum@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Have fun!
43 changes: 0 additions & 43 deletions benchmarks/app.js

This file was deleted.

48 changes: 48 additions & 0 deletions benchmarks/hello_world.js
@@ -0,0 +1,48 @@
var dialect = require('..').dialect({
locales: ['es', 'en'],
current_locale: 'es',
store: {mongodb: {}} // test your store here
}),
funk = require('funk')(),
funk2 = require('funk')(),
times = 300,
_ = dialect.get,
original = 'Hello World!',
translation = 'Hola Mundo!';

dialect.connect(function () {
var i = 0,
now = Date.now(),
time = null;

console.log('Setting ' + times + ' translations...');

for (i = 0; i < times; i ++) {
_(original);
dialect.set({original: original, locale: 'es'}, translation, funk.nothing());

//dialect.sync({}, function (err, foo) {
//_(original);
//_('Inexistant');
//});
}

funk.parallel(function () {
time = Date.now() - now;
console.log(time + 'ms');
console.log(parseInt(1000 / ( time / times)) + ' sets/sec');
});

dialect.sync({}, function (err, foo) {
now = Date.now();
time = null;

console.log('Getting ' + times + ' translations...');
for (i = 0; i < times; i ++) {
_(original);
}
time = Date.now() - now;
console.log(time + 'ms');
console.log(parseInt(1000 / (time / times)) + ' gets/sec');
});
});
20 changes: 20 additions & 0 deletions examples/contexts.js
@@ -0,0 +1,20 @@
var dialect = require('..').dialect({
locales: ['es', 'en'],
current_locale: 'es',
store: {mongodb: {}}
}),
_ = dialect.get;

dialect.connect(function () {
console.log(_(['Fight', {context: 'name'}]));
console.log(_(['Fight', {context: 'verb'}]));

dialect.set({original: 'Fight', locale: 'es', context: 'name'}, 'Lucha');
dialect.set({original: 'Fight', locale: 'es', context: 'verb'}, 'Luchar');

dialect.sync({interval: 3600}, function (err, foo) {
console.log(_(['Fight', {context: 'name'}]));
console.log(_(['Fight', {context: 'verb'}]));
process.exit();
});
});
14 changes: 8 additions & 6 deletions examples/hello_world.js
Expand Up @@ -7,11 +7,13 @@ var dialect = require('..').dialect({
original = 'Hello World!',
translation = 'Hola Mundo!';

console.log(_(original));
dialect.set({original: original, locale: 'es'}, translation);

dialect.sync({interval: 3600}, function (err, foo) {
dialect.connect(function () {
console.log(_(original));
console.log(_('Inexistant'));
process.exit();
dialect.set({original: original, locale: 'es'}, translation);

dialect.sync({interval: 3600}, function (err, foo) {
console.log(_(original));
console.log(_('Inexistant'));
process.exit();
});
});
23 changes: 12 additions & 11 deletions examples/plurals.js
Expand Up @@ -5,18 +5,19 @@ var dialect = require('..').dialect({
}),
_ = dialect.get;

[1, 2, 3].forEach(function (i) {
console.log(_(['{count} Beer', '{count} Beers', {count: i}]));
});

dialect.set({original: '{count} Beer', locale: 'es', plural: 1}, '{count} pivo');
dialect.set({original: '{count} Beer', locale: 'es', plural: 2}, '{count} pivi');
dialect.set({original: '{count} Beer', locale: 'es', plural: 3}, '{count} piva');

dialect.sync({interval: 3600}, function (err, foo) {
dialect.connect(function () {
[1, 2, 3].forEach(function (i) {
console.log(_(['{count} Beer', '{count} Beers', {count: i}]));
});
process.exit();
});

dialect.set({original: '{count} Beer', locale: 'es', plural: 1}, '{count} pivo');
dialect.set({original: '{count} Beer', locale: 'es', plural: 2}, '{count} pivi');
dialect.set({original: '{count} Beer', locale: 'es', plural: 3}, '{count} piva');

dialect.sync({interval: 3600}, function (err, foo) {
[1, 2, 3].forEach(function (i) {
console.log(_(['{count} Beer', '{count} Beers', {count: i}]));
});
process.exit();
});
});

0 comments on commit 71caac8

Please sign in to comment.