diff --git a/build/translate.js b/build/translate.js index b35bd23..1a892a1 100644 --- a/build/translate.js +++ b/build/translate.js @@ -1,7 +1,7 @@ /** * Microlib for translations with support for placeholders and multiple plural forms. * - * v1.0.1 + * v1.1.0 * * Usage: * var messages = { @@ -12,8 +12,9 @@ * } * * var options = { - * debug: true, //[Boolean]: Logs missing translations to console and adds @@-markers around output. Defaults to false. - * namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys. Defaults to '::'. + * // These are the defaults: + * debug: false, //[Boolean]: Logs missing translations to console and adds @@-markers around output. + * namespaceSplitter: '::' //[String|RegExp]: You can customize the part which splits namespace and translationKeys. * } * * var t = libTranslate.getTranslationFunction(messages, [options]) @@ -63,13 +64,24 @@ function getPluralValue(translation, count) { if (isObject(translation)) { - if(Object.keys(translation).length === 0) { + var keys = Object.keys(translation); + var upperCap; + + if(keys.length === 0) { debug && console.log('[Translation] No plural forms found.'); return null; } + for(var i = 0; i < keys.length; i++) { + if(keys[i].indexOf('gt') === 0) { + upperCap = parseInt(keys[i].replace('gt', ''), 10); + } + } + if(translation[count]){ translation = translation[count]; + } else if(count > upperCap) { //int > undefined returns false + translation = translation['gt' + upperCap]; } else if(translation.n) { translation = translation.n; } else { diff --git a/build/translate.min.js b/build/translate.min.js index a2ba891..e971b73 100644 --- a/build/translate.min.js +++ b/build/translate.min.js @@ -1,6 +1,6 @@ -/*! translate.js - v1.0.1 - 2015-03-24 +/*! translate.js - v1.1.0 - 2015-06-16 * https://github.com/musterknabe/translate.js * Copyright (c) 2015 Jonas Girnatis * Licensed under MIT license */ -!function(){"use strict";var a=function(a){return!isNaN(parseFloat(a))&&isFinite(a)},b=function(a){return"object"==typeof a&&null!==a},c=function(a){return"[object String]"===Object.prototype.toString.call(a)};window.libTranslate={getTranslationFunction:function(d,e){function f(a){if(d[a])return d[a];var b=a.split(j),c=b[0],e=b[1];return d[c]&&d[c][e]?d[c][e]:null}function g(a,c){if(b(a)){if(0===Object.keys(a).length)return i&&console.log("[Translation] No plural forms found."),null;a[c]?a=a[c]:a.n?a=a.n:(i&&console.log('[Translation] No plural forms found for count:"'+c+'" in',a),a=a[Object.keys(a).reverse()[0]])}return a}function h(a,b){return c(a)?a.replace(/\{(\w*)\}/g,function(a,c){return b.hasOwnProperty(c)?b.hasOwnProperty(c)?b[c]:c:(i&&console.log('Could not find replacement "'+c+'" in provided replacements object:',b),"{"+c+"}")}):a}e=b(e)?e:{};var i=e.debug,j=e.namespaceSplitter||"::";return function(c){var d=b(arguments[1])?arguments[1]:b(arguments[2])?arguments[2]:{},e=a(arguments[1])?arguments[1]:a(arguments[2])?arguments[2]:null,j=f(c);return null!==e&&(d.n=d.n?d.n:e,j=g(j,e)),j=h(j,d),null===j&&(j=i?"@@"+c+"@@":c,i&&console.log('Translation for "'+c+'" not found.')),j}}}}(); \ No newline at end of file +!function(){"use strict";var a=function(a){return!isNaN(parseFloat(a))&&isFinite(a)},b=function(a){return"object"==typeof a&&null!==a},c=function(a){return"[object String]"===Object.prototype.toString.call(a)};window.libTranslate={getTranslationFunction:function(d,e){function f(a){if(d[a])return d[a];var b=a.split(j),c=b[0],e=b[1];return d[c]&&d[c][e]?d[c][e]:null}function g(a,c){if(b(a)){var d,e=Object.keys(a);if(0===e.length)return i&&console.log("[Translation] No plural forms found."),null;for(var f=0;fd?a=a["gt"+d]:a.n?a=a.n:(i&&console.log('[Translation] No plural forms found for count:"'+c+'" in',a),a=a[Object.keys(a).reverse()[0]])}return a}function h(a,b){return c(a)?a.replace(/\{(\w*)\}/g,function(a,c){return b.hasOwnProperty(c)?b.hasOwnProperty(c)?b[c]:c:(i&&console.log('Could not find replacement "'+c+'" in provided replacements object:',b),"{"+c+"}")}):a}e=b(e)?e:{};var i=e.debug,j=e.namespaceSplitter||"::";return function(c){var d=b(arguments[1])?arguments[1]:b(arguments[2])?arguments[2]:{},e=a(arguments[1])?arguments[1]:a(arguments[2])?arguments[2]:null,j=f(c);return null!==e&&(d.n=d.n?d.n:e,j=g(j,e)),j=h(j,d),null===j&&(j=i?"@@"+c+"@@":c,i&&console.log('Translation for "'+c+'" not found.')),j}}}}(); \ No newline at end of file