Fix the plural category picker for notifications

Review the AI's codeMedium15 min

Your task

Your messaging app is being translated into 12 languages. An AI wrote the notification summary as "1 new message" for a count of 1 and "N new messages" otherwise, which only fits English. Your team is moving the strings to an ICU MessageFormat catalog, which picks each translation by the count's plural category from CLDR, the Unicode project's shared locale data. This function works out the category for each count.

An assistant wrote select_plural_categories(locale, counts). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • locale: a locale tag, like "pl", "en-US" or "EN_GB".
  • counts: message counts. They can be negative.
  • Return the plural category of each count, in the same order.

Rules the tests check. Below, n is the absolute value of a count, "n mod 10" is its last digit and "n mod 100" its last two digits.

  • The language is the locale lowercased, up to its first - or _. An empty or unknown language gives "other" for every count.
  • en: "one" when n is 1, otherwise "other".
  • ja: always "other".
  • pl: "one" when n is 1. Otherwise "few" when n mod 10 is 2 to 4 and n mod 100 is not 12 to 14. Otherwise "many".
  • ru: "one" when n mod 10 is 1 and n mod 100 is not 11. Otherwise "few" when n mod 10 is 2 to 4 and n mod 100 is not 12 to 14. Otherwise "many".
  • ar: "zero" for 0, "one" for 1 and "two" for 2. Otherwise "few" when n mod 100 is 3 to 10, "many" when n mod 100 is 11 to 99, and "other" for the rest.

Examples

select_plural_categories(locale: str, counts: list[int]) -> list[str]
Example 1
Inputselect_plural_categories("en", [0, 1, 2, 5])
Output["other", "one", "other", "other"]
Example 2
Inputselect_plural_categories("pl", [1, 2, 5, 22, 25])
Output["one", "few", "many", "few", "many"]
Example 3
Inputselect_plural_categories("ar", [0, 1, 2, 3, 11, 100])
Output["zero", "one", "two", "few", "many", "other"]