Skip to content

Commit 775851e

Browse files
Szabolcsakx
authored andcommitted
Number spelling based on the CLDR's RBNF rules
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
1 parent 9d6803a commit 775851e

4 files changed

Lines changed: 964 additions & 1 deletion

File tree

babel/numbers.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import warnings
2525

2626
from babel.core import default_locale, Locale, get_global
27+
from babel.rbnf import RuleBasedNumberFormat
2728

2829
try:
2930
# Python 2
@@ -662,6 +663,26 @@ def __init__(self, message, suggestions=None):
662663
self.suggestions = suggestions
663664

664665

666+
def spell_number(number, locale=LC_NUMERIC, **kwargs):
667+
"""Return value spelled out for a specific locale
668+
669+
:param number: the number to format
670+
:param locale: the `Locale` object or locale identifier
671+
:param kwargs: optional locale specific parameters
672+
"""
673+
speller = RuleBasedNumberFormat.negotiate(locale)
674+
return speller.format(number, **kwargs)
675+
676+
677+
def get_rbnf_rules(locale=LC_NUMERIC):
678+
"""Return all the available public rules for a specific locale
679+
680+
:param locale: the `Locale` object or locale identifier
681+
"""
682+
speller = RuleBasedNumberFormat.negotiate(locale)
683+
return speller.available_rulesets
684+
685+
665686
def parse_number(string, locale=LC_NUMERIC):
666687
"""Parse localized number string into an integer.
667688

0 commit comments

Comments
 (0)