From 42dd88c025a163f888124f0d3260ffcb70366de5 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Tue, 23 Jul 2024 18:13:59 +0300 Subject: [PATCH] data: add a new field, `locale_catalog.date_style` --- rendercv/data/models/computers.py | 29 ++++++++++++++++++-------- rendercv/data/models/locale_catalog.py | 13 ++++++++++++ rendercv/renderer/templater.py | 2 +- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/rendercv/data/models/computers.py b/rendercv/data/models/computers.py index 6e98b07b..7195e6c6 100644 --- a/rendercv/data/models/computers.py +++ b/rendercv/data/models/computers.py @@ -41,7 +41,7 @@ def format_phone_number(phone_number: str) -> str: return formatted_number -def format_date(date: Date, use_full_name: bool = False) -> str: +def format_date(date: Date, date_style: Optional[str] = None) -> str: """Formats a `Date` object to a string in the following format: "Jan 2021". The month names are taken from the `locale_catalog` dictionary from the `rendercv.data_models.models` module. @@ -56,21 +56,32 @@ def format_date(date: Date, use_full_name: bool = False) -> str: Args: date (Date): The date to format. - use_full_name (bool, optional): If `True`, the full name of the month will be - used. Defaults to `False`. + date_style (Optional[str]): The style of the date string. If not provided, the + default date style from the `locale_catalog` dictionary will be used. Returns: str: The formatted date. """ - if use_full_name: - month_names = locale_catalog["full_names_of_months"] - else: - month_names = locale_catalog["abbreviations_for_months"] + full_month_names = locale_catalog["full_names_of_months"] + short_month_names = locale_catalog["abbreviations_for_months"] month = int(date.strftime("%m")) - month_abbreviation = month_names[month - 1] year = date.strftime(format="%Y") - date_string = f"{month_abbreviation} {year}" + + placeholders = { + "FULL_MONTH_NAME": full_month_names[month - 1], + "MONTH_ABBREVIATION": short_month_names[month - 1], + "MONTH": str(month), + "MONTH_IN_TWO_DIGITS": f"{month:02d}", + "YEAR": year, + "YEAR_IN_TWO_DIGITS": year[-2:], + } + translator = str.maketrans(placeholders) + + if date_style is None: + date_style = locale_catalog["date_style"] # type: ignore + + date_string = date_style.translate(translator) # type: ignore return date_string diff --git a/rendercv/data/models/locale_catalog.py b/rendercv/data/models/locale_catalog.py index c2beabea..1308625a 100644 --- a/rendercv/data/models/locale_catalog.py +++ b/rendercv/data/models/locale_catalog.py @@ -111,6 +111,18 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys): description="Full names of the months in the locale.", validate_default=True, # to initialize the locale catalog with the default values ) + date_style: Optional[str] = pydantic.Field( + default="MONTH_ABBREVIATION YEAR", + title="Date Style", + description=( + "The style of the date. The following placeholder can be used:\n-" + " FULL_MONTH_NAME: Full name of the month\n- MONTH_ABBREVIATION:" + " Abbreviation of the month\n- MONTH: Month as a number\n-" + " MONTH_IN_TWO_DIGITS: Month as a number in two digits\n- YEAR: Year as a" + " number\n- YEAR_IN_TWO_DIGITS: Year as a number in two digits\nThe default" + ' value is "MONTH_ABBREVIATION YEAR".' + ), + ) @pydantic.field_validator( "month", @@ -122,6 +134,7 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys): "to", "full_names_of_months", "phone_number_format", + "date_style", ) @classmethod def update_translations(cls, value: str, info: pydantic.ValidationInfo) -> str: diff --git a/rendercv/renderer/templater.py b/rendercv/renderer/templater.py index ab22eb75..c9e8a579 100644 --- a/rendercv/renderer/templater.py +++ b/rendercv/renderer/templater.py @@ -75,7 +75,7 @@ class TemplatedFile: cv=self.cv, design=self.design, entry=entry, - today=data.format_date(Date.today(), use_full_name=True), + today=data.format_date(Date.today(), date_style="FULL_MONTH_NAME YEAR"), **kwargs, )