Permalink
Browse files

refactor done

  • Loading branch information...
1 parent b7765d4 commit 9f60ec8359bd42073f8bbc925eb7d2423323e2ac @masylum committed Mar 12, 2011
View
@@ -1,6 +1,14 @@
NODE = node
-test: test_dialect
+test: test_dialect test_helpers test_stores
test_dialect:
@$(NODE) test/dialect.js
+
+test_helpers:
+ @$(NODE) test/io.js
+ @$(NODE) test/sqlizer.js
+
+test_stores:
+ @$(NODE) test/stores/mongodb.js
+ @$(NODE) test/stores/sqlite.js
View
@@ -8,15 +8,10 @@
`Wbmd"MML..JMML.`Moo9^Yo..JMML.`Mbmmd' YMbmd' `Mbmo
-Dialect is a painless nodejs module that deals with i18n, and L10n.
-
-This module may contain traces of bugs.
+Dialect is the painless nodejs module that deals with i18n.
## Install
-Currently dialect just supports MongoDB, so, you need to install it or fork
-your own storage solution .
-
npm install dialect
## Philosphy
@@ -28,31 +23,17 @@ your own storage solution .
## Example
- var dialect = require('dialect').dialect({
- path:'./dictionaries',
- base_locale: 'en',
- });
-
- // change our locale to Spanish
- dialect.config('locale', 'es');
-
- // Configs and open the storage connection
- require('dialect').store(
- {store: 'mongodb', database: 'translations'},
- function (error, store) {
- dialect.config('store', store);
- dialect.getTranslation('Hello World!');
- }
- );
-
-## How does it work?
+ var dialect = require('dialect'),
+ d = dialect.dialect({current_locale: 'es', store: 'mongodb'}, function (err, store) {
+ d.get('Hello World!'); // => Hola mundo
+ }).reCache();
-Dialect stores automatically all the strings pending to be translated.
-Once your translate the strings, they will be cached on a JSON file
-for every machine that needs the translations. The JSON will be loaded
-to memory, so you will acces the translations from memory.
+## API
-Dialect supports counts and contexts.
+* `config`:
+* `get`:
+* `set`:
+* `reCache`:
### Usings counts
@@ -62,7 +43,7 @@ You need to provide an array with the singular, plural and
the number.
[1, 2, 3].forEach(function (i) {
- dialect.getTranslation([
+ dialect.get([
'Hello World',
'Hello Worlds',
{count: i}
@@ -81,7 +62,7 @@ about a sentence. It helps the translator and it may generate
diferent translations depending on the context.
['female', 'male'].forEach(function (gender) {
- dialect.getTranslation([
+ dialect.get([
'My friends',
gender
]);
@@ -98,7 +79,7 @@ meanings although they can be used with interpolations.
[1, 2].forEach(function (count) {
['female', 'male'].forEach(function (gender) {
- dialect.getTranslation([
+ dialect.get([
'You have {count} friend called {name}',
'You have {count} friends called {name}',
{count: count, context: context, name: 'Anna'}
@@ -112,9 +93,9 @@ meanings although they can be used with interpolations.
### Store translations
-To store a new translation, use the method setTranslation.
+To store a new translation, use the method `set`.
- dialect.setTranslation(
+ dialect.set(
{original: 'I love gazpacho', locale: 'es'},
'Me encanta el gazpacho',
function () {
@@ -131,9 +112,9 @@ Try [express-dialect](http://www.github.com/masylum/express-dialect)
## Test
-Dialect is heavily tested using a mix of Vows and node asserts module.
+Dialect is heavily tested using [testosterone](http://www.github.com/masylum/testosterone)
- make test
+ make
## Benchmarks
View
@@ -1,17 +1,11 @@
-var fs = require('fs'),
- http = require('http'),
- dialect = require('./..').dialect({
- path: __dirname + '/data',
- base_locale: 'en_us',
- current_locale: 'es_es',
- locales: ['en_en', 'es_es']
- });
-
-dialect.config('locales').forEach(function (locale) {
- fs.writeFileSync(dialect.config('path') + locale + '.js', '{}', 'utf8');
-});
-
+// benchmark
require('./..').store({store: 'mongodb', database: 'bench'}, function (error, store) {
+ var dialect = require('./..').dialect({
+ base_locale: 'en_us',
+ current_locale: 'es_es',
+ locales: ['en_en', 'es_es'],
+ store: store
+ });
store.collection.remove({}, function (err, data) {
@@ -27,22 +21,22 @@ require('./..').store({store: 'mongodb', database: 'bench'}, function (error, st
output = original[0],
now = Date.now();
- dialect.config('store', store);
-
- dialect.getTranslation(original, function () {
- dialect.setTranslation({
+ dialect.get(original, function () {
+ dialect.set({
original: original[0],
locale: dialect.config('current_locale'),
context: 'female',
count: 'singular'
}, translation, function () {
console.log('running...');
- for(;i < tests; i += 1) {
- dialect.getTranslation(original);
+
+ for (;i < tests; i += 1) {
+ dialect.get(original);
}
+
var time = Date.now() - now;
console.log(time + 'ms');
- console.log(time/tests + 'ms per request');
+ console.log(time / tests + 'ms per request');
});
});
});
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,2 +1 @@
module.exports.dialect = require('./lib/dialect');
-module.exports.store = require('./lib/store');
View
@@ -2,15 +2,11 @@ if (global.GENTLY) {
require = global.GENTLY.hijack(require);
}
-var fs = require('fs'),
- funk = require('funk');
-
module.exports = function (options) {
var DIALECT = {},
_io = require('./helpers/io').IO(DIALECT),
_options = options || {},
- _current_language = null,
_parse = function (str, params) {
var matches = /(\{(.*?)\})+/g.exec(str);
@@ -63,8 +59,7 @@ module.exports = function (options) {
current_dictionary = DIALECT.dictionaries[_options.current_locale],
params = Array.isArray(original) ? original.pop() : {},
index = Array.isArray(original) ? original[0] : original,
- pluralize = require('./helpers/plurals')(_options.base_locale),
- i = 0;
+ pluralize = require('./helpers/plurals')(_options.base_locale);
if ((typeof original !== 'string' && !Array.isArray(original)) || original.length === 0) {
throw Error("Original is not valid");
@@ -87,8 +82,7 @@ module.exports = function (options) {
if (typeof translation !== 'string') {
_options.store.add(
{original: index, locale: _options.current_locale},
- translation,
- function () { }
+ translation
);
return _parse(Array.isArray(original) ? original[pluralize(params.count)] : original, params);
} else {
@@ -117,7 +111,7 @@ module.exports = function (options) {
}
// Database
- _options.store.set(query, translation, cb);
+ _options.store.set(query, {translation: translation}, cb);
return DIALECT;
};
View
@@ -2,9 +2,7 @@ if (global.GENTLY) {
require = global.GENTLY.hijack(require);
}
-var fs = require('fs'),
- plurals = require('./plurals'),
- funk = require('funk');
+var plurals = require('./plurals');
module.exports.IO = function (DIALECT) {
var IO = {},
@@ -28,7 +26,11 @@ module.exports.IO = function (DIALECT) {
IO.getKeyFromQuery = function (query) {
var key = query.original;
- if (query.count) {
+ if (!key) {
+ throw Error("Original must be provided");
+ }
+
+ if (query.count !== undefined) {
key += '|p:' + _getPluralForm(query.count);
}
@@ -43,13 +45,17 @@ module.exports.IO = function (DIALECT) {
* Caches the dictionaries from the Store to the JSON files
*
* @param {String} locale
- * Target dictionary we want to cache, if not set use config('locales')
+ * Target dictionary we want to cache
* @param {Function} cb
* Callback when its done with async
* @returns IO
*/
IO.cacheDictionary = function (locale, cb) {
+ if (!locale) {
+ throw Error("You must provide a locale");
+ }
+
DIALECT.config('store').get({locale: locale}, function (err, data) {
var dictionary = {},
key = null,
@@ -61,16 +67,16 @@ module.exports.IO = function (DIALECT) {
for (i in data) {
key = IO.getKeyFromQuery({
original: data[i].original,
- count: data[i].count,
+ count: data[i].plural,
context: data[i].context
});
- dictionary[data[key].original] = data[i].translation;
+ dictionary[key] = data[i].translation;
}
// loads the dictionary to memory
DIALECT.dictionaries[locale] = dictionary;
- cb(err, data);
+ cb(err, dictionary);
}
});
Oops, something went wrong.

0 comments on commit 9f60ec8

Please sign in to comment.