Download the PHP package bambamboole/laravel-i18next without Composer
On this page you can find all versions of the php package bambamboole/laravel-i18next. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bambamboole/laravel-i18next
More information about bambamboole/laravel-i18next
Files in bambamboole/laravel-i18next
Package laravel-i18next
Short Description A laravel package which provides a HTTP backend for i18next.js
License MIT
Homepage https://github.com/bambamboole/laravel-i18next
Informations about the package laravel-i18next
Laravel i18next
If you are using i18next in your frontend and Laravel in your backend, this package is for you.
Requirements
- PHP 8.3+
- Laravel 11, 12 or 13
How does it work?
The package provides the routes to fetch the translations and to save missing translations when using i18next-http-backend.
it supports Laravels JSON and PHP translation files and converts them on the fly to be compatible with i18next.
PHP files in sub directories are namespaced by their path, so lang/en/entities/salesOrder.php is exposed under the entities.salesOrder.* keys.
Pluralization
Laravel plural strings are converted to i18next plurals:
| Laravel | i18next |
|---|---|
one apple\|:count apples |
key_one, key_other |
{0} no files\|{1} one file\|[2,*] :count files |
key_interval (see below) |
Simple one\|other strings become standard i18next _one/_other plurals.
Explicit count/range forms ({0}, {1}, [2,*], …) are converted to the
i18next-intervalplural-postprocessor
format, e.g. (0)[no files];(1)[one file];(2-inf)[{{count}} files];. Add that
postprocessor on the frontend to resolve them.
Installation
You can install the package via composer.
Configuration
Publish the config to customise routing, the missing-translation endpoint and caching:
Set 'output' => 'nested' if you'd rather receive a nested JSON tree and keep i18next's default dot key separator (no keySeparator: false needed).
Namespaces
Set 'namespaces' => true to load translations as i18next namespaces, where the
namespace is the path of the group file under lang/{locale}:
| Request | Source | i18next |
|---|---|---|
/locales/en/translation.json |
lang/en.json (root strings) |
t('key') |
/locales/en/auth.json |
lang/en/auth.php |
t('auth:key') |
/locales/en/entities/salesOrder.json |
lang/en/entities/salesOrder.php |
t('entities/salesOrder:title') |
/locales/en/lattice.json |
packages/lattice/lang/en/messages.php |
useTranslation('lattice'), then t('messages.key') |
Keys are returned unprefixed (the namespace is the prefix). Configure the backend
with loadPath: '/locales/{{lng}}/{{ns}}.json' and list your namespaces in ns.
For Laravel package translations, use the Laravel package namespace as the i18next
namespace, for example lattice. Package group files are exposed below it as
dotted keys, for example messages.key. The package must register the path with
loadTranslationsFrom($path, 'lattice').
When namespaces are disabled, package translations are exposed with their full
Laravel keys, for example lattice::messages.key. Keys without a :: namespace
continue to target normal app translations.
Saving missing keys for a slashed namespace reconstructs the full dotted key (
entities.salesOrder.subtitle). With alaravel-translation-dumperversion that supports writing into existing nested files, it lands back inentities/salesOrder.php; otherwise it falls into a flatentities.php.Saving missing keys for a Laravel package namespace writes back into the registered package lang path, for example
messages.subtitlein thelatticei18next namespace is written aslattice::messages.subtitleand lands inpackages/lattice/lang/en/messages.php.Security: the store route writes translation files. Disable it in production (
I18NEXT_SAVE_MISSING=false) or put it behind auth viasave_missing.middleware.
Usage
The package is still in its early development and therefor pretty opinionated and not very flexible.
It provides two routes. One is for fetching the translations and the other one is for saving missing translations.
Fetching translations
GET /locales/{locale}/translation.json returns all translations for the locale, converted to the i18next format. Point i18next-http-backend's loadPath at it.
Saving missing translations
POST /locales/add/{locale}/translation persists the keys reported by i18next's saveMissing. Point i18next-http-backend's addPath at it (and send the CSRF token, see below).
With Vue.js
To use the translations in your Vue.js components you can use the i18next-vue package.
To configure i18next-vue you can use the following code snippet:
With React (Inertia v3)
Install i18next and the React bindings alongside your Inertia app:
Initialise i18next in your Inertia entry point (resources/js/app.tsx) and wrap the app with I18nextProvider. Resolving the init() promise before createInertiaApp avoids a flash of untranslated keys on first paint:
Then translate inside your pages with the useTranslation hook:
Set
keySeparator: falseso i18next looks up the flat, dotted keys the package emits (test.greeting) verbatim instead of treating the dots as nesting.
A t / tp helper
Interval plurals need the _interval suffix and postProcess: 'interval' on every call. A tiny wrapper hides that — t for everything (including standard plurals), tp for interval plurals:
Outside of React, bind the same two functions to the i18next instance directly:
Testing
Demo
Run a live demo page that drives i18next in the browser against the package routes:
Contributing
Ideas/Roadmap
- Add more tests
- Make the package more flexible
- Support namespaces
- Support i18next multiload
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
Credits
- Manuel Christlieb
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-i18next with dependencies
bambamboole/laravel-translation-dumper Version ^2.2
illuminate/cache Version ^11.0 || ^12.0 || ^13.0
illuminate/filesystem Version ^11.0 || ^12.0 || ^13.0
illuminate/http Version ^11.0 || ^12.0 || ^13.0
illuminate/support Version ^11.0 || ^12.0 || ^13.0
illuminate/translation Version ^11.0 || ^12.0 || ^13.0
spatie/laravel-package-tools Version ^1.16 || ^2.0