1

Given an expression or variable that returns a number, is there a way to get the plural category of that number for the current locale?

Something like

// people.length == 1
ngPluralize.getCategory(people.length) // returns 'one'
bearfriend
  • 10,322
  • 3
  • 22
  • 28

1 Answers1

1

The $locale has rather tiny public API. However, there is a $locale.pluralCat method, so you can use it.

angular.module('app', [])
  .controller('LocaleCtrl', function($scope, $locale) {
    $scope.locale = $locale.id;
    $scope.getCategory = function(count) {
      return $locale.pluralCat(count);
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-i18n/1.5.5/angular-locale_pl-pl.js"></script>
<div ng-app="app">
  <div ng-controller="LocaleCtrl">
    <h1>ng-pluralize category for locale {{ locale }}</h1>
    <ul>
      <li ng-repeat="count in [0, 1, 2, 3, 4, 5, 6]">
        category for {{count}} is {{ getCategory(count) }}
      </li>
    </ul>
  </div>
</div>

Of course, the results are different if you use different locales.

fracz
  • 20,536
  • 18
  • 103
  • 149