diff --git a/example/deferred.js b/example/deferred.js deleted file mode 100644 index a41bde3..0000000 --- a/example/deferred.js +++ /dev/null @@ -1,56 +0,0 @@ -/* - * # locale library usage examples: deferred translation - * - * ## locale.js: i18n for Node.js and browser - * - * @author Dmitry A. Chleck - * @url https://github.com/chleck/locale-js - */ - -http = require('http'); - -// Import and init the library -var locale = require('..'); -locale.init('./i18n'); -var i18n = new locale.i18n(null); - -var __ = i18n.__; - -// Start HTTP server on 127.0.0.1:8080 -http.createServer(function (req, res) { - // Make JSON with i18n fields - var json = { - 'title': __('Hello!'), - 'msg': __(['The following error occurred during processing:', 'The following errors occurred during processing:'], 1), - 'error': [ __('Hello!'), __('Email %s is invalid. Please enter valid email.', '%#$@gmail.com') ], - 'session': 'jsahfkjsdfsdhiudfshiuh' - }; - // and submit it to HTTP client - res.writeHead(200, {'Content-Type': 'application/json'}); - res.end(JSON.stringify(json, null, ' ')); -}).listen(8080, '127.0.0.1'); - -// Get JSON from HTTP server -http.get({ host:'127.0.0.1', port:8080, path:'/', agent:false }, function (res) -{ - var data = ''; - res.setEncoding('utf8'); - res.on('data', function (chunk) { - data += chunk; - }); - res.on('end', function(){ - var json - , i18n = new locale.i18n(''); - // Translate to base language (English) - json = JSON.parse(data); - i18n.tr(json); - console.log('\nEnglish:\n', JSON.stringify(json, null, ' ')); - // Translate to Russian - json = JSON.parse(data); - i18n.to('ru'); - i18n.tr(json); - console.log('\nRussian:\n', JSON.stringify(json, null, ' ')); - // Exit - process.exit(0); - }); -}); diff --git a/example/i18n/i18n.json b/example/i18n/i18n.json deleted file mode 100644 index 63a08c8..0000000 --- a/example/i18n/i18n.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "base": "en", - "rule": "(n == 1 ? 0 : 1)", - "targets": [ - "ru" - ] -} \ No newline at end of file diff --git a/example/i18n/ru.json b/example/i18n/ru.json deleted file mode 100644 index b34db3a..0000000 --- a/example/i18n/ru.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "": "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)", - "Hello!": "Привет!", - "The following error occurred during processing:": [ - "Во время обработки возникла следующая ошибка:", - "Во время обработки возникли следующие ошибки:", - "Во время обработки возникли следующие ошибки:" - ], - "Email %s is invalid. Please enter valid email.": "Недопустимый email %s. Пожалуйста, введите правильный email.", - "Hello!#another_hello": "Здаров!", - "Hello!#another_hello2": "Алло!", - "There is ## in this phrase but it is not comment.": "В этой строке есть #, но это не комментарий.", - "Hello?": "", - "This is %% percent symbol.": "Это %% знак процента.", - "My %(0)s is faster then your %(1)s!": "Мой %(0)s быстрее, чем твой %(1)s!", - "Let's count in English: %s, %s, %(3)s and %s.": "Давайте считать по-английски: %s, %s, %(3)s и %s.", - "Inbox: %n unreaded message.": [ - "Входящие: %n непрочитанное сообщение.", - "Входящие: %n непрочитанных сообщения.", - "Входящие: %n непрочитанных сообщений." - ], - "%n developer from our team uses %(0)s with %(1)s.": [ - "%n разработчик из нашей команды использует %(0)s с %(1)s.", - "%n разработчика из нашей команды используют %(0)s с %(1)s.", - "%n разработчиков из нашей команды используют %(0)s с %(1)s." - ] -} \ No newline at end of file diff --git a/example/i18n/ru.jsonp b/example/i18n/ru.jsonp deleted file mode 100644 index 21b98b1..0000000 --- a/example/i18n/ru.jsonp +++ /dev/null @@ -1,28 +0,0 @@ -i18n_ru = -{ - "": "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)", - "Hello!": "Привет!", - "The following error occurred during processing:": [ - "Во время обработки возникла следующая ошибка:", - "Во время обработки возникли следующие ошибки:", - "Во время обработки возникли следующие ошибки:" - ], - "Email %s is invalid. Please enter valid email.": "Недопустимый email %s. Пожалуйста, введите правильный email.", - "Hello!#another_hello": "Здаров!", - "Hello!#another_hello2": "Алло!", - "There is ## in this phrase but it is not comment.": "В этой строке есть #, но это не комментарий.", - "Hello?": "", - "This is %% percent symbol.": "Это %% знак процента.", - "My %(0)s is faster then your %(1)s!": "Мой %(0)s быстрее, чем твой %(1)s!", - "Let's count in English: %s, %s, %(3)s and %s.": "Давайте считать по-английски: %s, %s, %(3)s и %s.", - "Inbox: %n unreaded message.": [ - "Входящие: %n непрочитанное сообщение.", - "Входящие: %n непрочитанных сообщения.", - "Входящие: %n непрочитанных сообщений." - ], - "%n developer from our team uses %(0)s with %(1)s.": [ - "%n разработчик из нашей команды использует %(0)s с %(1)s.", - "%n разработчика из нашей команды используют %(0)s с %(1)s.", - "%n разработчиков из нашей команды используют %(0)s с %(1)s." - ] -} \ No newline at end of file diff --git a/example/i18n/ru.tr b/example/i18n/ru.tr deleted file mode 100644 index 1d7bc21..0000000 --- a/example/i18n/ru.tr +++ /dev/null @@ -1,88 +0,0 @@ -@ [1] deferred.js (24:15) -@ [2] deferred.js (26:17) -@ [3] translate.js (37:6) -@ [4] translate.js (39:6) -S Hello! -# [4] This is comment. -? Hello! -! Привет! - -@ [1] deferred.js (25:13) -P The following error occurred during processing: -? The following error occurred during processing: -? The following errors occurred during processing: -! Во время обработки возникла следующая ошибка: -! Во время обработки возникли следующие ошибки: -! Во время обработки возникли следующие ошибки: - -@ [1] deferred.js (26:31) -S Email %s is invalid. Please enter valid email. -? Email %s is invalid. Please enter valid email. -! Недопустимый email %s. Пожалуйста, введите правильный email. - -@ [1] translate.js (41:6) -S Hello!#another_hello -? Hello! -! Здаров! - -@ [1] translate.js (43:6) -S Hello!#another_hello2 -# [1] Please translate this another way. -? Hello! -! Алло! - -@ [1] translate.js (45:6) -S There is ## in this phrase but it is not comment. -? There is ## in this phrase but it is not comment. -! В этой строке есть #, но это не комментарий. - -@ [1] translate.js (47:6) -S Hello? -? Hello? -! - -@ [1] translate.js (49:6) -S This is %% percent symbol. -? This is %% percent symbol. -! Это %% знак процента. - -@ [1] translate.js (51:6) -@ [2] translate.js (53:6) -@ [3] translate.js (55:6) -S My %(0)s is faster then your %(1)s! -? My %(0)s is faster then your %(1)s! -! Мой %(0)s быстрее, чем твой %(1)s! - -@ [1] translate.js (57:6) -S Let's count in English: %s, %s, %(3)s and %s. -? Let's count in English: %s, %s, %(3)s and %s. -! Давайте считать по-английски: %s, %s, %(3)s и %s. - -@ [1] translate.js (59:6) -@ [2] translate.js (60:6) -@ [3] translate.js (61:6) -P Inbox: %n unreaded message. -? Inbox: %n unreaded message. -? Inbox: %n unreaded messages. -! Входящие: %n непрочитанное сообщение. -! Входящие: %n непрочитанных сообщения. -! Входящие: %n непрочитанных сообщений. - -@ [1] translate.js (63:6) -@ [2] translate.js (68:6) -@ [3] translate.js (73:6) -P %n developer from our team uses %(0)s with %(1)s. -# [1] Comment 1 -# [2] Comment 3 -# [3] Multiline -# comment -? %n developer from our team uses %(0)s with %(1)s. -# [1] Comment 2 -# [2] Comment 4 -# [3] Another -# multiline -# comment -? %n developers from our team uses %(0)s with %(1)s. -! %n разработчик из нашей команды использует %(0)s с %(1)s. -! %n разработчика из нашей команды используют %(0)s с %(1)s. -! %n разработчиков из нашей команды используют %(0)s с %(1)s. diff --git a/example/i18n/tr.json b/example/i18n/tr.json deleted file mode 100644 index ae7af05..0000000 --- a/example/i18n/tr.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "base": "en", - "n": 2, - "rule": "(n == 1 ? 0 : 1)", - "targets": { - "ru": { - "n": 3, - "rule": "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)" - } - }, - "filter": [ - ".js" - ] -} \ No newline at end of file diff --git a/example/index.html b/example/index.html deleted file mode 100644 index 0a4ee19..0000000 --- a/example/index.html +++ /dev/null @@ -1,91 +0,0 @@ - - - - Locale library usage examples. - - - - - - - diff --git a/example/translate.js b/example/translate.js deleted file mode 100644 index 1b78c60..0000000 --- a/example/translate.js +++ /dev/null @@ -1,84 +0,0 @@ -/* - * # locale library usage examples - * - * ## locale.js: i18n for Node.js and browser - * - * @author Dmitry A. Chleck - * @url https://github.com/chleck/locale-js - */ - -// Import and init the library -var locale = require('..'); -locale.init('./i18n'); - -w('# LOCALE LIBRARY DEMO:'); - -// Create i18n for base language (English) -var i18n = new locale.i18n(''); - -// Pull __() to locals -__ = i18n.__; - -w('## English:'); -demo(); - -// Switch to Russian -i18n.to('ru'); - -w('## Russian:'); -demo(); - -// THE END - -// !!! Usage of translation functions -function demo() { - // Translate phrase - w(__('Hello!')); - // Comment for translator - w(__('Hello!# This is comment.')); - // Phrase with id - w(__('Hello!#another_hello')); - // Phrase with id and comment - w(__('Hello!#another_hello2 Please translate this another way.')); - // Phrase with # but not comment - w(__('There is ## in this phrase but it is not comment.')); - // This phrase will not be translated - missing in translation. - w(__('Hello?')); - // Escapes for placeholders - w(__('This is %% percent symbol.')); - // Placeholders with additional arguments - w(__('My %(0)s is faster then your %(1)s!', 'SSD', 'HDD')); - // Placeholders with array - w(__('My %(0)s is faster then your %(1)s!', [ 'Kawasaki', 'Segway' ])); - // Placeholders with object - w(__('My %(0)s is faster then your %(1)s!', { 0: 'Core i7', 1: '486DX' })); - // Both names and order - w(__('Let\'s count in English: %s, %s, %(3)s and %s.', 'one', 'two', 'four', 'three')); - // Plural forms - w(__(['Inbox: %n unreaded message.', 'Inbox: %n unreaded messages.'], 1)); - w(__(['Inbox: %n unreaded message.', 'Inbox: %n unreaded messages.'], 12)); - w(__(['Inbox: %n unreaded message.', 'Inbox: %n unreaded messages.'], 22)); - // All-in-one - w(__([ - '%n developer from our team uses %(0)s with %(1)s.# Comment 1', - '%n developers from our team uses %(0)s with %(1)s.# Comment 2' - ], 1, 'C', 'vim' - )); - w(__([ - '%n developer from our team uses %(0)s with %(1)s.# Comment 3', - '%n developers from our team uses %(0)s with %(1)s.# Comment 4' - ], 3, [ 'Python', 'PyCharm' ] - )); - w(__([ - '%n developer from our team uses %(0)s with %(1)s.# Multiline\ncomment', - '%n developers from our team uses %(0)s with %(1)s.# Another\nmultiline\ncomment' - ], 7, { 0: 'Node.js', 1: 'Sublime Text 2' } - )); - // No args - empty string - w(__()); -} - -// Some support functions - -// The short way message -function w(s) { console.log(s); } diff --git a/i18n.min.js b/i18n.min.js deleted file mode 100644 index e70255b..0000000 --- a/i18n.min.js +++ /dev/null @@ -1,8 +0,0 @@ -/* - * # i18n library - * - * ## locale.js: i18n for Node.js and browser - * - * @author Dmitry A. Chleck - * @url https://github.com/chleck/locale-js - */var locale=new function(){function i(r,i){var o,a;if(!r.phrase)return"";try{r.n===undefined?o=n[i][r.phrase]||u(r.phrase):(a=t[i](r.n),o=n[i][r.phrase[0]][a])}catch(f){if(r.n===undefined)o=u(r.phrase);else try{a=t[e](r.n),o=u(r.phrase[a])}catch(f){o=u(r.phrase[0])}}return s(o,r.args,r.n)}function s(e,t,n){function a(){return r=e[i++],r}function f(){o={};switch(a()){case undefined:break;case"%":u.push("%");break;case"(":l();break;default:i--,c()}}function l(){var t=i;while(a())if(r==")"){o.name=e.substring(t,i-1),c();break}}function c(){o.flags="";while(a()&&" +-".indexOf(r)>=0)o.flags+=r;r&&(i--,h())}function h(){o.width="";while(a()&&"0123456789".indexOf(r)>=0)o.width+=r;r&&(o.width*=1,r=="."?p():d())}function p(){o.precision="";while(a()&&"0123456789".indexOf(r)>=0)o.precision+=r;r&&(o.precision=Number(o.precision),o.precision<0&&(o.precision=0),d())}function d(){var e="",i="";o.type=r;if(o.type=="n"){o.type="d";var a=n}else var a=(o.name?t[o.name]:t[s++])||"";if("debhxX".indexOf(o.type)>=0){typeof a!="number"&&(a=NaN);if(isNaN(a)){if(o.flags.indexOf(" ")>=0||o.flags.indexOf("+")>=0)i=" "}else a<0?i="-":(o.flags.indexOf(" ")>=0&&(i=" "),o.flags.indexOf("+")>=0&&(i="+"));a=Math.abs(a)}switch(o.type){case"s":e=a,!e&&o.flags.indexOf(" ")>=0&&(e=" "),o.precision&&(e=e.substr(0,o.precision));break;case"d":e=i+(o.precision?a.toFixed(o.precision):a.toString());break;case"e":e=i+a.toExponential(o.precision?o.precision:6);break;case"b":e=i+Math.round(a).toString(2)+"b";break;case"h":e=i+Math.round(a).toString(16).toUpperCase()+"h";break;case"x":e=i+"0x"+Math.round(a).toString(16);break;case"X":e=i+"0x"+Math.round(a).toString(16).toUpperCase();break;default:return}var f="";for(var l=o.width-e.length;l>0;l--)f+=" ";o.flags.indexOf("-")>=0?e+=f:e=f+e,u.push(e)}var r,i=0,s=0,o,u=[];while(a())r=="%"?f():u.push(r);return u.join("")}function o(e){var t=0,n;while(n=e[t++])if(n=="#"){if(e[t]!="#")break;t++}var r=e.indexOf(" ",t);r<0&&(r=e.length);var i=e.slice(0,t-1),s=e.slice(t,r);return s&&(i+="#"+s),i}function u(e){var t=0,n;while(n=e[t++])if(n=="#"){if(e[t]!="#")break;t++}return e.slice(0,t-1).replace(/##/g,"#")}var e,t,n={},r=this;this.init=function(n,r){e=n||"en",t={},t[e]=Function("n","return "+(r||"(n == 1 ? 0 : 1)"))},this.add=function(e,r){n[e]=r,t[e]=Function("n","return "+(r[""]||"(n == 1 ? 0 : 1)"))},this.i18n=function(t){var n=this,r;r=t===undefined?"":t,this.to=function(t){if(t===undefined)return r;t===""&&(t=e||"en"),r=t},this.__=function(){var e,t,n=Array.prototype.slice.call(arguments);e=n.shift()||"",Array.isArray(e)&&(t=n.shift());if(t===undefined)e=o(e);else for(var s in e)e[s]=o(e[s]);if(typeof n[0]=="object"||Array.isArray(n[0]))n=n[0];var u={__i18n:!0,phrase:e,n:t,args:n};return r===null?u:i(u,r)},this.tr=function(e){if(typeof e!="object")return;for(var t in e)e[t].__i18n?e[t]=i(e[t],r):n.tr(e[t])}};var a=new r.i18n;for(var f in a)r[f]=a[f]};typeof exports=="object"&&(exports.locale=locale); \ No newline at end of file diff --git a/lib/locale.js b/lib/locale.js new file mode 100644 index 0000000..ca4a07c --- /dev/null +++ b/lib/locale.js @@ -0,0 +1,46 @@ +/* + * # node.js layer + * + * ## locale.js: i18n for Node.js and browser + * + * @author Dmitry A. Chleck + * @url https://github.com/chleck/locale-js + */ + +var locale = require('./i18n').locale; + +var fs = require('fs') + , path = require('path'); + +// Load JSON from file +function loadJson(file, from) { + try { + // return JSON.parse(fs.readFileSync('./' + path.join(from, file + '.json'))); + return require('./' + path.join(from, file + '.json')); + } catch(e) {} +} + +// Save original init() function +var i18n_init = locale.init; + +// # Library initialization +locale.init = function(path) { + // Path to locale folder + path = path || './i18n'; + // Try to load config + var cfg = loadJson('i18n', path) || {}; + // Base language of translation (translate from) + i18n_init(cfg.base, cfg.rule); + // Load translations + for(var lang in cfg.targets) { + lang = cfg.targets[lang]; + console.log(lang); + // Try to load translation for each configured language + var tmp = loadJson(lang, path); + console.log(tmp); + // Add translation if success + if(tmp) locale.add(lang, tmp); + } +} + +module.exports = locale; diff --git a/package.json b/package.json index 25cdab3..d8f7ab1 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Dmitry Chleck ", "license": "MIT", "name": "locale-js", - "version": "1.1.1", + "version": "1.1.2", "description": "i18n for Node.js and browser", "keywords": "locale i18n translate gettext", "main": "index.js", diff --git a/test/i18n/i18n.json b/test/i18n/i18n.json deleted file mode 100644 index 63a08c8..0000000 --- a/test/i18n/i18n.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "base": "en", - "rule": "(n == 1 ? 0 : 1)", - "targets": [ - "ru" - ] -} \ No newline at end of file diff --git a/test/i18n/ru.json b/test/i18n/ru.json deleted file mode 100644 index d26dbbf..0000000 --- a/test/i18n/ru.json +++ /dev/null @@ -1,65 +0,0 @@ -{ - "": "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)", - "Hello!": "Привет!", - "Message.": [ - "Сообщение.", - "Сообщения.", - "Сообщений." - ], - "%s %s!": "%s, %s!", - "%(0)s %(1)s!": "%(0)s, %(1)s!", - "%(one)s %(two)s!": "%(one)s, %(two)s!", - "%(1)s %s!": "%(1)s, %s!", - "%n message.": [ - "%n сообщение.", - "%n сообщения.", - "%n сообщений." - ], - "%5.2s": "", - "%-10s": "", - "% s": "", - "%10.2d": "", - "%-5d": "", - "%+d": "", - "% d": "", - "%10.2e": "", - "%-10.2e": "", - "%+e": "", - "% e": "", - "%10b": "", - "%-10b": "", - "%+b": "", - "% b": "", - "%10h": "", - "%-10h": "", - "%+h": "", - "% h": "", - "%10x": "", - "%-10x": "", - "%+x": "", - "% x": "", - "%10X": "", - "%-10X": "", - "%+X": "", - "% X": "", - "%10.2n": [ - "", - "", - "" - ], - "%-5n": [ - "", - "", - "" - ], - "%+n": [ - "", - "", - "" - ], - "% n": [ - "", - "", - "" - ] -} \ No newline at end of file diff --git a/test/i18n/ru.tr b/test/i18n/ru.tr deleted file mode 100644 index f9cb456..0000000 --- a/test/i18n/ru.tr +++ /dev/null @@ -1,223 +0,0 @@ -@ [1] test.js (53:8) -@ [2] test.js (71:8) -@ [3] test.js (90:8) -@ [4] test.js (108:8) -S Hello! -? Hello! -! Привет! - -@ [1] test.js (57:8) -@ [2] test.js (58:8) -@ [3] test.js (75:8) -@ [4] test.js (76:8) -@ [5] test.js (77:8) -@ [6] test.js (94:8) -@ [7] test.js (95:8) -@ [8] test.js (112:8) -@ [9] test.js (113:8) -P Message. -? Message. -? Messages. -! Сообщение. -! Сообщения. -! Сообщений. - -@ [1] test.js (125:10) -@ [2] test.js (129:10) -S %s %s! -? %s %s! -! %s, %s! - -@ [1] test.js (133:10) -@ [2] test.js (137:10) -S %(0)s %(1)s! -? %(0)s %(1)s! -! %(0)s, %(1)s! - -@ [1] test.js (141:10) -S %(one)s %(two)s! -? %(one)s %(two)s! -! %(one)s, %(two)s! - -@ [1] test.js (145:10) -@ [2] test.js (149:10) -S %(1)s %s! -? %(1)s %s! -! %(1)s, %s! - -@ [1] test.js (153:10) -@ [2] test.js (154:10) -@ [3] test.js (328:15) -P %n message. -? %n message. -? %n messages. -! %n сообщение. -! %n сообщения. -! %n сообщений. - -@ [1] test.js (162:10) -S %5.2s -? %5.2s -! - -@ [1] test.js (166:10) -S %-10s -? %-10s -! - -@ [1] test.js (170:10) -S % s -? % s -! - -@ [1] test.js (178:10) -S %10.2d -? %10.2d -! - -@ [1] test.js (182:10) -S %-5d -? %-5d -! - -@ [1] test.js (186:10) -S %+d -? %+d -! - -@ [1] test.js (190:10) -S % d -? % d -! - -@ [1] test.js (198:10) -S %10.2e -? %10.2e -! - -@ [1] test.js (202:10) -S %-10.2e -? %-10.2e -! - -@ [1] test.js (206:10) -S %+e -? %+e -! - -@ [1] test.js (210:10) -S % e -? % e -! - -@ [1] test.js (218:10) -S %10b -? %10b -! - -@ [1] test.js (222:10) -S %-10b -? %-10b -! - -@ [1] test.js (226:10) -S %+b -? %+b -! - -@ [1] test.js (230:10) -S % b -? % b -! - -@ [1] test.js (238:10) -S %10h -? %10h -! - -@ [1] test.js (242:10) -S %-10h -? %-10h -! - -@ [1] test.js (246:10) -S %+h -? %+h -! - -@ [1] test.js (250:10) -S % h -? % h -! - -@ [1] test.js (258:10) -S %10x -? %10x -! - -@ [1] test.js (262:10) -S %-10x -? %-10x -! - -@ [1] test.js (266:10) -S %+x -? %+x -! - -@ [1] test.js (270:10) -S % x -? % x -! - -@ [1] test.js (278:10) -S %10X -? %10X -! - -@ [1] test.js (282:10) -S %-10X -? %-10X -! - -@ [1] test.js (286:10) -S %+X -? %+X -! - -@ [1] test.js (290:10) -S % X -? % X -! - -@ [1] test.js (298:10) -P %10.2n -? %10.2n -? %10.2n -! -! -! - -@ [1] test.js (302:10) -P %-5n -? %-5n -? %-5n -! -! -! - -@ [1] test.js (306:10) -P %+n -? %+n -? %+n -! -! -! - -@ [1] test.js (310:10) -P % n -? % n -? % n -! -! -! diff --git a/test/i18n/tr.json b/test/i18n/tr.json deleted file mode 100644 index ae7af05..0000000 --- a/test/i18n/tr.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "base": "en", - "n": 2, - "rule": "(n == 1 ? 0 : 1)", - "targets": { - "ru": { - "n": 3, - "rule": "(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2)" - } - }, - "filter": [ - ".js" - ] -} \ No newline at end of file diff --git a/test/mocha.opts b/test/mocha.opts deleted file mode 100644 index 9495c03..0000000 --- a/test/mocha.opts +++ /dev/null @@ -1,2 +0,0 @@ ---reporter spec ---require should diff --git a/test/test.js b/test/test.js deleted file mode 100644 index 98f9cda..0000000 --- a/test/test.js +++ /dev/null @@ -1,340 +0,0 @@ -/* - * # locale library test suite - * - * ## locale.js: i18n for Node.js and browser - * - * @author Dmitry A. Chleck - * @url https://github.com/chleck/locale-js - */ - -var locale = require('..'); - -var i18n, __, tmp; - -describe('Test suite for locale library', function() { - - describe('Configure:', function() { - - it('Library initialization', function() { - locale.init('./test/i18n'); - }) - - it('Add translation', function() { - locale.add('zz', { 'Hello!': 'Bzzzz!' }); - }) - - it('Set target language', function() { - locale.to('ru'); - }) - - it('Get target language', function() { - locale.to().should.equal('ru'); - }) - - it('Create i18n object', function() { - i18n = new locale.i18n(); - }) - - it('Create i18n object for deferred translation', function() { - i18n = new locale.i18n(null); - }) - - }) - - describe('Pass through (no translation):', function() { - - it('Switch to pass trough mode', function() { - locale.to(''); - __ = locale.__; - }) - - it('Simple', function() { - __('Hello!').should.equal('Hello!'); - }) - - it('Plural', function() { - __(['Message.', 'Messages.'], 1).should.equal('Message.'); - __(['Message.', 'Messages.'], 2).should.equal('Messages.'); - }) - - }) - - describe('Translate:', function() { - - it('Switch to Russian', function() { - locale.to('ru'); - __ = locale.__; - }) - - it('Simple', function() { - __('Hello!').should.equal('Привет!'); - }) - - it('Plural', function() { - __(['Message.', 'Messages.'], 1).should.equal('Сообщение.'); - __(['Message.', 'Messages.'], 2).should.equal('Сообщения.'); - __(['Message.', 'Messages.'], 5).should.equal('Сообщений.'); - }) - - }) - - describe('Translate to unknown language:', function() { - - it('Switch to German', function() { - locale.to('de'); - __ = locale.__; - }) - - it('Simple', function() { - __('Hello!').should.equal('Hello!'); - }) - - it('Plural', function() { - __(['Message.', 'Messages.'], 1).should.equal('Message.'); - __(['Message.', 'Messages.'], 2).should.equal('Messages.'); - }) - - }) - - describe('Translate with i18n object:', function() { - - it('Create i18n object for Russian', function() { - i18n = new locale.i18n('ru'); - __ = i18n.__; - }) - - it('Simple', function() { - __('Hello!').should.equal('Привет!'); - }) - - it('Plural', function() { - __(['Message.', 'Messages.'], 1).should.equal('Сообщение.'); - __(['Message.', 'Messages.'], 2).should.equal('Сообщения.'); - }) - - }) - - describe('Placeholders:', function() { - - describe('Arguments:', function() { - - it('%s with additional args', function() { - locale.to(''); - __ = locale.__; - __('%s %s!', 'Hello', 'world').should.equal('Hello world!'); - }) - - it('%s with array args', function() { - __('%s %s!', [ 'Hello', 'world' ]).should.equal('Hello world!'); - }) - - it('%(n)s with additional args', function() { - __('%(0)s %(1)s!', 'Hello', 'world').should.equal('Hello world!'); - }) - - it('%(n)s with array args', function() { - __('%(0)s %(1)s!', [ 'Hello', 'world' ]).should.equal('Hello world!'); - }) - - it('%(key)s with map args', function() { - __('%(one)s %(two)s!', { one: 'Hello', two: 'world' }).should.equal('Hello world!'); - }) - - it('mixed %s and %(n)s with additional args', function() { - __('%(1)s %s!', 'world', 'Hello').should.equal('Hello world!'); - }) - - it('mixed %s and %(n)s with array args', function() { - __('%(1)s %s!', [ 'world', 'Hello' ]).should.equal('Hello world!'); - }) - - it('%n', function() { - __(['%n message.', '%n messages.'], 1).should.equal('1 message.'); - __(['%n message.', '%n messages.'], 2).should.equal('2 messages.'); - }) - - }) - - describe('%s:', function() { - - it('with width and precision', function() { - __('%5.2s', [ 'test' ]).should.equal(' te'); - }) - - it('with \'-\' flag', function() { - __('%-10s', [ 'Hi!' ]).should.equal('Hi! '); - }) - - it('with \' \' flag', function() { - __('% s', [ '' ]).should.equal(' '); - }) - - }) - - describe('%d:', function() { - - it('with width and precision', function() { - __('%10.2d', [ 13 ]).should.equal(' 13.00'); - }) - - it('with \'-\' flag', function() { - __('%-5d', [ -42 ]).should.equal('-42 '); - }) - - it('with \'+\' flag', function() { - __('%+d', [ 42 ]).should.equal('+42'); - }) - - it('with \' \' flag', function() { - __('% d', [ 42 ]).should.equal(' 42'); - }) - - }) - - describe('%e:', function() { - - it('with width and precision', function() { - __('%10.2e', [ 10/3 ]).should.equal(' 3.33e+0'); - }) - - it('with \'-\' flag', function() { - __('%-10.2e', [ -10/3 ]).should.equal('-3.33e+0 '); - }) - - it('with \'+\' flag', function() { - __('%+e', [ 10/3 ]).should.equal('+3.333333e+0'); - }) - - it('with \' \' flag', function() { - __('% e', [ 10/3 ]).should.equal(' 3.333333e+0'); - }) - - }) - - describe('%b:', function() { - - it('with width', function() { - __('%10b', [ -13 ]).should.equal(' -1101b'); - }) - - it('with \'-\' flag', function() { - __('%-10b', [ -13 ]).should.equal('-1101b '); - }) - - it('with \'+\' flag', function() { - __('%+b', [ 13 ]).should.equal('+1101b'); - }) - - it('with \' \' flag', function() { - __('% b', [ 13 ]).should.equal(' 1101b'); - }) - - }) - - describe('%h:', function() { - - it('with width', function() { - __('%10h', [ -43981 ]).should.equal(' -ABCDh'); - }) - - it('with \'-\' flag', function() { - __('%-10h', [ -43981 ]).should.equal('-ABCDh '); - }) - - it('with \'+\' flag', function() { - __('%+h', [ 43981 ]).should.equal('+ABCDh'); - }) - - it('with \' \' flag', function() { - __('% h', [ 43981 ]).should.equal(' ABCDh'); - }) - - }) - - describe('%x:', function() { - - it('with width', function() { - __('%10x', [ -43981 ]).should.equal(' -0xabcd'); - }) - - it('with \'-\' flag', function() { - __('%-10x', [ -43981 ]).should.equal('-0xabcd '); - }) - - it('with \'+\' flag', function() { - __('%+x', [ 43981 ]).should.equal('+0xabcd'); - }) - - it('with \' \' flag', function() { - __('% x', [ 43981 ]).should.equal(' 0xabcd'); - }) - - }) - - describe('%X:', function() { - - it('with width', function() { - __('%10X', [ -43981 ]).should.equal(' -0xABCD'); - }) - - it('with \'-\' flag', function() { - __('%-10X', [ -43981 ]).should.equal('-0xABCD '); - }) - - it('with \'+\' flag', function() { - __('%+X', [ 43981 ]).should.equal('+0xABCD'); - }) - - it('with \' \' flag', function() { - __('% X', [ 43981 ]).should.equal(' 0xABCD'); - }) - - }) - - describe('%n:', function() { - - it('with width and precision', function() { - __([ '%10.2n', '%10.2n' ], 13).should.equal(' 13.00'); - }) - - it('with \'-\' flag', function() { - __([ '%-5n', '%-5n' ], -42).should.equal('-42 '); - }) - - it('with \'+\' flag', function() { - __([ '%+n', '%+n' ], 42).should.equal('+42'); - }) - - it('with \' \' flag', function() { - __([ '% n', '% n' ], 42).should.equal(' 42'); - }) - - }) - - }) - - describe('Deferred translation:', function() { - - it('Turn on deferred translation mode', function() { - locale.to(null); - __ = locale.__; - }) - - it('Create object containing deferred translation data', function() { - tmp = { - data: 'Some data...', - array: [ 1, 2, 3, 4, 5 ], - msg: __(['%n message.', '%n messages.'], 1) - }; - }) - - it('Translate object', function() { - locale.to(''); - locale.tr(tmp); - tmp.msg.should.equal('1 message.'); - tmp.data.should.equal('Some data...'); - }) - - }) - -}) \ No newline at end of file