Skip to content

fix: add support for babel>=2.14.0 and updated Locale.number_symbols format #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

0.2.3 (2025-01-18)
------------------

* Fix issues #17: Add support for babel>=2.14.0 and updated Locale.number_symbols format

0.2.2 (2020-10-29)
------------------

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[bumpversion]
current_version = 0.2.2
current_version = 0.2.3
commit = True
tag = True

Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
],
description="Spreadsheet Number Format processor - a Python port of SheetJS/ssf.js",
install_requires=requirements,
Expand All @@ -45,6 +49,6 @@
test_suite='tests',
tests_require=test_requirements,
url='https://github.com/snoopyjc/ssf',
version='0.2.2',
version='0.2.3',
zip_safe=False,
)
2 changes: 1 addition & 1 deletion ssf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

__author__ = """Joe Cool"""
__email__ = '[email protected]'
__version__ = '0.2.2'
__version__ = '0.2.3'

from .ssf import SSF
28 changes: 18 additions & 10 deletions ssf/ssf.py
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,13 @@ def __init__(self, locale=None, locale_support=True, locale_currency=True, decim
locale.months['format']['wide'][month]))
self.months_leap = self.months
if locale:
self.decimal_point = decimal_separator or locale.number_symbols['decimal']
self.thousands_sep = thousands_separator or locale.number_symbols['group']
self.plus_sign = locale.number_symbols['plusSign']
self.minus_sign = locale.number_symbols['minusSign']
self.percent_sign = locale.number_symbols['percentSign']
self.time_separator = locale.number_symbols['timeSeparator']
self.exponential = locale.number_symbols['exponential']
self.decimal_point = decimal_separator or self.get_number_symbols_property(locale, 'decimal')
self.thousands_sep = thousands_separator or self.get_number_symbols_property(locale, 'group')
self.plus_sign = self.get_number_symbols_property(locale, 'plusSign')
self.minus_sign = self.get_number_symbols_property(locale, 'minusSign')
self.percent_sign = self.get_number_symbols_property(locale, 'percentSign')
self.time_separator = self.get_number_symbols_property(locale, 'timeSeparator')
self.exponential = self.get_number_symbols_property(locale, 'exponential')
self.time_format = locale.time_formats['medium'].pattern.replace('a', 'AM/PM')
self.short_date_format = locale.date_formats['short'].pattern.replace('E', 'd'). \
replace('M', 'm')
Expand All @@ -592,6 +592,13 @@ def __init__(self, locale=None, locale_support=True, locale_currency=True, decim
else:
raise ValueError(f'Locale {self.locale_name} not found!')

def get_number_symbols_property(self, locale, prop):
"""Get a property from the number symbols for the given locale"""
# https://github.com/snoopyjc/ssf/issues/17
old_val = locale.number_symbols.get(prop)
new_val = locale.number_symbols.get('latn', {}).get(prop)
return old_val or new_val

def normalize_locale(self, locale):
"""Normalize locale based on examples in the lcid/locale map"""
if locale is None:
Expand Down Expand Up @@ -638,14 +645,15 @@ def commaify(self, s): # Add commas to ints
df = ls-ln
return s[:df] + self.commaify(s[df:])
if self.locale is not None:
locale_group = self.get_number_symbols_property(self.locale, 'group')
if s[0] == '0': # Special processing for leading zeros
i = int('1'+s) # Protect them with a leading '1', which we later remove
result = format_decimal(i, locale=self.locale)
result = re.sub(r'^1(?:' + re.escape(self.locale.number_symbols['group']) + r')?(.*)$', r'\1', result)
result = re.sub(r'^1(?:' + re.escape(locale_group) + r')?(.*)$', r'\1', result)
else:
result = format_decimal(int(s), locale=self.locale)
if self.thousands_sep != self.locale.number_symbols['group']:
result = result.replace(self.locale.number_symbols['group'], self.thousands_sep)
if self.thousands_sep != locale_group:
result = result.replace(locale_group, self.thousands_sep)
return result

w = self.grouping[0] if len(self.grouping) >= 1 else 3
Expand Down