1

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()

1 Answer 1

3

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

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

code

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  );

output

enter image description here

Back-office Translation

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

1
  • Yes, with the exception that in English, 0 is written in the plural format (ex: I have 0 items, not: I have 0 item), and Drupal (as of 10.1, current as of today) will use the plural format if no translation table exists and the language code is set to "en". Source: api.drupal.org/api/drupal/… Commented Aug 9, 2023 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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