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

Lingua Laravel Package

rivalex/lingua

Database-driven translations for Laravel with a polished Livewire + Flux admin UI. Install and manage languages, edit strings in real time, and sync translations both ways between DB and PHP/JSON files via artisan commands. Supports Laravel 11–13, PHP 8.3+.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require rivalex/lingua
    php artisan lingua:install
    

    This publishes config, runs migrations, and seeds the database with your default language.

  2. First Use Case:

    • Access the UI at /lingua/languages to manage installed languages.
    • Use the Add Language button to install a new locale (e.g., es for Spanish).
    • Navigate to /lingua/translations to edit translations directly in the database.

Where to Look First

  • Configuration: config/lingua.php (customize default locale, sync behavior, UI settings).
  • Artisan Commands: Run php artisan lingua:update-lang to fetch the latest translations from laravel-lang.
  • UI Routes:
    • /lingua/languages → Manage installed languages.
    • /lingua/translations → Edit translations.
    • /lingua/statistics → Track progress.

Implementation Patterns

Core Workflows

  1. Database-Driven Translations:

    • Use the Lingua Facade (Lingua::get('key', 'locale')) to fetch translations from the database.
    • Example:
      $translation = Lingua::get('auth.login', 'es'); // Returns Spanish translation
      
    • Sync local files to the database:
      php artisan lingua:sync-to-database
      
  2. Language Management:

    • Add/remove languages via CLI:
      php artisan lingua:add fr  # Add French
      php artisan lingua:remove de # Remove German
      
    • Update all languages:
      php artisan lingua:update-lang
      
  3. Rich-Text Translations:

    • Enable Markdown/HTML support in config/lingua.php under the editor key.
    • Edit translations in the UI with a WYSIWYG editor (e.g., for help text or documentation).
  4. Headless Language Selector:

    • Use the @linguaSelector Blade component for a zero-CSS dropdown/modal/sidebar selector.
    • Customize via config/lingua.php:
      'selector' => [
          'mode' => 'dropdown', // 'sidebar' | 'modal' | 'dropdown'
          'show_flags' => true,
      ]
      
  5. Vendor Translations:

    • Manage package translations (e.g., vendor/laravel-lang) alongside your app’s translations.
    • Sync vendor files to the database automatically during lingua:update-lang.

Integration Tips

  • Blade Integration: Use @lingua directive for translations:

    <h1>@lingua('auth.welcome')</h1>
    

    Fallback to default locale if missing:

    <h1>@lingua('auth.welcome', fallback: true)</h1>
    
  • API Responses: Return translations dynamically:

    return response()->json([
        'message' => Lingua::get('api.success', request()->locale),
    ]);
    
  • Middleware: Force a locale for specific routes:

    Route::middleware(['locale' => 'es'])->group(function () {
        // Spanish-only routes
    });
    
  • Testing: Mock translations in tests:

    Lingua::shouldReceive('get')->andReturn('Mocked translation');
    

Gotchas and Tips

Pitfalls

  1. Default Locale Lock:

    • The default language (set in config/lingua.php) cannot be removed via the UI or CLI.
    • Workaround: Temporarily change the default locale before removal.
  2. Sync Conflicts:

    • Manual edits to lang/ files may be overwritten during sync-to-database.
    • Fix: Use sync-to-local to export database changes to files first.
  3. Livewire Caching:

    • Clear Livewire cache after major UI changes:
      php artisan lingua:clear-cache
      
  4. RTL Languages:

    • Right-to-left (RTL) languages (e.g., Arabic) require additional CSS for alignment.
    • Use the rtl class in your layout:
      <html lang="{{ app()->getLocale() }}" class="{{ app()->isLocaleRTL() ? 'rtl' : '' }}">
      
  5. Large Translation Sets:

    • Database performance may degrade with >100K translations.
    • Optimization: Add indexes to language_lines table (publish migrations first).

Debugging

  • Missing Translations:

    • Check the Statistics page for missing keys.
    • Use the Translations page filter: Show missing translations only.
  • Sync Issues:

    • Verify file permissions for lang/ directory.
    • Run php artisan lingua:sync-to-database --verbose for detailed logs.
  • UI Glitches:

    • Clear browser cache or use ?flush=1 to bypass Livewire cache: /lingua/translations?flush=1.

Extension Points

  1. Custom Fields:

    • Extend the language_lines table by publishing migrations:
      php artisan vendor:publish --tag="lingua-migrations"
      
    • Add a notes column for translator comments:
      Schema::table('language_lines', function (Blueprint $table) {
          $table->text('notes')->nullable();
      });
      
  2. Custom Validation:

    • Override translation validation in app/Providers/LinguaServiceProvider.php:
      public function boot()
      {
          Lingua::extend('validation', function ($validator, $key, $locale) {
              $validator->addRules([
                  'key' => 'required|max:255',
                  'value' => 'required|max:65535|custom:no_html_tags',
              ]);
          });
      }
      
  3. Custom UI Components:

    • Publish and override Livewire views:
      php artisan vendor:publish --tag="lingua-views"
      
    • Example: Modify resources/views/vendor/lingua/livewire/translations.blade.php to add a custom column.
  4. AI Translation Integration:

    • Hook into the lingua.translation.updated event to auto-translate via an API:
      Lingua::listen('translation.updated', function ($translation) {
          if ($translation->locale !== 'en') {
              $translated = callToAITranslationAPI($translation);
              $translation->value = $translated;
              $translation->save();
          }
      });
      

Configuration Quirks

  • Fallback Locale:

    • Ensure config/lingua.php and config/app.php use the same fallback_locale.
    • Example:
      'fallback_locale' => 'en', // Must match app.fallback_locale
      
  • Editor Toolbar:

    • Disable unsafe HTML tags in the rich-text editor:
      'editor' => [
          'allowed_tags' => ['<strong>', '<em>', '<p>'],
      ]
      
  • Database Drivers:

    • For SQLite, ensure the lang_dir in config/lingua.php points to a writable directory.

Pro Tips

  1. Bulk Updates:

    • Use the Translations page bulk actions to update multiple keys at once.
  2. Translation Groups:

    • Organize keys by group (e.g., auth, validation) for easier filtering.
  3. Localization Testing:

    • Switch locales dynamically in tests:
      $this->actingAs(User::factory()->create())
           ->withSession(['locale' => 'fr']);
      
  4. Deployment Workflow:

    • Sync to local before deployment:
      php artisan lingua:sync-to-local
      git add lang/
      git commit -m "Update translations"
      
  5. Collaboration:

    • Use the Statistics page to assign missing translations to team members via comments (custom field).
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
terminal42/code-quality-tools
codifyo/ts-generator-bundle
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity