Laravel Blade Translation Auto-Sync - VS Code Extension
A simple VS Code extension that automatically syncs Laravel translation keys from Blade files to existing translation files when you save.
Features
- Auto-Sync on Save: Automatically extracts translation keys from Blade files and syncs them to corresponding PHP translation files when you save
- Non-Destructive: Only works with existing translation files - never creates new files automatically
- Preserves Existing Translations: Adds new keys without overwriting existing translation values
- Comprehensive Pattern Detection: Finds various Laravel translation patterns in your Blade files
- Cleans Up Unused Keys: Removes keys that are no longer used - but only when it can prove it has found every key in the file
How It Works
- Save a Blade file (e.g.,
welcome.blade.php)
- Extension checks if corresponding translation file exists (e.g.,
welcome.php)
- If translation file exists: Extracts translation keys and syncs them
- If translation file doesn't exist: Does nothing (you control which files need translations)
Supported Translation Patterns
The extension detects these Laravel translation patterns in your Blade files:
__('key') and __("key")
trans('key') and trans_choice('key', $count)
@lang('key') and @choice('key', $count)
Lang::get('key'), Lang::choice('key', $count), Lang::has('key')
In any context: {{ }}, {!! !!}, @php blocks, or plain PHP. Keys containing HTML,
quotes, apostrophes, :placeholders and a second argument (__('Hi :name', ['name' => $n]))
are all handled. Calls inside {{-- blade comments --}} are ignored.
Safety
Deleting a translation is not recoverable from inside the editor, so the extension
refuses to act whenever it is not certain:
- Unparseable translation file - if any line cannot be read (string concatenation,
array() syntax, an unterminated quote), nothing is written and the error names the line.
A line it cannot read is never treated as "this key has no translation yet".
- Non-literal keys - if the file contains something like
__($key) or __('pre.' . $x),
the full key set is unknowable, so unused keys are kept instead of removed.
- Wrong file - if the sibling
.php file does not look like a translation file, or
contains translations but none of the keys this Blade file uses, it is left alone.
- No change - if the result is byte-identical, the file is not touched at all.
Nested groups ('auth' => ['failed' => '...']) are preserved as-is.
Example Workflow
1. Create your translation file manually
Create welcome.php in the same directory as your Blade file:
<?php
return [
'en' => [
// Your existing translations
],
'zh' => [
// Your existing translations
],
];
2. Edit your Blade file
Edit welcome.blade.php:
<h1>{{ __('Welcome to our site') }}</h1>
<p>{{ __('Please login') }}</p>
<span>{{ __('New feature') }}</span>
3. Save the Blade file
When you save welcome.blade.php, the extension automatically updates welcome.php:
<?php
return [
'en' => [
'New feature' => 'New feature', // ← Added automatically
'Please login' => 'Please login', // ← Added automatically
'Welcome to our site' => 'Welcome to our site', // ← Added automatically
],
'zh' => [
'New feature' => '', // ← Added (empty for translation)
'Please login' => '', // ← Added (empty for translation)
'Welcome to our site' => '', // ← Added (empty for translation)
],
];
Key Benefits
- ⚡ Zero Configuration: Just save your Blade files
- 🎯 Selective: Only works with existing translation files you've created
- 🔒 Safe: Never overwrites existing translations
- 🚀 Fast: Instant sync on save
- 🧹 Clean: No unnecessary commands or UI clutter
License
MIT