A language has two independent paths in this package: reading, which chronologia owns, and writing, which lives here. This page walks both. Do the path you need, or both.
Run this check first. It lists which functions already work for a language code.
import logging
from datetime import datetime, timedelta
import ovos_date_parser as p
logging.disable(logging.CRITICAL) # a missing resource file logs a warning
dt = datetime(2024, 1, 5, 15, 30)
for name, call in {
"nice_time": lambda c: p.nice_time(dt, c),
"nice_date": lambda c: p.nice_date(dt, c),
"nice_year": lambda c: p.nice_year(dt, c),
"nice_duration": lambda c: p.nice_duration(3690, c),
"nice_relative_time": lambda c: p.nice_relative_time(dt + timedelta(days=5), dt, c),
}.items():
try:
call("id")
print(name, "ok")
except Exception as error:
print(name, type(error).__name__)
print("reads:", "id" in p.CHRONOLOGIA_LOCALES)For Indonesian the output is:
nice_time ok
nice_date NotImplementedError
nice_year NotImplementedError
nice_duration ok
nice_relative_time ok
reads: True
A language with reads: False needs the reading path. A language that raises NotImplementedError needs the writing path.
This package holds no reader for a chronologia locale. extract_datetime and extract_duration normalize the tag and call chronologia, so the language must exist there first. Follow
chronologia's guide. It covers the vocabulary files, their sources and their tests.
When the chronologia release that reads the language is published, make two changes here:
- Raise the floor of
chronologiainpyproject.tomlto that release. - Add the code to
CHRONOLOGIA_LOCALESinovos_date_parser/__init__.py. A locale outside the set goes to the dateparser fallback for dates and answers(None, text)for durations.
The same entry gives the language extract_duration. A month or a year counts on the calendar from the day of the call. The resolution and replace_token options convert chronologia's answer.
Phrase files that this package adds on top of chronologia, such as era and calendar-scoped phrases, live in ovos_date_parser/locale/<code>/*.voc. A file there overrides the file of the same name in chronologia.
Write the clock first, then the date family, then the duration words.
-
Create
ovos_date_parser/dates_<code>.py. Start from a language that is close to yours:dates_pt.py,dates_es.pyanddates_eu.pyshare a lineage, and so dodates_en.py,dates_nl.pyanddates_de.py. Put the sources of every word in the module docstring, asdates_kab.pydoes. -
Write
nice_time_<code>(dt, speech=True, use_24hour=False, use_ampm=False). Withspeech=Falseit returns the digit clock. -
Import it in
ovos_date_parser/__init__.py, add the code to_WRITER_LANGSinovos_date_parser/common.py, and add acode == "<code>"branch tonice_time. Without the branch,nice_timeraisesNotImplementedError: Unsupported language. -
Add the date family. Two routes work.
-
Write
nice_date_<code>,nice_date_time_<code>,nice_day_<code>,nice_weekday_<code>,nice_month_<code>andnice_year_<code>. Add a branch for each to the matchingnice_*function in__init__.py, as Romanian and Hebrew do. -
Or write
ovos_date_parser/res/<code>/date_time.json. The generic engineDateTimeFormatreads it fornice_date,nice_date_time,nice_day,nice_weekday,nice_monthandnice_year. Copyres/en/date_time.jsonand translate it. The loader requires the keysdecade_format,hundreds_format,thousand_formatandyear_format.
-
-
Write
ovos_date_parser/res/<code>/date_words.jsonwith the nine keys ofres/en/date_words.json: one word for the moment of the call, and the singular and plural of second, minute, hour and day. The sharednice_durationandnice_relative_timeread it. Add anice_duration_<code>function and a branch innice_durationonly when the shared writer cannot say the language, as Polish and Russian need. -
Number words come from ovos-number-parser. Add the language there first when it lacks them.
Run the check from the first section again. Every line must read ok.
Test files sit in test/. Writers go in test/format_tests/test_format_<code>.py or test/test_dates_<code>.py. Cover:
nice_timefor the special minutes (00, 15, 30, 45), with and withoutuse_24houranduse_ampmnice_dateagainst the reference date: today, tomorrow and yesterdaynice_durationandnice_relative_time- a
NotImplementedErrorcheck for every form the language does not write
Take every expected string from a source and not from the output of the code. Never pin an unverified output as gold. A reader test belongs in chronologia. Here, only the routing test test/test_chronologia_routing.py runs over CHRONOLOGIA_LOCALES, and it picks up the code of your language by itself.
Add the language code to LANGS in test/test_lang_parity.py when it writes every function.
Then update the language lists in languages.md.