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
| Attribute | Type | Description |
|---|---|---|
| prefix_default_language | boolean = True | Boolean flag determining whether the URL prefix should be included for the site's default language. |
| converters | dict = {} | 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
| Name | Type | Description |
|---|---|---|
| prefix_default_language | boolean = True | Determines 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
| Type | Description |
|---|---|
re.Pattern | A 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
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| path | string | The full URL path string to be checked against the current language prefix. |
Returns
| Type | Description |
|---|---|
| `tuple | null` |
check()
@classmethod
def check() - > list
Performs a system check on the pattern configuration to identify potential issues.
Returns
| Type | Description |
|---|---|
list | An 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
| Type | Description |
|---|---|
string | The current language prefix wrapped in single quotes. |