Skip to content

Commit

Permalink
data: add a new field, locale_catalog.phone_format (rendercv#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinaatalay committed Jul 19, 2024
1 parent a3fea3a commit 7e20bae
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
12 changes: 8 additions & 4 deletions docs/user_guide/structure_of_the_yaml_input_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,18 @@ cv:
name: John Doe
location: Your Location
email: youremail@yourdomain.com
phone: +905419999999
phone: +905419999999 # (1)!
website: https://example.com/
social_networks:
- network: LinkedIn # (1)!
- network: LinkedIn # (2)!
username: yourusername
- network: GitHub
username: yourusername
...
```

1. The available social networks are: {{available_social_networks}}.
1. If you want to change the phone number formatting in the output, see the `locale_catalog` field's `phone_number_format` key.
2. The available social networks are: {{available_social_networks}}.

None of the values above are required. You can omit any or all of them, and RenderCV will adapt to your input. These generic fields are used in the header of the CV.

Expand Down Expand Up @@ -287,12 +288,13 @@ design:

## "`locale_catalog`" field

This field is what makes RenderCV a multilingual tool. RenderCV uses some English strings to render PDFs. For example, it takes the dates in ISO format (`2020-01-01`) and converts them into human-friendly strings (`"Jan 2020"`). However, you can override these strings for your own language or needs with the `locale_catalog` field.
This field is what makes RenderCV a multilingual tool. RenderCV uses some English strings to render PDFs. For example, it takes the dates in ISO format (`2020-01-01`) and converts them into human-friendly strings (`"Jan 2020"`). However, you can override these strings for your own language or needs with the `locale_catalog` field. Also, you can change the phone number formatting with the `phone_number_format` key.

Here is an example:

```yaml
locale_catalog:
phone_number_format: national # (1)!
abbreviations_for_months: # translation of the month abbreviations
- Jan
- Feb
Expand Down Expand Up @@ -326,3 +328,5 @@ locale_catalog:
present: present # translation of the word "present"
to: to # translation of the word "to"
```

1. The available phone number formats are: `national`, `international`, and `E164`.
32 changes: 32 additions & 0 deletions rendercv/data/models/computers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,41 @@
from datetime import date as Date
from typing import Optional

import phonenumbers

from .locale_catalog import locale_catalog


def format_phone_number(phone_number: str) -> str:
"""Format a phone number to the format specified in the `locale_catalog` dictionary.
Example:
```python
format_phone_number("+17034800500")
```
returns
```python
"(703) 480-0500"
```
Args:
phone_number (str): The phone number to format.
Returns:
str: The formatted phone number.
"""

format = locale_catalog["phone_format"].upper() # type: ignore

phonenumbers.PhoneNumberFormat.E164

parsed_number = phonenumbers.parse(phone_number, None)
formatted_number = phonenumbers.format_number(
parsed_number, getattr(phonenumbers.PhoneNumberFormat, format)
)
return formatted_number


def format_date(date: Date, use_full_name: bool = False) -> 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
Expand Down
6 changes: 3 additions & 3 deletions rendercv/data/models/curriculum_vitae.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ class CurriculumVitae(RenderCVBaseModelWithExtraKeys):
phone: Optional[pydantic_phone_numbers.PhoneNumber] = pydantic.Field(
default=None,
title="Phone",
description="The phone number of the person.",
description="The phone number of the person, including the country code.",
)
website: Optional[pydantic.HttpUrl] = pydantic.Field(
default=None,
Expand Down Expand Up @@ -455,11 +455,11 @@ def connections(self) -> list[dict[str, Optional[str]]]:
)

if self.phone is not None:
phone_placeholder = self.phone.replace("tel:", "").replace("-", " ")
phone_placeholder = computers.format_phone_number(self.phone)
connections.append(
{
"latex_icon": "\\faPhone*",
"url": f"{self.phone}",
"url": self.phone,
"clean_url": phone_placeholder,
"placeholder": phone_placeholder,
}
Expand Down
15 changes: 14 additions & 1 deletion rendercv/data/models/locale_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
`locale_catalog` field of the input file.
"""

from typing import Annotated, Optional
from typing import Annotated, Literal, Optional

import annotated_types as at
import pydantic
Expand All @@ -16,6 +16,18 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
updates the `locale_catalog` dictionary.
"""

phone_format: Optional[Literal["national", "international", "E164"]] = (
pydantic.Field(
default="national",
title="Phone Number Format",
description=(
"If 'national', phone numbers are formatted without the country code."
" If 'international', phone numbers are formatted with the country"
" code. The default value is 'national'."
),
validate_default=True, # To initialize the locale catalog with the default
)
)
month: Optional[str] = pydantic.Field(
default="month",
title='Translation of "Month"',
Expand Down Expand Up @@ -109,6 +121,7 @@ class LocaleCatalog(RenderCVBaseModelWithoutExtraKeys):
"abbreviations_for_months",
"to",
"full_names_of_months",
"phone_format",
)
@classmethod
def update_translations(cls, value: str, info: pydantic.ValidationInfo) -> str:
Expand Down
1 change: 1 addition & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,7 @@ def test_locale_catalog():
"11",
"12",
],
phone_format="international",
)

assert locale_catalog.locale_catalog == data_model.locale_catalog.model_dump()
Expand Down

0 comments on commit 7e20bae

Please sign in to comment.