weglot/translation-definitions
Laravel package providing translation definitions for Weglot integrations—helpers and configuration to manage translation keys, mapping, and defaults in a consistent way across your app. Useful for standardizing multilingual content handling with Weglot.
Install the Package
composer require weglot/translation-definitions
This installs the shared translation definitions without requiring additional setup.
Publish Configuration (Optional) If the package includes default configurations or assets, publish them:
php artisan vendor:publish --tag=weglot-translation-definitions
This generates a config/weglot-translation-definitions.php file for customization.
First Use Case: Define a Translation Key Use the package’s interfaces or value objects to define a translation key. For example:
use Weglot\TranslationDefinitions\Language;
use Weglot\TranslationDefinitions\TranslationKey;
$key = new TranslationKey('user.welcome', [
'en' => 'Welcome, {name}!',
'es' => '¡Bienvenido, {name}!',
]);
// Store or use the key in your application logic.
Integrate with Laravel’s Translation System If using Laravel’s built-in translation system, extend the package’s definitions:
// In a service provider or config file
'translations' => [
'paths' => [
resource_path('lang/weglot'),
],
],
Place Weglot-compatible translation files in resources/lang/weglot.
Defining Translation Keys
Use the package’s TranslationKey or TranslationDefinition classes to structure translations:
$definition = new TranslationDefinition(
'user.profile',
[
'en' => 'Profile Settings',
'fr' => 'Paramètres du profil',
],
['plural' => true] // Optional metadata
);
Language and Locale Handling
Leverage the Language and Locale value objects for consistency:
$language = new Language('en', 'English');
$locale = new Locale('en_US', 'en', 'US');
Integration with Laravel’s trans() Helper
Ensure translations align with Laravel’s syntax:
// In Blade or PHP
{{ trans('weglot::user.welcome', ['name' => 'John']) }}
Store Weglot-specific translations in resources/lang/vendor/weglot/.
Service Provider Integration Bind the package’s interfaces to Laravel’s container for dependency injection:
// In AppServiceProvider
public function register()
{
$this->app->bind(
TranslationDefinitionResolver::class,
WeglotTranslationDefinitionResolver::class
);
}
Dynamic Translation Loading For dynamic content (e.g., CMS-driven translations), use the package’s resolvers:
$resolver = app(TranslationDefinitionResolver::class);
$definition = $resolver->resolve('dynamic.key');
Translation Extraction Use the package’s tools to extract translation keys from your codebase:
php artisan weglot:extract
This generates a report of missing or unused keys.
Translation Validation Validate translations against Weglot’s schema:
php artisan weglot:validate
Ensures keys and locales adhere to the package’s standards.
Hybrid Translation Management Combine Weglot’s definitions with Laravel’s native translations:
resources/lang/vendor/weglot/.lang directory for non-Weglot translations.Testing Translations Write PHPUnit tests to verify translations:
public function testTranslationResolution()
{
$this->assertEquals(
'Welcome, John!',
trans('weglot::user.welcome', ['name' => 'John'])
);
}
Middleware for Language Detection Use Weglot’s middleware to detect and set the user’s preferred language:
// In app/Http/Kernel.php
protected $middlewareGroups = [
'web' => [
// ...
\Weglot\TranslationDefinitions\Http\Middleware\DetectLanguage::class,
],
];
Event Listeners for Translation Updates Listen for translation updates and trigger cache invalidation:
// In EventServiceProvider
protected $listen = [
'weglot.translation.updated' => [
\App\Listeners\InvalidateTranslationCache::class,
],
];
Customizing Fallback Logic Extend the package’s fallback resolver to handle missing translations:
public function resolve($key, $locale = null, $fallback = true)
{
$definition = $this->getDefinition($key, $locale);
return $definition ?: ($fallback ? trans('fallback.' . $key) : null);
}
API Integration If using Weglot’s API, integrate the package’s definitions with API responses:
$translations = $this->weglotApi->fetchTranslations($locale);
$this->translationStore->store($translations);
Namespace Conflicts
resources/lang.weglot::user.welcome) and ensure Laravel’s translation loader respects it.Locale Mismatches
en_US) may not align with Laravel’s App::getLocale() (e.g., en).app()->bind('locale.normalizer', function () {
return new WeglotLocaleNormalizer();
});
Performance Overhead
Cache::remember("weglot.definitions.{$locale}", now()->addHours(1), function () use ($locale) {
return $this->resolver->resolveAll($locale);
});
Breaking Changes
composer.json:
"weglot/translation-definitions": "^1.0"
Monitor for deprecations in Weglot’s changelog.Missing Fallback Logic
$translation = trans('weglot::key', [], $locale) ?: trans('fallback.key');
Log Translation Resolution Enable debug logging for translation issues:
// In config/weglot-translation-definitions.php
'debug' => env('APP_DEBUG', false),
Check logs for Weglot\TranslationDefinitions\Logging\TranslationLogger.
Inspect Resolved Definitions Dump definitions to debug missing keys:
dd(app(TranslationDefinitionResolver::class)->resolve('missing.key'));
Validate Translation Files Use Artisan commands to validate your translation files:
php artisan weglot:validate --locale=es --path=resources/lang/vendor/weglot
Check for Circular Dependencies If translations reference each other, use a dependency checker:
composer require --dev nette/di
php artisan weglot:check-circular-dependencies
Default Language Configuration
Ensure config/app.php and Weglot’s config align:
'locale' => 'en', // Laravel default
'weglot' => [
'default_locale' => 'en_US', // Weglot default
],
Caching Configuration Configure caching for definitions:
'cache' => [
'enabled' => true,
'driver' => 'file', // or 'redis', 'database'
'ttl' => 60, // minutes
],
Path Configuration Specify custom paths for translation files:
'paths' => [
'weglot' => resource_path('lang/vendor/weglot'),
'fallback' => resource_path('lang/fallback'),
],
Custom Value Objects Extend Weglot’s value objects for project-specific needs:
class CustomTranslationKey extends TranslationKey
{
public function __construct(string $key, array $translations, array $metadata = [])
{
parent::__construct($key, $translations, $metadata + ['custom' => true]);
}
}
Custom Resolvers Implement a custom resolver for non-standard translation
How can I help you explore Laravel packages today?