laravel-lang/publisher
Laravel Lang Publisher lets you publish and manage Laravel Lang translation files in your app. Install as a dev dependency and use it to keep localization resources organized and up to date with the Laravel Lang ecosystem.
Installation:
composer require --dev laravel-lang/publisher
Add to composer.json under require-dev if not using Laravel’s default package discovery.
Publish Core Translations:
php artisan lang:publish
This generates resources/lang/ with default Laravel translations (e.g., auth.php, validation.php) from Laravel-Lang’s upstream repository.
First Use Case:
resources/lang/en/auth.php to customize the "failed" key.php artisan lang:publish fr to publish French translations.config/lang.php (auto-generated) for configuration like default_locale or smart_punctuation.resources/lang/ directory structure to understand how translations are organized (e.g., en/, fr/).Publishing Translations:
php artisan lang:publish (all supported locales).php artisan lang:publish fr (French only).source config:
'source' => 'https://github.com/your-repo/translations',
Updating Translations:
php artisan lang:update
php artisan lang:update --force
Removing Locales:
es):
php artisan lang:remove es
Environment-Specific Config:
PUBLISHER_DEFAULT_LOCALE in .env to avoid hardcoding:
PUBLISHER_DEFAULT_LOCALE=es
CI/CD Pipeline:
Add to deploy.sh or GitHub Actions to auto-update translations on main:
- name: Update translations
run: php artisan lang:update
Use --force sparingly (e.g., only in staging).
Custom Translation Files:
Place custom files (e.g., resources/lang/en/custom.php) outside the lang/ directory published by Laravel-Lang to avoid being overwritten.
Lumen Support:
Ensure lang_path() is defined in bootstrap/app.php:
$app->withFacades();
if (! function_exists('lang_path')) {
function lang_path() {
return __DIR__.'/../resources/lang';
}
}
Validation Messages:
Extend validation.php by adding keys under a namespace (e.g., custom.*) to avoid conflicts:
'custom' => [
'email' => 'The :attribute must be a valid email address.',
],
Dynamic Locale Switching: Use middleware to set the locale dynamically:
public function handle(Request $request, Closure $next) {
app()->setLocale($request->header('Accept-Language') ?? config('app.locale'));
return $next($request);
}
Local Overrides Lost:
lang:update --force wipes all local changes. Use --dry-run first to preview changes:
php artisan lang:update --dry-run
Namespace Collisions:
auth.password to validation.php, they’ll conflict with Laravel’s core auth.php. Use namespaced keys (e.g., validation.custom.*).Lumen Compatibility:
lang_path() by default. Ensure it’s defined (see Integration Tips) or use the full path:
php artisan lang:publish --path=/full/path/to/resources/lang
Smart Punctuation Quirks:
smart_punctuation in config/lang.php may break HTML-escaped strings. Disable for API responses:
'smart_punctuation' => env('APP_ENV') !== 'local',
Git Ignore:
lang/ directory may contain sensitive data (e.g., placeholder emails). Add to .gitignore if needed, but ensure backups exist.Command Not Found:
Ensure the package is listed under require-dev in composer.json and run composer dump-autoload.
Permission Denied:
Use --force with file operations or adjust storage permissions:
chmod -R 775 resources/lang/
Locale Not Found:
Verify the locale exists in Laravel-Lang’s supported locales. For custom locales, use php artisan lang:publish custom_locale and manually add translations.
Custom Sources:
Extend the LaravelLang\Publisher\Contracts\Source interface to pull translations from a custom repository:
// app/Providers/PublisherServiceProvider.php
use LaravelLang\Publisher\Contracts\Source;
class CustomSource implements Source {
public function get($locale) { /* ... */ }
}
Bind it in the service provider:
$this->app->bind('publisher.source', function () {
return new CustomSource();
});
Post-Update Hooks:
Listen to the lang.published event to run custom logic after updates:
// app/Providers/EventServiceProvider.php
protected $listen = [
'LaravelLang\Publisher\Events\LangPublished' => [
\App\Listeners\SyncTranslationsToS3::class,
],
];
Configuration Overrides:
Override config/lang.php in config/lang.php (auto-generated) to disable features like smart_punctuation or change the default_locale.
Backup Strategy:
Use git stash or rsync to backup resources/lang/ before running lang:update --force.
Partial Updates:
Update only specific files by leveraging the --file option (if supported in future versions) or manually merge changes.
Locale-Specific Config:
Use config('app.locale') in Blade to conditionally load assets:
@if(app()->getLocale() === 'fr')
<link rel="stylesheet" href="{{ asset('css/fr.css') }}">
@endif
Testing:
Mock translations in tests using trans() with a custom provider:
// tests/TestCase.php
protected function getEnvironmentSetUp($app) {
$app['translator']->setLocale('test');
$app['translator']->addNamespace('test', __DIR__.'/stubs/lang');
}
How can I help you explore Laravel packages today?