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

Nova Translations Loader Laravel Package

outl1ne/nova-translations-loader

Load your package’s translation files into Laravel Nova. Add the LoadsNovaTranslations trait to a service provider and call loadTranslations() to register Nova translations and optionally publish them automatically. Compatible with Nova 4, Laravel 9/10.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer in your Laravel/Nova project:

    composer require outl1ne/nova-translations-loader
    
  2. Publish Config (Optional) Publish the config for customization (supports Nova 5+):

    php artisan vendor:publish --provider="Outl1ne\NovaTranslationsLoader\NovaTranslationsLoaderServiceProvider" --tag="nova-translations-loader-config"
    
  3. Basic Usage Load translations in Nova resources/tools using the facade:

    use Outl1ne\NovaTranslationsLoader\Facades\NovaTranslationsLoader;
    
    $translations = NovaTranslationsLoader::load('path/to/translations');
    
  4. First Use Case (Nova 5+) Dynamically load translations for Nova 5 fields/labels:

    use Laravel\Nova\Fields\Text;
    
    public function fields(Request $request)
    {
        return [
            Text::make(__('fields::user.name'))
                ->help(NovaTranslationsLoader::load('fields::user.name.help')),
        ];
    }
    

Implementation Patterns

Common Workflows

  1. Nova 5 Resource Integration Use Nova 5's fluent field syntax with translations:

    Text::make(NovaTranslationsLoader::load('fields::post.title'))
        ->rules('required', 'max:255')
        ->onlyOnForms();
    
  2. Tool-Specific Translations (Nova 5) Load translations for Nova 5 tools (e.g., NovaCard, NovaMetric):

    // NovaCard.php
    public function title()
    {
        return NovaTranslationsLoader::load('cards::analytics.title');
    }
    
  3. Dynamic Key Resolution Resolve keys with Nova 5's context-aware helpers:

    protected function translationKey($key)
    {
        return "nova-translations::{$key}"; // Nova 5+ compatible
    }
    
  4. Fallback Handling Chain fallbacks with Nova 5's localization support:

    NovaTranslationsLoader::load('fields::missing.key', __('fallback.default'));
    

Integration Tips

  • Nova 5 Translation Namespace Organize translations in resources/lang/vendor/nova-translations with Nova 5's structure:

    nova-translations/
    ├── fields/
    │   ├── user.json
    │   └── post.json
    └── cards/
        └── metrics.json
    
  • Lazy Loading in Nova 5 Defer translation loading in resolveForNavigation or resolveForIndex:

    public function resolveForNavigation(Request $request)
    {
        $this->title = NovaTranslationsLoader::load('resources::dashboard.title');
        return parent::resolveForNavigation($request);
    }
    
  • Nova 5 Field Localization Use translations for field attributes:

    Text::make('bio')
        ->placeholder(NovaTranslationsLoader::load('fields::user.bio.placeholder'))
        ->onlyOnForms();
    
  • Caching (Nova 5 Compatibility) Cache translations to avoid repeated disk reads in Nova 5:

    private static $cache = [];
    
    public static function load($key, $fallback = null)
    {
        if (!isset(self::$cache[$key])) {
            self::$cache[$key] = trans($key, [], 'nova-translations');
        }
        return self::$cache[$key] ?? $fallback;
    }
    

Gotchas and Tips

Pitfalls

  1. Nova 5 Field Syntax Changes

    • Issue: Nova 5 uses fluent methods (e.g., Text::make()), not constructor arguments.
    • Fix: Update field definitions to use fluent syntax:
      // Old (Nova 4)
      new Text('name', __('fields::user.name'))
      
      // New (Nova 5)
      Text::make(__('fields::user.name'))
      
  2. Missing Translation Files (Nova 5)

    • Issue: trans() fails silently in Nova 5 if files are misplaced.
    • Fix: Ensure files are in resources/lang/vendor/nova-translations/ and use .json format:
      # Example structure
      resources/lang/
      └── vendor/
          └── nova-translations/
              ├── en/
              │   ├── fields.json
              │   └── cards.json
              └── es/
                  └── fields.json
      
  3. Caching Conflicts (Nova 5)

    • Issue: Nova 5's cache may not reflect translation changes.
    • Fix: Clear Nova 5's cache:
      php artisan nova:cache-clear
      
  4. Key Collisions (Nova 5)

    • Issue: Overlapping keys with Nova 5's built-in translations (e.g., nova::).
    • Fix: Prefix keys explicitly:
      NovaTranslationsLoader::load('nova-translations::auth.login');
      
  5. Nova 5 Tool Localization

    • Issue: Tools like NovaCard may not resolve translations in Nova 5.
    • Fix: Ensure tools extend NovaTool and use resolveForNavigation:
      public function resolveForNavigation(Request $request)
      {
          $this->title = NovaTranslationsLoader::load('cards::title');
          return parent::resolveForNavigation($request);
      }
      

Debugging

  • Log Missing Keys (Nova 5) Add debug logging for unresolved keys:

    public static function debugLoad($key, $fallback = null)
    {
        $translation = trans($key, [], 'nova-translations');
        if ($translation === "{$key}") {
            Log::warning("Translation key not found: {$key}");
        }
        return $translation ?? $fallback;
    }
    
  • Check Loaded Languages (Nova 5) Verify the correct locale is set in Nova 5:

    app()->getLocale(); // Should return 'en' or your default.
    
  • Nova 5 Translation Dump Dump all loaded translations for debugging:

    dd(trans('nova-translations::*', [], 'nova-translations'));
    

Extension Points

  1. Custom Translation Directories (Nova 5) Extend to load from additional directories:

    NovaTranslationsLoader::addNamespace('custom', resource_path('lang/custom'));
    
  2. Dynamic Language Switching (Nova 5) Override the locale for specific requests:

    NovaTranslationsLoader::setLocale($request->header('accept-language'));
    
  3. Nova 5 Field Localization Hooks Use Nova 5's field hooks to inject translations:

    Text::make('bio')
        ->useFieldTranslationHooks()
        ->placeholder(NovaTranslationsLoader::load('fields::bio.placeholder'));
    
  4. Integration with Nova 5's resolveUsing Dynamically resolve fields/tools using translations:

    public function fields(Request $request)
    {
        return [
            Text::make('dynamic_field')
                ->resolveUsing(function () {
                    return NovaTranslationsLoader::load('fields::dynamic.value');
                }),
        ];
    }
    
  5. Nova 5 Tool Translation Fallbacks Implement custom fallbacks for tools:

    NovaTranslationsLoader::setFallbackResolver(function ($key) {
        return "nova-fallbacks::{$key}";
    });
    
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.
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
christhompsontldr/laravel-inky