bambamboole/laravel-i18next

A laravel package which provides a HTTP backend for i18next.js

0.3.0 2024-08-09 21:10 UTC

This package is auto-updated.

Last update: 2024-09-11 12:21:14 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

If you are using i18next in your frontend and Laravel in your backend, this package is for you.

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.

Installation

You can install the package via composer.

composer require bambamboole/laravel-i18next

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

The route

With Vue.js

To use the translations in your Vue.js components you can use the i18next-vue package.

npm install -D i18next-vue i18next-http-backend

To configure i18next-vue you can use the following code snippet:

import I18NextVue from 'i18next-vue';
import HttpBackend from 'i18next-http-backend'

i18next.use(HttpBackend).init({
    saveMissing: true,
    lng: 'en',
    backend: {
        // This is needed for CSRF protection
        withCredentials: true,
        customHeaders: () => {
            const csrf = document.querySelector<HTMLElement>('meta[name="csrf-token"]')

            return csrf === null ? {} : {'X-CSRF-TOKEN': csrf.getAttribute('content')}
        },
    },
});

//...

app.use(I18NextVue, {i18next})

Testing

composer test

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 manuel@christlieb.eu instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.