toretore / babilu

ABANDONED - Use i18n-js instead
MIT License
65 stars 11 forks source link

translation with count option (pluralize) fails when count == 0 #3

Closed basvk closed 13 years ago

basvk commented 13 years ago

example: in en.yml: items_left: one: '1 item left' other: '{{count}} items left'

I18n.t('items_left', {'count': 0}) returns [Object Object] rather than the expected '0 items left' This error is caused by the first line in the pluralize function:

I18n.pluralize = function(value, count){
  if (!count) return value;
  return count == 1 ? value.one : value.other;
};

The (!count) expression returns false when count is 0, causing the value object to be returned without choosing the correct plural form. The value object is an object containing the 'one' and 'other' keys, it doesn't really make sense to return it. In my opinion this if (!count) check can be safely removed, and the function changed to this:

I18n.pluralize = function(value, count){
  return (typeof(count) !== 'undefined' && count == 1) ? value.one : value.other;
}

This way pluralize always returns a correct key.

toretore commented 13 years ago

fixd

basvk commented 13 years ago

unfortunately this still fails... The interpolation itself still fails when 0 has to be interpolated, same problem as in pluralize. I forgot to mention that when I first created this ticket, sorry for that.

The offending function: function interpolate(str, obj){ return str.replace(interpolatePattern, function(){ return obj[arguments[1]] || arguments[0]; }); };

When count = 0, obj[arguments[1]] is 0, and due to this, the arguments[0] string is returned instead (since the boolean || operator sees 0 as false). I guess 0 should be checked as a separate case, something like this:

function interpolate(str, obj){
  return str.replace(interpolatePattern, function(){
    return (obj[arguments[1]] === 0 ? '0' : obj[arguments[1]]) || arguments[0];
  });
};
toretore commented 13 years ago

fixed