定制 sertxudeveloper/laravel-translatable 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sertxudeveloper/laravel-translatable

Composer 安装命令:

composer require sertxudeveloper/laravel-translatable

包简介

Manage localized routes and use translatable models in a Laravel app.

README 文档

README

Laravel Translatable by Sertxu Developer

Laravel Translatable

Manage localized routes and use translatable models in a Laravel app.

  • Automatically register the routes for each locale you wish to support.
  • Optionally remove the locale slug from the URL for your main language.
  • Generate localized route URLs using the trans_route() helper.
  • Allow routes to be cached.

Requirements

  • PHP >= 7.4
  • Laravel >= 8.x

Installation

You can install this package using Composer.

composer require sertxudeveloper/laravel-translatable

Configuration

First you should publish the configuration file.

php artisan vendor:publish --tag='trans-config'

After running this command, you will now find a translatable.php`` file in the config` folder.

Locales available

You can set the locales availables in your application. The localized routes will be registered with all of these locales.

'locales' => ['es', 'en', 'it'],

Fallback locale

The fallback locale should be the one that not require to be translated.

'fallback_locale' => 'es',

You can also hide the fallback locale from the URL prefix.

"hide_fallback_locale" => true,

Routes

Register Routes

All the routes you want lo localize should be registered inside the Route::localized closure.

// Not Localized
Route::get('home', [HomeController::class, 'index'])->name('home');

// Localized
Route::localized(function () {
  Route::get('blog', [BlogController::class, 'index'])->name('blog')
  Route::get('{slug}', [PageController::class, 'index'])->name('page')
});

// Not Localized
Route::get('about', [AboutController::class, 'index'])->name('about');

Inside this closure you can use Route Groups such as Middlewares, Namespaces or even Sub-Domain Routing. This closure will prepend the locale to the route's URI and name.

This will be the result of the viewed configuration examples.

URI Name Locale
/home home es - en - it
--- --- ---
/blog blog es
/es/blog es.blog es
/en/blog en.blog en
/it/blog it.blog it
--- --- ---
/{slug} page es
/es/{slug} es.page es
/en/{slug} en.page en
/it/{slug} it.page it
--- --- ---
/about about es - en - it

Beware that you don't register the same URL twice when omitting the locale. You can't have a localized /about route and also register a non-localized /about route in this case. The same idea applies to the / (root) route! Also note that the route names still have the locale prefix.

Generate Localized URLs

You should use the trans_route helper in order to get the requested route localized. To this helper you can pass a route name or a route url, in booth cases it will be localized.

trans_route($name, $parameters = [], $absolute = false, $locale = null)

If you pass only the route it will be localized using the current locale ('en').

trans_route('blog') // /en/blog

You can also pass params to the helper.

trans_route('page', ['help']) // /en/help

The third param is a boolean to make it absolute or not.

trans_route('page', ['help'], true) // https://yourdomain.test/en/help
trans_route('page', ['help'], false) // /en/help

The last param is used for specify the locale to use.

trans_route('blog', [], false, 'it') // /it/blog

Switch Locale

If you're building a dropdown or similar with links to change the locale of the application, you should use the switch_to_locale helper.

switch_to_locale('en') // Changes to 'en' locale without changing the route

Eloquent translations

Create translations tables

You can to customize the name of the translations tables.

'table_suffix' => '_translations'

The usage of this value will be the following one. If you have the model Page with the trait HasTranslations and the model table is pages. This package will look up for the translations at the table page_translations. Always the model table followed by the table suffix in the config file.

The translations tables should contain the translatable fields from the model, the id, a column locale to specify the language saved, created_at and updated_at. The column deleted_at should never be in the translations table, regardless the models is softDeleted or not.

As you can see in the following example,

Pages table

id name slug excerpt body image status created_at updated_at
int varchar varchar varchar text varchar enum datetime datetime

Pages translation table

id locale name excerpt body created_at updated_at
int varchar(2) varchar varchar text datetime datetime

Get Eloquent Translated Attribute

In order to get a translated attribute you should use the getTranslated method.

$post = Post::find(1);
echo $post->getTranslated('name');

Cache Routes

In production, you can safely cache your routes per usual.

php artisan route:cache



Copyright © 2022 Sertxu Developer

sertxudeveloper/laravel-translatable 适用场景与选型建议

sertxudeveloper/laravel-translatable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.6k 次下载、GitHub Stars 达 18, 最近一次更新时间为 2019 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translatable」 「laravel」 「localization」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 3.6k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 18
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-04