定制 in10/multilanguage 二次开发

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

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

in10/multilanguage

Composer 安装命令:

composer require in10/multilanguage

包简介

Simple, opinionated multilanguage package for Laravel

README 文档

README

Simple, opinionated multilanguage package for Laravel

Contents

Requirements

  • PHP 7.1 - 7.3
  • Laravel 5.7

Design

This is an opinionated package: it works in a specific way, based on the setups we run at IN10. That means:

  1. All translated routes start with a route part, e.g. example.com/de/news/
  2. A website has a pre-defined set of languages, all other languages return a 404.
  3. A language is always two characters.
  4. The homepage is translated.
  5. The website has a single default language, by default "en" which you can change in the configuration. This default language is excluded from the URL via a 301-redirect. If you visit example.com/en/test, it will be redirected to example.com/test.

Installation

Install the package using composer:

composer require "in10/multilanguage"

and publish the configuration file:

php artisan vendor:publish --provider=IN10\\Multilanguage\\ServiceProvider

You can customize this file as needed.

Migrating from ARCANEDEV/localization

One of the packages we used to use at IN10 is ARCANEDEV/Localization. To facilitate an easy upgrade from the package to this new, smaller package, execute the following steps:

  1. Remove the existing package: composer remove arcanedev/localization.
  2. Remove the ServiceProvider and configuration file if needed.
  3. Follow the steps in the Installation section above to install IN10/multilanguage.
  4. Search through your project for the following instances of Localization-specific code that must be replaced
Search for Replace with Remarks
localization()->getCurrentLocale() App::getLocale() Don't forget to import the Facade
Localization::getCurrentLocale() App::getLocale() Don't forget to import the Facade
config('localization.supported-locales') config('languages.supported-languages
  1. Test your project thoroughly to check if all translated routes and features still work.

Usage

Setting up groups

You can make a set of routes translated by wrapping them in a group:

Route::multilanguage([], function() {
    Route::get('/', 'HomepageController')->name('homepage');
    Route::get('/news/{slug}', 'HomepageController')->name('news.show');
});

The first parameter attributes takes the same settings as a regular route group, except for prefix, as and middleware, which are overwritten (these parameters are required to make the translation work). The multilanguage-group should be a root-level construct. Adding it inside of another group or prefix is not tested and probably won't work.

Route translation

In some cases, you might want to translate slugs in the URL. A common example is the /en/news/an-article and /nl/nieuws/een-artikel variants of URLs. This can be accomplished using the transGet routing function:

Route::multilanguage([], function() {
    Route::transGet('news.show');
});

Note that using transGet outside of a multilanguage routing group will not work.

The translation key is automatically looked up in the routes.php translation file. All translation routes must always be translated. Don't fret: the package will scream at you if you're missing a translation.

// resources/lang/en/routes.php
return [
    'news.show' => 'news/{slug}',
];

// resources/lang/nl/routes.php
return [
    'news.show' => 'nieuws/{slug}',
];

Route generation

If you want to generate a route with a correct language, use the included helper:

function translatedRoute(string $route, array $parameters = [], bool $absolute = true, ?string $language = null) : string

This helpers takes the same parameters as the Laravel route() helper, with an optional language as a last parameter. If you omit the language, the helper uses the current language for the request. This is usually what you want, so in general you can use the translatedRoute helper as if it were the regular helper:

translatedRoute('news.show', ['slug' => 'five-ways-to-translate-content');

which will generate /nl/news/five-ways-to-translate-content in this example if the current language is set to Dutch.

Developer

Jakob Buis

License

Copyright 2019 IN10. All rights reserved.

in10/multilanguage 适用场景与选型建议

in10/multilanguage 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.19k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 01 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 in10/multilanguage 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 7
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: proprietary
  • 更新时间: 2019-01-23