betty.locale module

Provide the Locale API.

class betty.locale.Date[source]

Bases: LinkedDataDumpable

A (Gregorian) date.

Parameters:
__init__(year: int | None = None, month: int | None = None, day: int | None = None, fuzzy: bool = False)[source]
Parameters:
property comparable: bool

If this date is comparable to other dateys.

property complete: bool

Whether this date is complete.

async datey_dump_linked_data(dump: dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]], start_schema_org: str, end_schema_org: str) None[source]

Dump this instance to JSON-LD for a ‘datey’ field.

Parameters:
Return type:

None

day: int | None
async dump_linked_data(app: betty.app.App, schemas_org: list[str] | None = None) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Dump this instance to JSON-LD.

Parameters:
Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

fuzzy: bool
async classmethod linked_data_schema(app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Define the JSON Schema for betty.json.linked_data.LinkedDataDumpable.dump_linked_data().

Parameters:

app (betty.app.App)

Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

month: int | None
property parts: tuple[int | None, int | None, int | None]

The date parts: a 3-tuple of the year, month, and day.

to_range() betty.locale.DateRange[source]

Convert this date to a date range.

Return type:

betty.locale.DateRange

year: int | None
class betty.locale.DateRange[source]

Bases: LinkedDataDumpable

A date range can describe a period of time between, before, after, or around start and/or end dates.

Parameters:
__init__(start: betty.locale.Date | None = None, end: betty.locale.Date | None = None, start_is_boundary: bool = False, end_is_boundary: bool = False)[source]
Parameters:
property comparable: bool

If this date is comparable to other dateys.

async datey_dump_linked_data(dump: dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]], start_schema_org: str, end_schema_org: str) None[source]

Dump this instance to JSON-LD for a ‘datey’ field.

Parameters:
Return type:

None

async dump_linked_data(app: betty.app.App, start_schema_org: str | None = None, end_schema_org: str | None = None) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Dump this instance to JSON-LD.

Parameters:
Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

end: betty.locale.Date | None
end_is_boundary: bool
async classmethod linked_data_schema(app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Define the JSON Schema for betty.json.linked_data.LinkedDataDumpable.dump_linked_data().

Parameters:

app (betty.app.App)

Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

start: betty.locale.Date | None
start_is_boundary: bool
exception betty.locale.IncompleteDateError[source]

Bases: ValueError

Raised when a datey was unexpectedly incomplete.

exception betty.locale.LocaleNotFoundError[source]

Bases: RuntimeError

Raise when a locale could not be found.

Parameters:

locale (str)

__init__(locale: str) None[source]
Parameters:

locale (str)

class betty.locale.Localizable[source]

Bases: object

A localizable object.

Objects of this type can convert themselves to localized strings at the point of use.

localize(localizer: betty.locale.Localizer) str[source]

Localize self to a human-readable string.

Parameters:

localizer (betty.locale.Localizer)

Return type:

str

class betty.locale.Localized[source]

Bases: LinkedDataDumpable

A resource that is localized, e.g. contains information in a specific locale.

Parameters:
__init__(*args: Any, locale: str | None = None, **kwargs: Any)[source]
Parameters:
async dump_linked_data(app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Dump this instance to JSON-LD.

Parameters:

app (betty.app.App)

Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

async classmethod linked_data_schema(app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Define the JSON Schema for betty.json.linked_data.LinkedDataDumpable.dump_linked_data().

Parameters:

app (betty.app.App)

Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

locale: str | None
class betty.locale.Localizer[source]

Bases: object

Provide localization functionality for a specific locale.

Parameters:
__init__(locale: str, translations: gettext.NullTranslations)[source]
Parameters:
format_date(date: betty.locale.Date) str[source]

Format a date to a human-readable string.

Parameters:

date (betty.locale.Date)

Return type:

str

format_date_range(date_range: betty.locale.DateRange) str[source]

Format a date range to a human-readable string.

Parameters:

date_range (betty.locale.DateRange)

Return type:

str

format_datetime_datetime(datetime_datetime: datetime.datetime) str[source]

Format a datetime date to a human-readable string.

Parameters:

datetime_datetime (datetime.datetime)

Return type:

str

format_datey(date: betty.locale.Date | betty.locale.DateRange) str[source]

Format a datey value into a human-readable string.

Parameters:

date (betty.locale.Date | betty.locale.DateRange)

Return type:

str

gettext(message: str) str[source]

Like gettext.gettext().

Arguments are identical to those of gettext.gettext().

Parameters:

message (str)

Return type:

str

property locale: str

The locale.

ngettext(message_singular: str, message_plural: str, n: int) str[source]

Like gettext.ngettext().

Arguments are identical to those of gettext.ngettext().

Parameters:
  • message_singular (str)

  • message_plural (str)

  • n (int)

Return type:

str

npgettext(context: str, message_singular: str, message_plural: str, n: int) str[source]

Like gettext.npgettext().

Arguments are identical to those of gettext.npgettext().

Parameters:
  • context (str)

  • message_singular (str)

  • message_plural (str)

  • n (int)

Return type:

str

pgettext(context: str, message: str) str[source]

Like gettext.pgettext().

Arguments are identical to those of gettext.pgettext().

Parameters:
Return type:

str

class betty.locale.LocalizerRepository[source]

Bases: object

Exposes the available localizers.

Parameters:

assets (betty.fs.FileSystem)

__init__(assets: betty.fs.FileSystem)[source]
Parameters:

assets (betty.fs.FileSystem)

async coverage(locale: str | babel.core.Locale) tuple[int, int][source]

Get the translation coverage for the given locale.

Return type:

tuple[int, int]

Returns:

A 2-tuple of the number of available translations and the number of translatable source strings.

Parameters:

locale (str | babel.core.Locale)

async get(locale: str | babel.core.Locale) betty.locale.Localizer[source]

Get the localizer for the given locale.

Parameters:

locale (str | babel.core.Locale)

Return type:

betty.locale.Localizer

async get_negotiated(*preferred_locales: str) betty.locale.Localizer[source]

Get the best matching available locale for the given preferred locales.

Parameters:

preferred_locales (str)

Return type:

betty.locale.Localizer

property locales: Iterator[str]

The available locales.

class betty.locale.Str[source]

Bases: Localizable

Create new localizable strings.

classmethod call(call: Callable[[betty.locale.Localizer], str]) betty.locale.Str[source]

Create a new localizable that outputs the callable’s return value.

Parameters:

call (typing.Callable[[betty.locale.Localizer], str])

Return type:

betty.locale.Str

classmethod gettext(message: str, **format_kwargs: str | betty.locale.Localizable) betty.locale.Str[source]

Like gettext.gettext().

Positional arguments are identical to those of gettext.gettext(). Keyword arguments are identical to those of :py:met:`str.format`, except that any betty.locale.Localizable will be localized before string formatting.

Parameters:
Return type:

betty.locale.Str

classmethod ngettext(message_singular: str, message_plural: str, n: int, **format_kwargs: str | betty.locale.Localizable) betty.locale.Str[source]

Like gettext.ngettext().

Positional arguments are identical to those of gettext.ngettext(). Keyword arguments are identical to those of :py:met:`str.format`, except that any betty.locale.Localizable will be localized before string formatting.

Parameters:
Return type:

betty.locale.Str

classmethod npgettext(context: str, message_singular: str, message_plural: str, n: int, **format_kwargs: str | betty.locale.Localizable) betty.locale.Str[source]

Like gettext.npgettext().

Positional arguments are identical to those of gettext.npgettext(). Keyword arguments are identical to those of :py:met:`str.format`, except that any betty.locale.Localizable will be localized before string formatting.

Parameters:
Return type:

betty.locale.Str

classmethod pgettext(context: str, message: str, **format_kwargs: str | betty.locale.Localizable) betty.locale.Str[source]

Like gettext.pgettext().

Positional arguments are identical to those of gettext.pgettext(). Keyword arguments are identical to those of :py:met:`str.format`, except that any betty.locale.Localizable will be localized before string formatting.

Parameters:
Return type:

betty.locale.Str

classmethod plain(plain: Any, **format_kwargs: str | betty.locale.Localizable) betty.locale.Str[source]

Create a new localizable that outputs the given plain text string.

Keyword arguments are identical to those of :py:met:`str.format`, except that any betty.locale.Localizable will be localized before string formatting.

Parameters:
Return type:

betty.locale.Str

betty.locale.get_data(locale: str | babel.core.Locale) babel.core.Locale[source]

Get locale metadata.

Parameters:

locale (str | babel.core.Locale)

Return type:

babel.core.Locale

betty.locale.get_display_name(locale: str | babel.core.Locale, display_locale: str | babel.core.Locale | None = None) str | None[source]

Return a locale’s human-readable display name.

Parameters:
Return type:

str | None

async betty.locale.init_translation(locale: str) None[source]

Initialize a new translation.

Parameters:

locale (str)

Return type:

None

betty.locale.negotiate_locale(preferred_locales: str | babel.core.Locale | Sequence[str | babel.core.Locale], available_locales: Sequence[str | babel.core.Locale]) babel.core.Locale | None[source]

Negotiate the preferred locale from a sequence.

Parameters:
Return type:

babel.core.Locale | None

betty.locale.negotiate_localizeds(preferred_locales: str | babel.core.Locale | Sequence[str | babel.core.Locale], localizeds: Sequence[betty.locale.Localized]) betty.locale.Localized | None[source]

Negotiate the preferred localized value from a sequence.

Parameters:
Return type:

betty.locale.Localized | None

async betty.locale.ref_date(root_schema: dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]], app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Reference the Date schema.

Parameters:
Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

async betty.locale.ref_date_range(root_schema: dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]], app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Reference the DateRange schema.

Parameters:
Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

async betty.locale.ref_datey(root_schema: dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]], app: betty.app.App) dict[str, bool | int | float | str | None | Sequence[bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]] | Mapping[str, bool | int | float | str | None | Sequence[Dump] | Mapping[str, Dump]]][source]

Reference the Datey schema.

Parameters:
Return type:

dict[str, typing.Union[bool, int, float, str, None, typing.Sequence[typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]], typing.Mapping[str, typing.Union[bool, int, float, str, None, typing.Sequence[Dump], typing.Mapping[str, Dump]]]]]

async betty.locale.run_babel(*args: str) None[source]

Run a Babel Command Line Interface (CLI) command.

Parameters:

args (str)

Return type:

None

betty.locale.to_babel_identifier(locale: str | babel.core.Locale) str[source]

Convert a locale or locale metadata to a Babel locale identifier.

Parameters:

locale (str | babel.core.Locale)

Return type:

str

betty.locale.to_locale(locale: str | babel.core.Locale) str[source]

Ensure that a locale or locale metadata is a locale.

Parameters:

locale (str | babel.core.Locale)

Return type:

str

async betty.locale.update_translations(_output_assets_directory_path: pathlib.Path = PosixPath('/home/runner/work/betty/betty/betty/assets')) None[source]

Update all existing translations based on changes in translatable strings.

Parameters:

_output_assets_directory_path (pathlib.Path)

Return type:

None