what is the difference between :

Drupal.t(string, arguments, options ) and Drupal.formatPlural()

Drupal.t() is used in the javascript part to translate the string and it can accept a set of arguments that will translate the string placeholders ex:

var translatedString = Drupal.t('a string about @subject that needs to be translated', {'@subject': 'Javascript in Drupal'});

what about Drupal.formatPlural()


Actually after some playing aroud with the code I HAVE FOUND THE ANSWER :

formatPlural($count, $singular, $plural, array $args = array(), array $options = array());


the formPlural function enable us to translate a string in plural or singular format according to the first argument $count , a $singualr string will display if $count = 0 or 1 otherwise if $count > 1 the $plural string will be displayed , and in plural string we can get the $count value using @count , for example in the code bellow :

  • the first formPlural function will return $singular string since $count = 1
  • the second formPlural function will return $plural string since $count > 1
var typeVar = 'list';
var singular = Drupal.formatPlural(1, 'I have 1 item', 'I have many @type items', {'@type': typeVar});
$('#translate_singular_item').html(  singular   );

var plural = Drupal.formatPlural(3, 'I have 1 item', 'I have @count @type items', {'@type': typeVar});
$('#translate_plural_item').html(  plural  );


enter image description here

Back-office Translation

now we can translate both singular and plural string : enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.