This document describes the automated system for detecting unused localization (L10n) strings in the Home Assistant iOS app.
The system consists of:
- Detection Script - Identifies unused L10n strings
- CI Check - Automatically detects unused strings in pull requests
Purpose: Identifies L10n strings that are not used anywhere in the codebase.
How it works:
- Parses
Sources/Shared/Resources/Swiftgen/Strings.swiftto extract all L10n properties - Reads all Swift source files once for efficient searching
- Checks for:
- Full L10n property path usage (e.g.,
L10n.About.title) - Leaf property usage (e.g.,
.title) - Direct Localizable key usage (e.g.,
"about.title")
- Full L10n property path usage (e.g.,
- Reports unused strings grouped by category
Usage:
python3 Tools/detect_unused_strings.pyExit Codes:
0: No unused strings found1: Unused strings detected (exits with 1 to enable workflow detection)
Purpose: Alert developers when unused strings are introduced or exist in PRs.
Job: check-unused-strings
- Runs on every pull request
- Executes the detection script
- Posts a sticky comment on the PR with results
- Non-blocking (informational only)
Comment Format:
- Shows count of unused strings
- Includes detailed list in collapsible section
The detection script uses a multi-step approach:
-
Parse Strings.swift: Extracts L10n enum structure using regex patterns
- Tracks enum nesting using a stack
- Identifies both properties and functions
- Maps L10n properties to Localizable keys
-
Efficient Code Search: Reads all Swift files once into memory
- Avoids multiple git grep calls
- Case-insensitive matching for robustness
- Checks multiple usage patterns
-
Multi-Pattern Matching:
- Full path:
L10n.About.title - Leaf property:
.title - Direct key:
"about.title"
- Full path:
- Double-checking: Both L10n usage and direct key usage are checked
- Regex precision: Handles escaped characters and complex string values
- Git visibility: All changes are visible for review
- Reversibility: Changes can be reverted before merging
- CI notification: Developers are informed about unused strings
To enhance the detection script:
- Edit
Tools/detect_unused_strings.py - Add new patterns to check in the
find_unused_stringsfunction - Test with:
python3 Tools/detect_unused_strings.py
To change when checks run:
- Edit
.github/workflows/ci.yml(PR checks)
- Check if the string is used in a way not covered by patterns
- Consider if the string is used in non-Swift files (storyboards, etc.)
- Verify the L10n property path is correctly parsed
- Verify the workflow file has correct YAML syntax
- Check GitHub Actions permissions
- Ensure Python setup-python action is available
Potential improvements:
- Automated removal of unused strings
- Support for Core.strings and Frontend.strings
- Integration with localization service (Lokalise)
- Support for detecting unused strings in other file types
- Performance optimizations for very large codebases
- SwiftGen: https://github.com/SwiftGen/SwiftGen
- Lokalise: Used for translation management
- GitHub Actions: https://docs.github.com/en/actions