Why %%? #49

Closed
enyo opened this Issue Feb 6, 2013 · 3 comments

Projects

None yet

2 participants

@enyo
enyo commented Feb 6, 2013

Sorry I don't get it, and the documentation for plural support is insufficient.

Why do I need to use two % signs in the example provided in the readme:

__n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, 'tree')

Is this a special case because of the plural? What if there was another string parameter after that? Would I need to use 3 % signs?

@mashpie
Owner
mashpie commented Feb 7, 2013

well you don't and you shouldn't in that setup

__n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, 'tree')
__n('There is one monkey in the %s', 'There are %d monkeys in the %s', 3, 'tree')

you are probably refering to an example of nested usage like so

 __n('There is one monkey in the %%s', 'There are %d monkeys in the %%s', 3, __('tree'));
@mashpie
Owner
mashpie commented Feb 7, 2013

hm, looks like I need to correct myself, stay tuned

@mashpie
Owner
mashpie commented Feb 7, 2013

ok, answer is explaind by implementation of __n(), see comments:

i18n.__n = function (singular, plural, count) {
  var locale, msg;

  // get locale from scope (deprecated) or object
  if (this && this.scope) {
    locale = this.scope.locale;
  }
  if (this && this.locale) {
    locale = this.locale;
  }

  // get translation
  msg = translate(locale, singular, plural);

  // parse translation and replace all digets '%d' by `count`
  // this also replaces extra strings '%%s' to parseble '%s' for next step

  // simplest 2 form implementation of plural, like https://developer.mozilla.org/en/docs/Localization_and_Plurals#Plural_rule_.231_.282_forms.29
  if (parseInt(count, 10) > 1) {
    msg = vsprintf(msg.other, [count]);
  } else {
    msg = vsprintf(msg.one, [count]);
  }

  // if we have extra arguments with strings to get replaced, 
  // an additional substition injects those strings afterwards
  if (arguments.length > 3) {
    msg = vsprintf(msg, Array.prototype.slice.call(arguments, 3));
  }

  return msg;
};
@mashpie mashpie added a commit that referenced this issue Feb 7, 2013
@mashpie added comments and test to clarify #49
behavior will change as I intent to precheck any unmasked strings (%s) internaly
724bf45
@mashpie mashpie closed this Mar 15, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment