Skip to main content

LocalePrefixPattern

This class manages URL path matching and generation based on the current active language code. It provides functionality to determine the appropriate language prefix for a path, optionally omitting the prefix for the default language. The class is primarily used to handle localized URL patterns by matching incoming paths against dynamic language-specific prefixes.

Attributes

AttributeTypeDescription
prefix_default_languageboolean = TrueBoolean flag determining whether the URL prefix should be included for the site's default language.
convertersdict = {}A dictionary mapping path converter names to their respective instances, used for URL pattern matching.

Constructor

Signature

def LocalePrefixPattern(
prefix_default_language: boolean = True
) - > null

Parameters

NameTypeDescription
prefix_default_languageboolean = TrueDetermines whether the default language code should be included as a URL prefix.

Methods


regex()

@classmethod
def regex() - > re.Pattern

Compiles a regular expression pattern based on the current language prefix, primarily used for URL reversing and caching.

Returns

TypeDescription
re.PatternA compiled regular expression object matching the escaped language prefix string.

language_prefix()

@classmethod
def language_prefix() - > string

Determines the URL path prefix for the current active language, returning an empty string if the default language should not be prefixed.

Returns

TypeDescription
stringThe language code followed by a trailing slash, or an empty string based on localization settings.

match()

@classmethod
def match(
path: string
) - > tuple|null

Attempts to strip the language prefix from the beginning of a given URL path to facilitate internal routing.

Parameters

NameTypeDescription
pathstringThe full URL path string to be checked against the current language prefix.

Returns

TypeDescription
`tuplenull`

check()

@classmethod
def check() - > list

Performs a system check on the pattern configuration to identify potential issues.

Returns

TypeDescription
listAn empty list, as there are currently no validation errors defined for this pattern type.

describe()

@classmethod
def describe() - > string

Provides a human-readable string representation of the pattern for debugging or logging purposes.

Returns

TypeDescription
stringThe current language prefix wrapped in single quotes.