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.
Installation Add the package via Composer in your Laravel/Nova project:
composer require outl1ne/nova-translations-loader
Publish Config (Optional) Publish the config for customization (supports Nova 5+):
php artisan vendor:publish --provider="Outl1ne\NovaTranslationsLoader\NovaTranslationsLoaderServiceProvider" --tag="nova-translations-loader-config"
Basic Usage Load translations in Nova resources/tools using the facade:
use Outl1ne\NovaTranslationsLoader\Facades\NovaTranslationsLoader;
$translations = NovaTranslationsLoader::load('path/to/translations');
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')),
];
}
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();
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');
}
Dynamic Key Resolution Resolve keys with Nova 5's context-aware helpers:
protected function translationKey($key)
{
return "nova-translations::{$key}"; // Nova 5+ compatible
}
Fallback Handling Chain fallbacks with Nova 5's localization support:
NovaTranslationsLoader::load('fields::missing.key', __('fallback.default'));
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;
}
Nova 5 Field Syntax Changes
Text::make()), not constructor arguments.// Old (Nova 4)
new Text('name', __('fields::user.name'))
// New (Nova 5)
Text::make(__('fields::user.name'))
Missing Translation Files (Nova 5)
trans() fails silently in Nova 5 if files are misplaced.resources/lang/vendor/nova-translations/ and use .json format:
# Example structure
resources/lang/
└── vendor/
└── nova-translations/
├── en/
│ ├── fields.json
│ └── cards.json
└── es/
└── fields.json
Caching Conflicts (Nova 5)
php artisan nova:cache-clear
Key Collisions (Nova 5)
nova::).NovaTranslationsLoader::load('nova-translations::auth.login');
Nova 5 Tool Localization
NovaCard may not resolve translations in Nova 5.NovaTool and use resolveForNavigation:
public function resolveForNavigation(Request $request)
{
$this->title = NovaTranslationsLoader::load('cards::title');
return parent::resolveForNavigation($request);
}
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'));
Custom Translation Directories (Nova 5) Extend to load from additional directories:
NovaTranslationsLoader::addNamespace('custom', resource_path('lang/custom'));
Dynamic Language Switching (Nova 5) Override the locale for specific requests:
NovaTranslationsLoader::setLocale($request->header('accept-language'));
Nova 5 Field Localization Hooks Use Nova 5's field hooks to inject translations:
Text::make('bio')
->useFieldTranslationHooks()
->placeholder(NovaTranslationsLoader::load('fields::bio.placeholder'));
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');
}),
];
}
Nova 5 Tool Translation Fallbacks Implement custom fallbacks for tools:
NovaTranslationsLoader::setFallbackResolver(function ($key) {
return "nova-fallbacks::{$key}";
});
How can I help you explore Laravel packages today?