atomjoy/trans

Laravel database translations.

v1.0.2 2023-02-16 11:29 UTC

This package is not auto-updated.

Last update: 2024-05-28 23:43:34 UTC


README

Laravel translations from database.

Install

composer require "atomjoy/trans"

composer update
composer dump-autoload -o

Migrate, locales

# locales
php artisan lang:publish

# update tables
php artisan migrate

# new tables
php artisan migrate:fresh

Laravel trans

lang/pl.json

{
"This text not exists in db": "Ten tekst nie istnieje w bazie danych"
}

Examples

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use Trans\Models\Translate;

Route::get('/trans', function () {
 try {
  // Clear cache (in Seeder)
  Translate::clearCache();

  // Add translation for locale (in Seeder)
  Translate::add('Hello','Witaj', 'pl');

  // Change locale
  app()->setLocale('pl');

  // If exists in db
  echo "<br> PL " . trans_db('Hello');

  // If not exists in db get translation from default trans() helper
  echo "<br> PL " . trans_db('This text not exists in db');

 } catch (Exception $e) {
  report($e);
  return 'Errors ...';
 }
});