Skip to content

Commit

Permalink
Number spelling based on the CLDR's RBNF rules
Browse files Browse the repository at this point in the history
A pure Python engine for parsing RBNF rules.
The rules are incomplete in many cases, fractional
number spelling is hardly supported.

Based on an earlier discussion:
#114
and referenced in
#179
  • Loading branch information
Szabolcs authored and akx committed Jan 28, 2022
1 parent 9d6803a commit 775851e
Show file tree
Hide file tree
Showing 4 changed files with 964 additions and 1 deletion.
21 changes: 21 additions & 0 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import warnings

from babel.core import default_locale, Locale, get_global
from babel.rbnf import RuleBasedNumberFormat

try:
# Python 2
Expand Down Expand Up @@ -662,6 +663,26 @@ def __init__(self, message, suggestions=None):
self.suggestions = suggestions


def spell_number(number, locale=LC_NUMERIC, **kwargs):
"""Return value spelled out for a specific locale
:param number: the number to format
:param locale: the `Locale` object or locale identifier
:param kwargs: optional locale specific parameters
"""
speller = RuleBasedNumberFormat.negotiate(locale)
return speller.format(number, **kwargs)


def get_rbnf_rules(locale=LC_NUMERIC):
"""Return all the available public rules for a specific locale
:param locale: the `Locale` object or locale identifier
"""
speller = RuleBasedNumberFormat.negotiate(locale)
return speller.available_rulesets


def parse_number(string, locale=LC_NUMERIC):
"""Parse localized number string into an integer.
Expand Down
Loading

0 comments on commit 775851e

Please sign in to comment.