Laravel Code Scanner — Find Hardcoded Strings
The Code Scanner analyzes hardcoded text in your Laravel application and helps you convert them into proper translation keys, streamlining the internationalization (i18n) process. It automates the process of finding translatable strings, creating translations, and updating your source code.
⚠️ Important: Run Scanner Commands Locally
The Code Scanner commands (
translator:scanandtranslator:apply) should be executed in your local development environment, not on production servers or serverless deployments.Why?
- The
applycommand modifies your source files directly, replacing hardcoded text with__()calls- You need to review the changes with
git diffbefore committing- You can revert invalid changes with
git checkoutif needed- Running on a server would require SSH access, file permissions, and complicate version control
Recommended workflow:
- Run
translator:scanlocally to detect hardcoded strings- Review and approve suggestions in LangSyncer (web interface)
- Run
translator:applylocally to update your source files- Review changes:
git diff- Test your application
- Commit and push:
git add . && git commit -m "Apply translations"
How It Works
The Code Scanner workflow has three main phases:
- Scan - Run the CLI command to find hardcoded text in your codebase
- Review - Approve, edit, or reject suggestions in LangSyncer
- Apply - Update your source files with translation function calls
Phase 1: Scanning Your Code
Running a Scan
From your Laravel project directory, run:
php artisan translator:scan
What Gets Detected
The scanner finds hardcoded text in .blade.php and .php files, including Livewire Volt single-file components, where it reads both the PHP block and the template:
- HTML content in tags (headings, buttons, paragraphs)
- Translatable HTML attributes:
placeholder,title,alt,aria-label,aria-descriptionandlabel - String literals in PHP code
- Exception messages
Examples:
// HTML content
<h1>Welcome to our app</h1>
<button>Submit</button>
// Attributes
<input placeholder="Enter your email">
<img alt="Profile picture">
// PHP strings
throw new Exception('Invalid credentials');
What the Scanner Skips
Most of a Blade file is not user-facing text, so the scanner ignores strings that are almost certainly technical:
- Text already wrapped in
__(),trans(),trans_choice(),@langor@choice - Technical attributes:
class,id,name,type,href,src,action,method,rel,target - Alpine.js, Livewire and Blade bindings:
x-*,wire:*,@click,:classand other:-prefixed attributes - Arguments of
route(),url(),asset(),vite(),config(),env()and the path helpers - View and component names in
@extends,@include,@livewire,@section,@pushand@slot - URLs,
mailto:andtel:links, colour codes, numbers and JSON-like strings - Single lowercase words without spaces, such as
primaryoruser_id, which are almost always identifiers or CSS classes
The last rule has a side effect worth knowing: a one-word label written in lowercase, like <span>save</span>, is skipped. Capitalised labels such as Save are detected. If your design system lowercases labels in markup, search for them separately before you rely on the scan.
Scanning Several Projects or Packages
Each entry in the projects array of config/translator-client.php has its own scan_paths, so one application can scan its own views and a package's views separately and send each set of suggestions to the right LangSyncer project:
'projects' => [
[
'name' => 'main',
'api_key' => env('CLI_TRANSLATOR_API_KEY'),
'path' => resource_path('lang'),
'scan_paths' => ['resources/views', 'app'],
],
],
# List the configured projects
php artisan translator:scan --list-projects
# Scan one of them
php artisan translator:scan --project=main
vendor, node_modules, storage and bootstrap/cache are always excluded.
CLI Scanner or Free Blade Scanner?
translator:scan |
Free Blade scanner | |
|---|---|---|
| Input | Whole project, Blade and PHP files | One pasted Blade template |
| Account needed | Yes | No |
| Review queue in LangSyncer | Yes | No |
| Rewrites your files | Yes, with translator:apply |
No, you copy the result |
| AI key suggestions | Optional, with --ai |
No |
The free tool is a quick way to check one template or to see what the scanner flags before you connect a project. The CLI scanner is the one to use for a real codebase.
Scan Options
# Scan specific paths
php artisan translator:scan --path=resources/views --path=app/Http
# Exclude directories
php artisan translator:scan --exclude=tests --exclude=database
# Preview without sending to LangSyncer
php artisan translator:scan --dry-run
# Use AI to enhance key suggestions
php artisan translator:scan --ai
# Filter by minimum confidence (0-100)
php artisan translator:scan --min-confidence=50
# Output format
php artisan translator:scan --format=table
php artisan translator:scan --format=json
php artisan translator:scan --format=csv
# Save the results to a file
php artisan translator:scan --dry-run --format=csv --output=storage/scan.csv
# Skip confirmation prompts (CI and scripts)
php artisan translator:scan --yes
A dry run with --format=csv --output=... is a cheap way to audit a codebase before you start: no suggestions are sent to the review queue, no quota is used unless you add --ai, and the file can be shared with whoever will review the strings. On plans without the Code Scanner, the command shows a short preview of the first results instead and does not write the file; see Subscription Plans for which plans include it.
Understanding Confidence Scores
Each detected text receives a confidence score:
| Score | Level | Description |
|---|---|---|
| 80-100% | High | Safe to approve, clear translation candidate |
| 50-79% | Medium | Review recommended |
| 0-49% | Low | Careful review required |
Factors that increase confidence:
- Known element types (headings, buttons, labels)
- Appropriate text length (5-80 characters)
- User-facing file locations (views, components)
AI-Enhanced Analysis
Adding the --ai flag uses AI to generate significantly better suggestions:
php artisan translator:scan --ai
Example comparison:
| Original Text | Without --ai |
With --ai |
|---|---|---|
"You did it John!" |
Key: you-did-it-john |
Key: congratulations |
Value: You did it John! |
Value: You did it, :name! |
|
"Hola María!" |
Key: hola-maria |
Key: greeting |
Value: Hola María! |
Value: Hola, :name! |
AI analysis benefits:
- Semantic keys - Meaningful names like
congratulationsinstead of literalyou-did-it-john - Detects dynamic content - Identifies names, numbers, dates and converts them to placeholders (
:name,:count) - Consistent language - Keys generated in English even for non-English text
- Cleaner values - Removes hardcoded data, adds proper placeholders
Cost: 1 quota unit per candidate. See Quota Management for details on tracking and purchasing quota.
Phase 2: Reviewing Suggestions
After scanning, review the suggestions in LangSyncer.
Accessing the Review Page
- Go to Code Scanner in the sidebar
- Select your project
- Review pending suggestions
Review Interface
Each suggestion shows:
- Original text - The hardcoded string found
- Suggested key - Proposed translation key (e.g.,
auth.buttons.login) - Confidence - Color-coded badge
- Locations - Files where this text appears
Available Actions
| Action | Description |
|---|---|
| Approve | Accept the suggestion as-is and create the translation |
| Edit | Open modal to modify the key or value |
| Reject | Dismiss the suggestion |
Editing Suggestions
Click Edit to open the edit modal where you can:
- Change the suggested key
- Modify the translation value
- Add placeholders for dynamic content
The modal offers two save options:
- Save - Save changes but keep the review pending for later approval
- Save & Approve - Save changes and approve immediately
Batch Actions
For efficiency:
- Approve Selected - Approve multiple checked items
- Reject Selected - Reject multiple checked items
- Approve All High Confidence - Auto-approve all 80%+ confidence items
Filtering
Filter the review list by:
- Status - Pending, Approved, Rejected, Approved (Not Applied)
- Confidence - High, Medium, Low
- File path - Search by filename
Phase 3: Applying Changes
After approving reviews, apply the changes to your source code:
php artisan translator:apply
This command does three things:
- Replaces hardcoded text with Laravel translation functions in your source files
- Activates translations - moves them from draft to active status
- Regenerates CDN - updates the CDN with the new translations
Before:
<h1>Welcome to our app</h1>
<button>Submit</button>
After:
<h1>{{ __('home.headings.welcome') }}</h1>
<button>{{ __('buttons.submit') }}</button>
Apply Options
# Preview changes without modifying files
php artisan translator:apply --dry-run
# Skip confirmation prompt
php artisan translator:apply --yes
After Applying
- Review changes:
git diff- check for syntax errors or invalid placements - Fix undefined variables: Your IDE will flag invented variable names - replace with real ones
- Test your application: Run your tests and verify the app works correctly
- Commit changes:
git add . && git commit -m "Apply translations"
Tip: If you find invalid replacements (like
__()in property declarations), revert those specific changes withgit checkout -pand reject the corresponding review.
Best Practices
Scan Regularly
Run scans:
- After adding new features
- Before releases
- As part of code review
Review Carefully
- Check suggested keys for clarity
- Verify confidence scores match actual quality
- Don't auto-approve everything—review medium/low confidence items
⚠️ Important: The Code Scanner is not 100% accurate in determining valid placement for
__()calls. It may suggest translations in contexts where PHP syntax doesn't allow function calls, such as:
- Property declarations (
public $placeholder = __('...'))- Constant definitions
- Array keys in certain contexts
Always review the code context before approving, especially for low/medium confidence items. After applying, check
git diffand test your application.
Use Consistent Naming
Good key patterns:
section.type.description
auth.buttons.login
profile.labels.email
errors.validation.required
Handle Dynamic Content
When the scanner detects text with dynamic values (like names, counts, or dates), it may suggest placeholders:
Original: "Welcome, John!"
Suggested key: welcome.greeting
Suggested value: "Welcome, :name!"
After applying, you'll see:
__('welcome.greeting', ['name' => $name])
⚠️ Manual step required: The scanner generates valid PHP code, but the variable name is invented based on the placeholder. You must replace it with the actual variable from your application:
// Before (auto-generated - $name doesn't exist) __('welcome.greeting', ['name' => $name]) // After (manually corrected) __('welcome.greeting', ['name' => $user->name])
Your IDE will likely flag these as "Undefined variable" errors, making them easy to find and fix.
Troubleshooting
Scan Not Finding Text
- Check file paths with
--pathoption - Ensure files are not in excluded directories
- Verify file types are supported (.blade.php, .php)
Low Confidence Scores
- Text may be too short or generic
- File location not recognized as user-facing
- Consider reviewing manually
Apply Not Working
- Ensure translations are published first
- Check that reviews are approved (not just pending)
- Verify API key configuration
Next Steps
- Translations - Manage your translations
- AI Translations - Auto-translate content
- Quota Management - Understand quotas
- Troubleshooting - Fix common scanner problems
- What is a hardcoded string? - Why hardcoded text breaks localization