Laravel 翻译加载器项目教程

Laravel 翻译加载器项目教程

laravel-translation-loaderStore your translations in the database or other sources项目地址:https://gitcode.com/gh_mirrors/la/laravel-translation-loader

1、项目的目录结构及介绍

spatie/laravel-translation-loader/
├── config/
│   └── translation-loader.php
├── database/
│   └── migrations/
│       └── 2018_01_01_000000_create_language_lines_table.php
├── src/
│   ├── TranslationLoaderManager.php
│   ├── TranslationLoaders/
│   │   └── Db.php
│   ├── LanguageLine.php
│   └── TranslationServiceProvider.php
├── tests/
│   └── LanguageLineTest.php
├── .editorconfig
├── .gitattributes
├── .gitignore
├── .php_cs
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
└── phpunit.xml.dist

目录结构介绍

  • config/: 包含项目的配置文件。
  • database/migrations/: 包含数据库迁移文件,用于创建语言行表。
  • src/: 包含项目的主要源代码。
    • TranslationLoaderManager.php: 翻译加载器管理类。
    • TranslationLoaders/Db.php: 数据库翻译加载器。
    • LanguageLine.php: 语言行模型。
    • TranslationServiceProvider.php: 服务提供者。
  • tests/: 包含测试文件。
  • 其他文件:包含项目的各种配置和文档文件。

2、项目的启动文件介绍

项目的启动文件主要是 src/TranslationServiceProvider.php。这个文件是 Laravel 服务提供者,负责注册和启动翻译加载器服务。

namespace Spatie\TranslationLoader;

use Illuminate\Support\ServiceProvider;
use Spatie\TranslationLoader\TranslationLoaders\Db;
use Spatie\TranslationLoader\TranslationLoaderManager;

class TranslationServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');

        $this->publishes([
            __DIR__.'/../config/translation-loader.php' => config_path('translation-loader.php'),
        ], 'config');
    }

    public function register()
    {
        $this->mergeConfigFrom(__DIR__.'/../config/translation-loader.php', 'translation-loader');

        $this->app->singleton(TranslationLoaderManager::class, function ($app) {
            return new TranslationLoaderManager($app);
        });

        $this->app->singleton(Db::class, function () {
            return new Db();
        });

        $this->app->alias(TranslationLoaderManager::class, 'translation-loader');
    }
}

启动文件介绍

  • boot(): 加载数据库迁移文件并发布配置文件。
  • register(): 注册翻译加载器管理类和数据库翻译加载器,并将其绑定到应用程序中。

3、项目的配置文件介绍

项目的配置文件位于 config/translation-loader.php。这个文件包含了翻译加载器的配置选项。

return [
    /*
     * Language lines will be fetched by these loaders. You can put any class here that implements
     * the Spatie\TranslationLoader\TranslationLoaders\TranslationLoader interface.
     */
    'translation_loaders' => [
        Spatie\TranslationLoader\TranslationLoaders\Db::class,
    ],

    /*
     * This is the model used by the Db Translation loader. You can put any model here
     * that extends Spatie\TranslationLoader\LanguageLine.
     */
    'model' => Spatie\TranslationLoader\LanguageLine::class,

    /*
     * This is the translation manager which overrides the default Laravel `translation loader`.
     */
    'translation_manager' => Spatie\TranslationLoader\TranslationLoaderManager::class,
];

配置文件介绍

  • translation_loaders: 定义了翻译加载器类,这里使用了数据库翻译加载器。
  • model: 定义了用于数据库翻译加载器的模型类。
  • translation_manager: 定义了翻译管理器类,用于覆盖 Laravel 默认的翻译加载器。

以上是 Laravel 翻译加载器项目的目录结构、启动文件和配置文件的详细介绍。希望这份教程能帮助你更好地理解和使用该项目。

laravel-translation-loaderStore your translations in the database or other sources项目地址:https://gitcode.com/gh_mirrors/la/laravel-translation-loader

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

羿舟芹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值