Skip to content
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

fix translate keep_formatting #720

Merged
merged 1 commit into from Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion dateparser/languages/locale.py
Expand Up @@ -141,7 +141,8 @@ def translate(self, date_string, keep_formatting=False, settings=None):
date_string_tokens[i] = pattern.sub(replacement, word)
else:
if word in dictionary:
date_string_tokens[i] = dictionary[word] or ''
fallback = word if keep_formatting and not word.isalpha() else ''
date_string_tokens[i] = dictionary[word] or fallback
if "in" in date_string_tokens:
date_string_tokens = self._clear_future_words(date_string_tokens)

Expand Down
20 changes: 19 additions & 1 deletion tests/test_languages.py
Expand Up @@ -4,17 +4,35 @@
from io import StringIO

import logging

import pytest
from parameterized import parameterized, param

from dateparser.languages import default_loader, Locale
from dateparser.languages.validation import LanguageValidator
from dateparser.conf import apply_settings
from dateparser.conf import apply_settings, settings
from dateparser.search.detection import AutoDetectLanguage, ExactLanguages
from dateparser.utils import normalize_unicode

from tests import BaseTestCase


class TestLocaleTranslation:

@pytest.mark.parametrize("date_string,expected,locale,keep_formatting", [
('December 04, 1999, 11:04:59 PM', 'december 04, 1999, 11:04:59 pm', 'en', True),
('December 04, 1999, 11:04:59 PM', 'december 04 1999 11:04:59 pm', 'en', False),
('23 März, 18:37', '23 march, 18:37', 'de', True),
('23 März 18:37', '23 march 18:37', 'de', False),
])
def test_keep_formatting(self, date_string, expected, locale, keep_formatting):
result = default_loader.get_locale(locale).translate(
date_string=date_string, keep_formatting=keep_formatting, settings=settings
)
print(result)
assert expected == result


class TestBundledLanguages(BaseTestCase):
def setUp(self):
super(TestBundledLanguages, self).setUp()
Expand Down