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

Translate Laravel Laravel Package

weglot/translate-laravel

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require weglot/translate-laravel
    

    The package uses Laravel's auto-discovery, so no manual service provider registration is needed.

  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 Weglot: Add your Weglot API key and project slug to .env:

    WEGLOT_API_KEY=your_api_key_here
    WEGLOT_PROJECT_SLUG=your_project_slug
    
  4. First Use Case: Add Weglot's JavaScript snippet to your resources/views/layouts/app.blade.php (or your main layout file):

    @weglot
    

    This outputs the Weglot script with your configured API key and project slug.


Implementation Patterns

Core Workflows

  1. Dynamic Language Switching: Use Weglot's language detection middleware to auto-switch based on user preferences or browser settings:

    // In routes/web.php
    Route::middleware(['weglot'])->group(function () {
        // Your routes here
    });
    
  2. Blade Directives:

    • @weglot: Outputs the Weglot script tag.
    • @weglotLang: Displays the current language (e.g., @weglotLang → "en").
    • @weglotLangSwitcher: Renders a language switcher dropdown.
  3. API Integration: Fetch Weglot's translation status or user language via the facade:

    use Weglot\TranslateLaravel\Facades\Weglot;
    
    $currentLang = Weglot::getCurrentLanguage(); // e.g., "fr"
    $isTranslated = Weglot::isTranslated();      // boolean
    
  4. Asset Optimization: Load Weglot asynchronously or defer its script to improve page load performance:

    @weglot(['async' => true])
    
  5. Conditional Loading: Only load Weglot for specific locales or routes:

    if (app()->getLocale() !== 'en') {
        @weglot
    }
    

Integration Tips

  • Laravel Mix/Webpack: Exclude Weglot's script from bundling (it’s third-party).
  • Caching: Weglot’s JS handles caching; avoid re-rendering @weglot in cached views.
  • Testing: Mock Weglot’s facade in PHPUnit:
    $this->partialMock(Weglot::class, ['getCurrentLanguage']);
    

Gotchas and Tips

Pitfalls

  1. Deprecation Warning: The package is deprecated. Migrate to Weglot’s subdomain integration for long-term support.

    • Workaround: Use the package temporarily for quick prototyping, but plan to replace it.
  2. Configuration Overrides:

    • The weglot.php config file may not exist after installation. Publish it explicitly if customization is needed.
    • Default values (e.g., default_language) are hardcoded in the package. Override them in the config file.
  3. JavaScript Conflicts:

    • Weglot’s script may interfere with other translation tools or SPAs. Test thoroughly.
    • If using Laravel Mix, ensure the @weglot directive is placed before </body> to avoid race conditions.
  4. Locale Mismatch:

    • Weglot’s detected language (@weglotLang) may differ from Laravel’s app()->getLocale(). Sync them via middleware:
      public function handle($request, Closure $next) {
          $weglotLang = Weglot::getCurrentLanguage();
          if ($weglotLang && !in_array($weglotLang, config('app.locales'))) {
              app()->setLocale(config('app.fallback_locale'));
          }
          return $next($request);
      }
      
  5. API Key Exposure:

    • The .env file is committed by default in some setups. Add weglot.php to .gitignore if it contains sensitive keys.

Debugging Tips

  • Check Console Errors: Weglot’s JS may log errors if the API key/project slug is invalid.

  • Disable Weglot Temporarily:

    // In config/weglot.php
    'enabled' => env('WEGLOT_ENABLED', false),
    

    Set WEGLOT_ENABLED=false in .env to debug without Weglot.

  • Inspect Network Requests: Verify Weglot’s API calls in Chrome DevTools (Network tab) under weglot.com.

Extension Points

  1. Custom Language Detection: Override Weglot’s language logic by extending the Weglot facade:

    Weglot::extend(function ($app) {
        $app->bind('weglot.detector', function () {
            return new CustomLanguageDetector();
        });
    });
    
  2. Post-Translation Hooks: Trigger actions after language changes (e.g., redirect or reload):

    // In a custom JS file
    window.Weglot.on('languageChanged', function() {
        window.location.reload();
    });
    
  3. Server-Side Fallbacks: Use Weglot’s API to fetch translations for untranslated content:

    public function getTranslation($text, $targetLang) {
        return Weglot::translate($text, $targetLang); // Hypothetical method
    }
    

    Note: The package lacks a direct API client; use Weglot’s official API instead.

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.
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge