B

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.

HookCommunitydevelopmentv1.0.0MIT
0 views0 copies

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

AmericanBritish
colorcolour
favorfavour
organizeorganise
analyzeanalyse
centercentre
license (noun)licence
catalogcatalogue
defensedefence
graygrey
canceledcancelled

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])
Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates