PHP code example of nvl / laravel-typescript-translations
1. Go to this page and download the library: Download nvl/laravel-typescript-translations library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nvl / laravel-typescript-translations example snippets
// app/Http/Middleware/HandleInertiaRequests.php
public function share(Request $request): array
{
return array_merge(parent::share($request), [
'locale' => app()->getLocale(),
// ONLY navigation and layout translations
'translations' => [
'navigation' => trans('navigation'),
'common' => [
'logout' => trans('common.logout'),
'profile' => trans('common.profile'),
]
]
]);
}
// VendorController.php
public function index()
{
return Inertia::render('Vendors/Index', [
'vendors' => Vendor::paginate(),
'translations' => [
'vendors' => [
'list' => trans('vendors.list'),
'filters' => trans('vendors.filters'),
'actions' => [
'create' => trans('vendors.actions.create'),
'export' => trans('vendors.actions.export'),
]
]
]
]);
}
public function edit(Vendor $vendor)
{
return Inertia::render('Vendors/Edit', [
'vendor' => $vendor,
'translations' => [
'vendors' => [
'forms' => trans('vendors.forms'),
'validation' => trans('vendors.validation'),
]
]
]);
}
return [
/*
|--------------------------------------------------------------------------
| Translation Paths
|--------------------------------------------------------------------------
| Paths to scan for translation files. Use :locale placeholder.
*/
'paths' => [
'lang/:locale',
'resources/lang/:locale',
'Modules/*/Resources/lang/:locale', // For modular apps
],
/*
|--------------------------------------------------------------------------
| Base Language
|--------------------------------------------------------------------------
| The primary language to use for type generation.
*/
'base_language' => env('TRANSLATION_BASE_LANGUAGE', 'en'),
/*
|--------------------------------------------------------------------------
| Additional Locales
|--------------------------------------------------------------------------
| Other locales to scan when exporting translations.
*/
'locales' => ['en', 'bg', 'de', 'fr'],
/*
|--------------------------------------------------------------------------
| Type Generation Output
|--------------------------------------------------------------------------
| Configure how TypeScript types are generated.
*/
'output' => [
'path' => 'resources/js/types',
'mode' => env('TRANSLATION_TYPES_MODE', 'module'),
'file_name' => 'translations.d.ts', // For single mode
],
/*
|--------------------------------------------------------------------------
| Translation Sources
|--------------------------------------------------------------------------
| Define specific sources to generate types for.
| Leave empty to scan all paths.
*/
'sources' => [
'vendors' => [
'path' => 'lang/en/vendors',
'nested' => true, // Scan subdirectories
'ignore' => ['temp.php', 'old/*'],
],
'tasks' => [
'path' => 'lang/en/tasks',
'nested' => true,
],
],
/*
|--------------------------------------------------------------------------
| Type Generation Settings
|--------------------------------------------------------------------------
*/
'type_suffix' => 'I18N', // Interface suffix
'export_keys' => true, // Generate key union types
'strict_mode' => true, // Use strict TypeScript
'preserve_array_keys' => false, // Keep numeric array keys
/*
|--------------------------------------------------------------------------
| Translation Export Settings
|--------------------------------------------------------------------------
| Configure how translation data is exported.
*/
'translation_export' => [
'path' => 'resources/js/data/translations',
'mode' => 'module', // single, module, granular
'organize_by' => 'locale-mapped', // locale-mapped, locale, module
'format' => 'typescript', // typescript, json
'filename_pattern' => '{source}.translations.{ext}',
'
// config/typescript-translations.php
'translation_export' => [
'minify' => env('APP_ENV') === 'production',
'path' => env('APP_ENV') === 'local'
? 'resources/js/data/translations'
: 'public/js/translations', // CDN in production
],
bash
php artisan vendor:publish --tag=typescript-translations-config
bash
# 1. Generate TypeScript types for backend translations
php artisan translations:generate --mode=module
# 2. Export translation data for frontend usage
php artisan translations:export --mode=module --organize-by=locale-mapped
bash
php artisan translations:generate [options]
bash
php artisan translations:export [options]