keasy9/laravel-localize 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

keasy9/laravel-localize

最新稳定版本:3.0

Composer 安装命令:

composer require keasy9/laravel-localize

包简介

A small Laravel package that helps with site localization.

README 文档

README

Installation:

  • composer require keasy9/laravel-localize
  • php artisan vendor:publish --provider='Keasy9\Localize\Providers\LocalizeServiceProvider'
  • php artisan migrate

Usage:

Adding uri-prefix:

At first you need to add available locales in config/localize.php. By default here is two locales:

'available_locales' => [
    'ru' => 'русский',
    'en' => 'english',
],

And add prefix for your app's routes:

namespace App\Providers;

use Keasy9\Localize\Facades\Localize;

class RouteServiceProvider extends ServiceProvider
{

    public function boot(): void
    {

        $this->routes(function () {
            //...

            Route::middleware('web')
                ->prefix(Localize::getLocalePrefix()) //prefix
                ->group(base_path('routes/web.php'));
        });
    }
}

Or in routes/web.php:

use Illuminate\Support\Facades\Route;
use Keasy9\Localize\Facades\Localize;

Route::prefix(Localize::getLocalePrefix())->group(function() {
    //...
});

Localize model fields:

Firstly you need run command php artisan migrate that creates table "translations" in your DB. Secondly all models that need translation must use trait and have property:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Keasy9\Localize\Traits\HasTranslations;

class Post extends Model
{
    use HasTranslations;

    // the array must include all fields that can be translated
    public static array $translated = [
        'title',
        'text',
    ];

}

so when you try to get a model attribute you will get a translated version of it:

{{ $post->title }} {{-- will returns title translated to current app locale --}}

Also you can translate all attributes:

$post->translate();

but be carefully. If you save translated model, fields in your database will be translated:

use App/Models/Post;

$post->translate()->save();
dd(Post->find($post->id)->title); //will dump translated title

Translate all attributes for all models in collection:

$posts->translate();

Web-interface for editing lang/**.json files and localize models:

This package also provides a simple web interface at http(s)://yourSiteRoot/localize/. But if you need to change this URI, you can do it through the configuration in config/localize.php:

'uri' => 'localize',

You can also specify a middleware to access that URL or remove it to open access to all users:

'access' => ['web','auth'],

All models added to the configuration will be available for translation via the web interface:

'translated_models' => [
    Post::class
],

And you can exclude default locale from editing also in config:

'default_locale' => 'en',
'translate_default_locale' => false,

Now it's time for you to create an amazing web application!

keasy9/laravel-localize 适用场景与选型建议

keasy9/laravel-localize 是一款 基于 Blade 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 11 月 30 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 keasy9/laravel-localize 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 keasy9/laravel-localize 我们能提供哪些服务?
定制开发 / 二次开发

基于 keasy9/laravel-localize 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 44
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: Blade

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-30