Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Weglot Laravel Laravel Package

weglot/weglot-laravel

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require weglot/translate-laravel
    

    (Note: Despite being deprecated, the package remains functional for existing integrations.)

  2. Publish Config (if needed):

    php artisan vendor:publish --provider="Weglot\TranslateLaravel\WeglotServiceProvider"
    

    This generates a config/weglot.php file with default settings.

  3. Configure .env:

    WEGLOT_API_KEY=your_api_key_here
    WEGLOT_DOMAIN=yourdomain.com
    

    (Replace placeholders with your Weglot credentials from dashboard.weglot.com.)

  4. First Use Case: Add Weglot’s JavaScript snippet to your layout (e.g., resources/views/layouts/app.blade.php):

    @weglot
    

    This renders the Weglot script with your configured API key and domain.


Implementation Patterns

Core Workflows

  1. Dynamic Language Switching: Use Weglot’s built-in language selector in Blade templates:

    @weglotLanguageSelector
    

    Customize appearance via config/weglot.php (e.g., selector_position, selector_theme).

  2. Excluding Pages/Elements: Skip translation for specific routes or elements:

    // In routes/web.php
    Route::middleware(['weglot:exclude'])->group(function () {
        Route::get('/admin', 'AdminController@index');
    });
    

    Or exclude elements via HTML attributes:

    <div data-weglot-ignore="true">...</div>
    
  3. API Integration: Fetch translation stats or manage projects via Weglot’s API:

    use Weglot\TranslateLaravel\Facades\Weglot;
    
    $stats = Weglot::api()->stats();
    
  4. Middleware for Conditional Loading: Load Weglot only for non-admin routes:

    // app/Http/Kernel.php
    protected $middlewareGroups = [
        'web' => [
            // ...
            \Weglot\TranslateLaravel\Http\Middleware\LoadWeglot::class,
        ],
        'admin' => [
            // Exclude from admin
        ],
    ];
    

Advanced Patterns

  • Custom Translations: Override Weglot’s translations for specific terms via config/weglot.php:

    'custom_translations' => [
        'en' => ['home' => 'Home Page'],
        'es' => ['home' => 'Página Principal'],
    ],
    

    (Note: Requires Weglot’s custom dictionary feature.)

  • Event Listeners: Trigger actions on language change (e.g., update user locale):

    // app/Providers/EventServiceProvider.php
    public function boot()
    {
        Weglot::onLanguageChanged(function ($locale) {
            app()->setLocale($locale);
        });
    }
    

Gotchas and Tips

Common Pitfalls

  1. Deprecation Warning:

    • The package is archived; new projects should use Weglot’s subdomain integration.
    • Existing projects may face breaking changes if Weglot updates its API.
  2. Caching Issues:

    • Weglot’s JavaScript snippet may be cached aggressively. Clear your browser/cache or use ?v=2 to bypass:
      <script src="//cdn.weglot.com/weglot.js?v=2"></script>
      
  3. Locale Mismatch:

    • Ensure app()->getLocale() matches Weglot’s detected language to avoid UI inconsistencies.
    • Debug with:
      dd(Weglot::currentLanguage());
      
  4. API Rate Limits:

    • Weglot’s free tier has API limits. Monitor usage via:
      $limits = Weglot::api()->limits();
      

Debugging Tips

  • Verify API Key: Check config/weglot.php and .env for typos. Test with:

    Weglot::api()->validateKey();
    
  • Console Logs: Enable debug mode in config/weglot.php:

    'debug' => env('WEGLOT_DEBUG', false),
    

    Logs appear in storage/logs/laravel.log.

  • Blade Directives: If @weglot fails, ensure the service provider is registered (auto-discovery should handle this, but verify in config/app.php).

Extension Points

  1. Custom JavaScript: Override the default snippet via config/weglot.php:

    'custom_script' => '<script src="custom-weglot.js"></script>',
    
  2. Webhook Integration: Use Weglot’s webhooks to sync translations with your database:

    // routes/web.php
    Route::post('/weglot-webhook', 'WebhookController@handle');
    
  3. Testing: Mock Weglot in tests:

    $this->partialMock(Weglot::class, ['api']);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nasirkhan/laravel-sharekit
directorytree/privacy-filter-classifier
directorytree/privacy-filter
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit
testo/output-teamcity
testo/bridge-symfony