atomjoy/trans

Laravel 数据库翻译。

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

This package is not auto-updated.

Last update: 2024-09-18 01:11:18 UTC


README

从数据库中提取Laravel翻译。

安装

composer require "atomjoy/trans"

composer update
composer dump-autoload -o

迁移,区域设置

# 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"
}

示例

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 ...';
 }
});