Skip to content

Locale order support - #789

Merged
serhii73 merged 10 commits into
masterfrom
locale-order-support
Jun 8, 2026
Merged

serhii73 merged 10 commits into
masterfrom
locale-order-support

Conversation

@dariuschira

Copy link
Copy Markdown
Contributor

closes #770

Comment thread docs/usage.rst Resolved
Comment thread dateparser/date.py Outdated
if not locales and use_given_order:
raise ValueError("locales must be given if use_given_order is True")
if not (locales or languages) and settings.USE_GIVEN_LANGUAGE_ORDER:
raise ValueError("locales or languages must be given if USE_GIVEN_LANGUAGE_ORDER is True")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure of this failure. If the given list of languages is empty in this case then it should use the defaults.

I'm thinking on the case where this is combined with a language detection model. For some cases, the prediction could fail and not return any language, but still, dateparser must try its best.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do see your point, and I agree. The question is if this is what's expected: to restrict to the given locales (or languages) or to use those as a starting point but try the other locales anyways.
If the second is true, then removing this check should be the right choice.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can talk about the use case I have in mind: using a page language detector to provide hints to have better date parsing. In this case, what you have is just hints:

  • In some case, you don't get any language from the language detector. Still, you want dateparser to try its best.
  • In some cases, the list won't be complete. You don't want to remove locales just because the language detector didn't detect them.

So from my point of view, this list should be just hints: they can change the order of the default locales, but never restrict them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ivanprado, the languages and locales parameters have been used historically for restricting the used languages. This is sometimes really important and helps to improve the performance, so I think we can't change them to work as you mentioned.

However, what you say sounds like adding something new like a exclude_languages parameter, so you could do something like this:

date = dateparser.parse("<date string>", languages=['ru', 'tr', 'es'])
if not date:
    date = dateparser.parse("<date string>", exclude_languages=['ru', 'tr', 'es'])

What do you think? Could this work for you?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, on the argument of not raising an exception because the calling code may provide an empty list, the calling code should also be able not to set that setting when that happens. I fail to see the point of removing the exception.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noviluni I see your point. It was not clear to me that locales and languages were used as contraints to improve performance. The documentation (https://github.com/scrapinghub/dateparser/blob/master/dateparser/__init__.py#L23-L31) is ambiguous, and my first thought was that they would be used as "hints" . I think it would be great to update the documentation to make it clear that they restrict the languages used.

Given that languages and locales are for restricting I agree with you than then maybe is not the best place for the use case I'm proposing. What I'm proposing is to be able to propose "hints" for the locales/languages. I don't think exclude_languages should be way.

What do you think about a new parameter language_hints? It would be used just to alter the languages/locales priorities.

@noviluni noviluni left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mirceachira! Good job!

It seems that there are some errors in the flake8 pipeline: https://travis-ci.org/github/scrapinghub/dateparser/jobs/728909794, could you fix them?

Comment thread dateparser/date.py Outdated

@apply_settings
def __init__(self, languages=None, locales=None, region=None, try_previous_locales=False,
use_given_order=False, settings=None):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably deprecate this instead of just removing it, but maybe we can do it as we are going to release a new version with multiple breaking changes. cc: @Gallaecio

Comment thread dateparser_data/settings.py Outdated
'RETURN_TIME_AS_PERIOD': False,
'PARSERS': default_parsers,
'REQUIRE_PARTS': [],
'USE_GIVEN_LANGUAGE_ORDER': False

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if it's better to keep the False by default or to change to True. Any strong opinion?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think False is right as a default because that way you'd get the added benefit of trying your most likely language first. If you give a list of locales I'd assume you want the date to be extracted correctly regardless of which of your locales is the right one. Also, I would argue that expecting it to be as efficient as possible by default is better if you allow the customization.

@codecov

codecov Bot commented Sep 24, 2020 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 97.11%. Comparing base (081d251) to head (cc3a925).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #789   +/-   ##
=======================================
  Coverage   97.11%   97.11%           
=======================================
  Files         235      235           
  Lines        2912     2913    +1     
=======================================
+ Hits         2828     2829    +1     
  Misses         84       84           

β˜” View full report in Codecov by Harness.
πŸ“’ Have feedback on the report? Share it here.

πŸš€ New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread dateparser/date.py Resolved
@dariuschira dariuschira self-assigned this Sep 24, 2020
@noviluni noviluni added this to the v1.0.0 milestone Sep 28, 2020
@noviluni noviluni modified the milestones: v1.0.0, 1.1.0 Oct 26, 2020
@noviluni noviluni mentioned this pull request Oct 27, 2020
dariuschira and others added 10 commits June 5, 2026 15:16
…ty (#770)

Reconcile the rebased work onto current master with the agreed design:

- Keep the public ``use_given_order`` constructor argument (non-breaking);
  the new ``USE_GIVEN_LANGUAGE_ORDER`` setting is OR-ed with it rather than
  replacing it, and also reaches the top-level ``dateparser.parse()``.
- When the setting is True but no languages/locales are given, it is a no-op
  (fall back to the default order) instead of raising.
- Register ``USE_GIVEN_LANGUAGE_ORDER`` in ``check_settings`` so it is a valid
  setting, document it under "Language Order" in docs/settings.rst, and add
  settings-validation and parsing tests.
- Revert the unneeded loader change: DateDataParser already normalizes an
  empty languages list to None.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@serhii73
serhii73 force-pushed the locale-order-support branch from 94c96fd to cc3a925 Compare June 5, 2026 13:20
@serhii73 serhii73 self-assigned this Jun 5, 2026
@serhii73
serhii73 requested a review from wRAR June 8, 2026 07:21
@serhii73
serhii73 merged commit 0d11d82 into master Jun 8, 2026
18 checks passed
@serhii73
serhii73 deleted the locale-order-support branch June 8, 2026 08:33
@serhii73 serhii73 mentioned this pull request Jun 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong prioritization of languages

6 participants