From c8f33e955349eb94527a3386599eba19b7172efd Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Thu, 20 Jun 2024 15:55:45 +0300 Subject: [PATCH] renderer: localize TODAY placeholder in `last_updated_date_style` (#111) --- .../structure_of_the_yaml_input_file.md | 13 ++++++ rendercv/data_models.py | 41 +++++++++++++++++-- rendercv/renderer.py | 2 +- tests/test_data_models.py | 14 +++++++ 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/docs/user_guide/structure_of_the_yaml_input_file.md b/docs/user_guide/structure_of_the_yaml_input_file.md index 15dce9f..b2e152f 100644 --- a/docs/user_guide/structure_of_the_yaml_input_file.md +++ b/docs/user_guide/structure_of_the_yaml_input_file.md @@ -289,6 +289,19 @@ locale_catalog: - Oct - Nov - Dec + full_names_of_months: # translation of the full month names + - January + - February + - March + - April + - May + - June + - July + - August + - September + - October + - November + - December month: month # translation of the word "month" months: months # translation of the word "months" year: year # translation of the word "year" diff --git a/rendercv/data_models.py b/rendercv/data_models.py index 9304097..22af173 100644 --- a/rendercv/data_models.py +++ b/rendercv/data_models.py @@ -75,7 +75,7 @@ def get_date_object(date: str | int) -> Date: return date_object -def format_date(date: Date) -> str: +def format_date(date: Date, use_full_name: bool = False) -> str: """Formats a `Date` object to a string in the following format: "Jan. 2021". Example: @@ -88,16 +88,21 @@ def format_date(date: Date) -> 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`. Returns: str: The formatted date. """ # Month abbreviations, # taken from: https://web.library.yale.edu/cataloging/months - abbreviations_of_months = locale_catalog["abbreviations_for_months"] + if use_full_name: + month_names = locale_catalog["full_names_of_months"] + else: + month_names = locale_catalog["abbreviations_for_months"] month = int(date.strftime("%m")) - month_abbreviation = abbreviations_of_months[month - 1] + month_abbreviation = month_names[month - 1] year = date.strftime(format="%Y") date_string = f"{month_abbreviation} {year}" @@ -1110,9 +1115,37 @@ class LocaleCatalog(RenderCVBaseModel): description="Abbreviations of the months in the locale.", validate_default=True, # to initialize the locale catalog with the default values ) + full_names_of_months: Optional[ + Annotated[list[str], at.Len(min_length=12, max_length=12)] + ] = pydantic.Field( + default=[ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ], + title="Full Names of Months", + description="Full names of the months in the locale.", + validate_default=True, # to initialize the locale catalog with the default values + ) @pydantic.field_validator( - "month", "months", "year", "years", "present", "abbreviations_for_months", "to" + "month", + "months", + "year", + "years", + "present", + "abbreviations_for_months", + "to", + "full_names_of_months", ) @classmethod def update_translations(cls, value: str, info: pydantic.ValidationInfo) -> str: diff --git a/rendercv/renderer.py b/rendercv/renderer.py index 9872ace..4124c66 100644 --- a/rendercv/renderer.py +++ b/rendercv/renderer.py @@ -87,7 +87,7 @@ class TemplatedFile: cv=self.cv, design=self.design, entry=entry, - today=Date.today().strftime("%B %Y"), + today=dm.format_date(Date.today(), use_full_name=True), **kwargs, ) diff --git a/tests/test_data_models.py b/tests/test_data_models.py index fae36d8..f4d127c 100644 --- a/tests/test_data_models.py +++ b/tests/test_data_models.py @@ -690,6 +690,20 @@ def test_locale_catalog(): "11", "12", ], + full_names_of_months=[ + "1", + "2", + "3", + "4", + "5", + "6", + "7", + "8", + "9", + "10", + "11", + "12", + ], ) assert dm.locale_catalog == data_model.locale_catalog.model_dump()