sawirricardo/laravel-translation-loader

Store your translations in the database or other sources, using the modern packages

0.0.3 2023-07-03 04:52 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

In a vanilla Laravel or Lumen installation you can use language files to localize your app. This package will enable the translations to be stored in the database. You can still use all the features of the trans function you know and love.

trans('messages.welcome', ['name' => 'dayle']);

You can even mix using language files and the database. If a translation is present in both a file and the database, the database version will be returned.

Support us

68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d7472616e736c6174696f6e2d6c6f616465722e6a70673f743d31

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require sawirricardo/laravel-translation-loader

You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-translation-loader-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="laravel-translation-loader-config"

This is the contents of the published config file:

'translation_loaders' => [
    \Sawirricardo\LaravelTranslationLoader\TranslationLoaders\Db::class,
],

'model' => \Sawirricardo\LaravelTranslationLoader\Models\Translation::class,

'translation_manager' => \Sawirricardo\LaravelTranslationLoader\TranslationLoaderManager::class,

'locals' => [
    'en' => 'English',
    'ar' => 'Arabic',
    'pt_BR' => 'Português (Brasil)',
    'my' => 'Burmese',
    'id' => 'Bahasa Indonesia',
],

'paths' => [
    app_path(),
    resource_path('views'),
    base_path('vendor'),
],

'excluded_paths' => [
    //
],

Usage

use Sawirricardo\LaravelTranslationLoader\Models\Translation;

Translation::create([
   'group' => 'validation',
   'key' => 'required',
   'text' => ['en' => 'This is a required field', 'nl' => 'Dit is een verplicht veld'],
]);

You can fetch the translation with Laravel's default trans function:

trans('validation.required'); // returns 'This is a required field'

app()->setLocale('nl');

trans('validation.required'); // returns 'Dit is een verplicht veld'

Creating your own translation providers

This package ships with a translation provider than can fetch translations from the database. If you're storing your translations in a yaml-file, a csv-file, or ... you can easily extend this package by creating your own translation provider.

A translation provider can be any class that implements the Sawirricardo\LaravelTranslationLoader\TranslationLoaders\TranslationLoader-interface. It contains only one method:

namespace Sawirricardo\LaravelTranslationLoader\TranslationLoaders;

interface TranslationLoader { public function loadTranslations($locale, $group); } Translation providers can be registered in the translation_loaders key of the config file.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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