abetwothree / lang-export
Export your Laravel translation files as JSON for Vue, React, & Svelte with @tolki/lang
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^13.0||^12.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- brianium/paratest: !=7.26.1
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0||^10.0
- pestphp/pest: ^5.0
- pestphp/pest-plugin-arch: ^5.0
- pestphp/pest-plugin-laravel: ^5.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
This package writes your Laravel translations to one JSON file per locale. It reads every file through Laravel's own translation loader, so the JSON holds the same lines __() returns.
It is the Laravel side of @tolki/lang, a JavaScript port of Laravel's translator from Tolki. @tolki/lang does not read lang files itself. It takes the JSON this package writes.
Installation
composer require abetwothree/lang-export
npm install @tolki/lang
Optionally publish the config file:
php artisan vendor:publish --tag="lang-export-config"
Exporting
php artisan lang:export
This writes resources/js/lang/{locale}.json for each locale, plus a manifest.json that lists the locales and the lang directories they came from. PHP groups become nested objects, JSON lines stay at the top level, and a package's groups sit under namespace::group keys:
{
"Welcome": "Welcome",
"auth": {
"failed": "These credentials do not match our records."
},
"courier::mail": {
"sent": "Your message was sent."
}
}
Pass --locale, --only, --except or --output to narrow a run, or set them in config/lang-export.php.
Re-exporting one file
php artisan lang:export --source=lang/en/auth.php
--source re-exports one changed lang file and updates its locale's JSON file. File watchers call it on every save.
Vite plugin
The Vite plugin in @tolki/lang runs the export for you. It exports when vite dev starts and before vite build, re-exports each lang file you save, and serves the locales as the virtual:tolki-lang module.
// vite.config.ts import laravelLangExport from "@tolki/lang/vite-laravel"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [laravelLangExport()], });
import { createLang } from "@tolki/lang"; import { load } from "virtual:tolki-lang"; const lang = createLang({ locale: "en", fallback: "en", load }); await lang.ready; lang.__("auth.failed"); // => "These credentials do not match our records."
Vue, React and Svelte plugins
@tolki/lang has a native plugin for each framework. Each one hands the lang to your components and re-renders them when the locale changes. All three work with Inertia, server-side rendering included.
@tolki/lang/vueinstalls into a Vue 3.5 app. Templates get__,transandtransChoice, anduseLang()gives alocaleyou can bind withv-model.@tolki/lang/reactgives React 19 a<LangProvider>and auseLang()hook.@tolki/lang/sveltepasses the lang to Svelte 5.7 components through context, withsetLang()andgetLang().
Key capabilities
- Package namespaces and
lang/vendoroverrides, resolved as Laravel resolves them - Extra lang paths, JSON paths and namespaces for modular apps
- Locale filtering, plus allow and deny patterns for groups
- Single-file re-export with
--sourcefor file watchers - Laravel's own validation, auth, pagination and password lines included
- Skips rewriting locale files whose lines did not change
- Overlapping runs wait for each other instead of overwriting each other's files
Documentation
The Tolki localization docs cover usage in depth:
- Exporting from Laravel
- Vite plugin, with its options, Sail setup and the virtual module
- Vue, React and Svelte bindings
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.