British English Spelling Hook
Automatically converts American English spellings to British English in code files. Context-aware -- handles code identifiers, comments, and strings differently. Perfect for UK-based teams enforcing consistent British spellings.
Hook Type
PostToolUse -- Runs after Claude writes or edits a file.
Description
This hook detects American English spellings in written files and converts them to British English. It is context-aware: it only transforms human-readable text (comments, strings, documentation) and leaves code identifiers unchanged when they follow API naming conventions.
Patterns/Rules
Common Conversions
| American | British |
|---|---|
| color | colour |
| favor | favour |
| organize | organise |
| analyze | analyse |
| center | centre |
| license (noun) | licence |
| catalog | catalogue |
| defense | defence |
| gray | grey |
| canceled | cancelled |
Context Rules
- Convert: Comments, docstrings, string literals, markdown content, error messages
- Do NOT convert: CSS property names (
color,background-color), API identifiers, imported library names, variable names matching external APIs - Filetype scope:
.ts,.js,.jsx,.tsx,.md,.mdx,.txt,.html,.css(comments only for CSS)
Configuration
Add to .claude/settings.json:
{ "hooks": { "PostToolUse": [ { "matcher": "(Write|Edit)", "hooks": [ { "type": "command", "command": "python3 .claude/hooks/britfix.py \"$CLAUDE_FILE_PATH\"" } ] } ] } }
Action
The hook script (.claude/hooks/britfix.py):
#!/usr/bin/env python3 """Convert American spellings to British English in text content.""" import sys import re from pathlib import Path REPLACEMENTS = { "color": "colour", "Color": "Colour", "favor": "favour", "Favor": "Favour", "favorite": "favourite", "Favorite": "Favourite", "organize": "organise", "Organize": "Organise", "recognize": "recognise", "Recognize": "Recognise", "analyze": "analyse", "Analyze": "Analyse", "center": "centre", "Center": "Centre", "theater": "theatre", "Theater": "Theatre", "defense": "defence", "Defense": "Defence", "offense": "offence", "Offense": "Offence", "gray": "grey", "Gray": "Grey", "canceled": "cancelled", "Canceled": "Cancelled", "catalog": "catalogue", "Catalog": "Catalogue", "dialog": "dialogue", "Dialog": "Dialogue", } # Skip these in code contexts CODE_EXCEPTIONS = {"color", "background-color", "border-color", "text-decoration-color"} def is_text_context(line: str, match_start: int) -> bool: """Check if match is in a comment or string, not code.""" prefix = line[:match_start] return bool(re.search(r'(//|/\*|#|\*|"|\'|`)', prefix)) def process_file(filepath: str) -> None: path = Path(filepath) if path.suffix not in {".ts", ".js", ".jsx", ".tsx", ".md", ".mdx", ".txt", ".html"}: return text = path.read_text() for american, british in REPLACEMENTS.items(): text = re.sub( rf'\b{re.escape(american)}\b', lambda m: british if is_text_context(m.string[m.start()-80:m.start()+80], 80) or path.suffix in {".md", ".mdx", ".txt"} else m.group(), text ) path.write_text(text) if __name__ == "__main__": if len(sys.argv) > 1: process_file(sys.argv[1])
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Pre-Commit Security Scanner
Pre-commit hook that scans staged files for hardcoded secrets, API keys, passwords, and sensitive data patterns before allowing commits.
Agents Md Watcher
Streamline your workflow with this automatically, loads, agents, configuration. Includes structured workflows, validation checks, and reusable patterns for automation.
Automated Build Inspector
Boost productivity using this automatically, trigger, build, processes. Includes structured workflows, validation checks, and reusable patterns for automation.