Global-ready Django framework
GeoCanon
Geographic canon.
Give your Django application a consistent foundation for location and locale data: 196 jurisdictions, each with a flag class and timezone. Dial codes cover 167 of them and default languages cover 191. Public holidays are optional.
pip install GeoCanon
import django
from django.conf import settings
settings.configure(USE_I18N=True)
django.setup()
from geo_canon import (
get_dial_code,
get_flag_css_class,
get_language_for_jurisdiction,
get_timezone_name,
)
for country in ["Romania", "Japan", "Brazil", "Kenya", "United Kingdom"]:
dial = get_dial_code(country)
flag = get_flag_css_class(country)
lang = get_language_for_jurisdiction(country)
zone = get_timezone_name(country)
print(f"{country:<15}{dial:<6}{flag:<7}{lang:<4}{zone}")python lookup.py
Romania +40 fi-ro ro Europe/Bucharest Japan +81 fi-jp ja Asia/Tokyo Brazil +55 fi-br pt America/Sao_Paulo Kenya +254 fi-ke sw Africa/Nairobi United Kingdom +44 fi-gb en Europe/London
import django
from django.conf import settings
settings.configure(USE_I18N=True)
django.setup()
from django.core.exceptions import ValidationError
from geo_canon import JURISDICTIONS, LANGUAGE_CHOICES, TIMEZONE_MAP
from geo_canon.jurisdictions.forms import JurisdictionChoiceField
print("jurisdictions:", len(JURISDICTIONS))
print("first three:", [key for key, _ in JURISDICTIONS[:3]])
print("timezone mappings:", len(TIMEZONE_MAP))
print("language choices:", len(LANGUAGE_CHOICES))
field = JurisdictionChoiceField()
print("clean('Romania') ->", field.clean("Romania"))
try:
field.clean("Atlantis")
except ValidationError as exc:
print("clean('Atlantis') ->", exc.messages[0])python coverage.py
jurisdictions: 196
first three: ['Afghanistan', 'Albania', 'Algeria']
timezone mappings: 197
language choices: 73
clean('Romania') -> Romania
clean('Atlantis') -> Select a valid choice. Atlantis is not one of the available choices.import django
from django.conf import settings
settings.configure(USE_I18N=True)
django.setup()
from geo_canon import (
TimezoneNotFoundError,
format_duration,
get_country_by_iso,
get_timezone_name,
validate_phone,
)
print(validate_phone("7123456789", "+44")) # UK mobile, national digits
print(validate_phone("712345", "+44")) # too short
print(get_country_by_iso("RO"))
print(format_duration(3661))
try:
get_timezone_name("Atlantis")
except TimezoneNotFoundError as exc:
print(str(exc).splitlines()[0]) # first line of the errorpython validate.py
True False Romania 1 hour, 1 minute, 1 second No timezone configured for jurisdiction: Atlantis
Each window shows a snippet exactly as it was run and the output it printed, using the version published on PyPI.
01Before and after
The localisation problem
Stop rebuilding locale infrastructure for every project. One canonical source of truth for geographic data.
Scattered data
Country lists hardcoded in templates. Timezone logic scattered across views. Dial codes copy-pasted from Stack Overflow. Every project reinvents the wheel.
COUNTRY_CHOICES = [
('US', 'United States'),
('UK', 'United Kingdom'),
# ... copy-paste 190 more?
# Which ISO standard?
# Are they translatable?
# What about dial codes?
]
GeoCanon
One import. Canonical, typed, translatable data for 196 jurisdictions. Forms, middleware, and settings included.
from geo_canon import (
get_jurisdiction_choices,
get_dial_code,
get_flag_css_class,
get_current_local_time,
)
# 196 jurisdictions, typed & translatable
# Dial codes, flags, timezones — all linked
02Modules
Seven modules.
Core locale data for a Django application with a global user base, in one package.
Jurisdictions
196 jurisdictions with translatable names, form fields, and context-switching middleware.
- JurisdictionChoiceField for forms
- Session-based middleware
- Validation helpers
Dial codes
177 international dial-code choices, a country-to-code mapping for 167 of the 196 jurisdictions and 173 per-code phone validation patterns.
- Country-to-code mapping
- Regex phone validation
- Django form choices
Flags
Country-to-ISO-3166 CSS class mapping compatible with the flag-icons library.
- 197 flag-icons CSS classes
- Flag annotation helper for lists of dicts
- Reverse ISO lookup
Timezones
Every one of the 196 jurisdictions mapped to an IANA timezone (one primary zone per country), with aware-datetime helpers.
- zoneinfo + pytz fallback
- Settings-level overrides
get_current_local_time()
Public holidays
Public-holiday checks for 143 of the 196 jurisdictions, through the open-source holidays library.
is_public_holiday()check- Full year holiday listing
- Optional dependency
Languages
73 ISO 639-1 language choices, with a default language for 191 of 196 jurisdictions.
- ISO 639-1 language codes
- Jurisdiction-to-default-language lookup
- Sorted form choices
Durations, reverse lookup and optional extras
Durations
Readable durations
format_duration(3661) returns "1 hour, 1 minute, 1 second", with translatable unit names.
One of seven modules shipped in 0.1.0.
Reverse lookup
ISO code to jurisdiction
get_country_by_iso("RO") returns the canonical jurisdiction name from an ISO 3166-1 alpha-2 code, so imported data joins the same list your forms use.
An unknown code returns None instead of raising.
Optional extras
Holidays and timezone fallback
geocanon[holidays] adds public-holiday checks through the holidays library. geocanon[timezones] adds a pytz fallback. geocanon[all] installs both.
Core imports need only Django.
03Developer experience
Designed for global scale
Stop copy-pasting country lists. GeoCanon provides versioned, typed, translatable geographic defaults with Django-native integration.
-
Rich exceptions
Errors carry an actionable hint.
-
Pydantic settings
Type-safe configuration with validation. Works with or without Pydantic installed.
-
PEP 561 typed
Full py.typed support for MyPy and VS Code autocompletion out of the box.
from geo_canon import (
get_current_local_time,
get_flag_css_class,
is_public_holiday,
)
def dashboard(request):
jurisdiction = request.jurisdiction
now = get_current_local_time(jurisdiction)
flag = get_flag_css_class(jurisdiction)
holiday = is_public_holiday(jurisdiction)
# All data — one canonical source
04Installation
Integration
Install from PyPI, set defaults and the middleware in settings.py, then call the helpers anywhere.
-
01Install the package
pip install "geocanon[all]" -
02Configure in settings.py
# settings.py GEOCANON = { "default_jurisdiction": "United Kingdom", "default_language": "en", "enable_form_widgets": True, } MIDDLEWARE = [ # ... "geo_canon.jurisdictions.middleware.JurisdictionMiddleware", ] -
03Use anywhere in your app
from geo_canon.jurisdictions.forms import JurisdictionContextSwitchForm from geo_canon.timezones import get_current_local_time # requires: pip install "geocanon[holidays]" from geo_canon.holidays import is_public_holiday from geo_canon.flags import get_flag_css_class # request.jurisdiction is set automatically by the middleware local_time = get_current_local_time(request.jurisdiction) flag_class = get_flag_css_class(request.jurisdiction)
05Global operations playbook
Design once, localize everywhere
GeoCanon keeps jurisdiction data in one versioned package. Forms, billing, reporting and support tools can then share the same list instead of drifting apart.
Product teams
Ship locale-aware defaults for language, timezone and holidays without hardcoding market-specific branches.
Operations teams
Coordinate support windows and cutoff times from local timezone truth instead of manually maintained spreadsheets.
Compliance teams
Track jurisdiction-level policy requirements against one consistent set of jurisdiction names.
GeoCanon supplies identifiers and defaults, not legal or policy decisions.
Design goals
- One shared jurisdiction list across apps and services.
- Locale constants maintained in one package instead of in every project.
- Translatable names, so the same list works in every language your app supports.
- Typed, documented helpers that are easy to test.
GeoCanon
Read the design paper
The paper sets out how GeoCanon models geographic data as one shared layer, request-scoped jurisdiction context, update governance, and what canonical data cannot decide.