skysplit/laravel5-intl-translation 问题修复 & 功能扩展

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

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

skysplit/laravel5-intl-translation

Composer 安装命令:

composer require skysplit/laravel5-intl-translation

包简介

Laravel 5 package for better translation syntax using php-intl extension

README 文档

README

Build Status Latest Stable Version Latest Unstable Version

Introduction

Laravel Intl Translator uses php-intl extension to provide translation for your application.

Please mind that this package breaks framework default behaviour for validators.

Due to MessageFormatter::formatMessage method, Validator::replacer method should return array of parameters as key-value pair, instead replacing placeholders in message.

Besides that app('translator')->get($key) always returns message in raw format (unparsed). Translated messages are returned by:

app('translator')->trans($key)
app('translator')->formatMessage($locale, $message, $params)

Requirements

  • Laravel 6
  • php-intl extension installed

Please feel free to contribute to this package for other Laravel versions support!

Installation

If you do not have php-intl extension you can install it by following command (Ubuntu, Debian)

$ sudo apt-get install php-intl

If you have other OS, you can use it's respective package manager

All versions

In your config/app.php providers
Remove line

Illuminate\Translation\TranslationServiceProvider::class,
Illuminate\Validation\ValidationServiceProvider::class,

And add line:

Skysplit\Laravel\Translation\TranslationServiceProvider::class,
Skysplit\Laravel\Translation\ValidationServiceProvider::class,

Publishing config and language files

Be careful! This will override your existing resources/lang/{lang} files! Check Currently adapted locales table to see which files could be overriden.

php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --force

If you would like to publish only config

php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --tag=config

If you would like to publish only one language files set

php artisan vendor:publish --provider="Skysplit\Laravel\Translation\TranslationServiceProvider" --force --tag="lang.{locale}[,lang.{other_locale}]"

Currently adapted locales

Locale Published files
en auth.php, validation.php
pl auth.php, pagination.php, passwords.php, validation.php

Usage examples

Both trans() and trans_choice() helper functions use this translator, so the only thing you have to change is your language files.

For detailed documentation please visit php's MessageFormatter docs and links related there

Placeholders

app/resources/lang/en/custom.php

return [
	'placeholder' => 'Hello there, {username}!'
]

view.blade.php

{{ trans('custom.placeholder', ['username' => 'Jane']); }}

Returns

Hello there, Jane!

Select

app/resources/lang/en/custom.php

return [
	'select' => '{gender, select, male{He} female{She} other{It}} has two legs and is {gender}!'
]

view.blade.php

{{ trans('custom.select', ['gender' => 'male']); }}
{{ trans('custom.select', ['gender' => 'female']); }}
{{ trans('custom.select', ['gender' => 'penguin']); }}

Returns

He has two legs and is male!
She has two legs and is female!
It has two legs and is penguin!

Plurals

app/resources/lang/en/custom.php

return [
	'plural' => 'Jon has {n, plural, =0{no apples} one{# apple} other{# apples}}'
]

view.blade.php

{{ trans_choice('custom.plural', 0); }}
{{ trans_choice('custom.plural', 1); }}
{{ trans_choice('custom.plural', 2); }}

Returns

Jon has no apples
Jon has 1 apples
Jon has 2 apples

Instead of trans_choice() you can you use trans() helper as well.

resources/lang/en/custom.php

return [
	'custom.plural' => 'Jon has {0, plural, =0{no apples} one{# apple} other{# apples}}, {grapes, plural, =0{no grapes} one{# grape} other{# grapes} and {oranges, plural, =0{no oranges} one{# orange} other{# oranges}}'
];

view.blade.php

{{ trans('custom.plural', [3, 'grapes' => 1, 'oranges' => 0]) }}

Returns

Jon has 3 apples, 1 grape and no oranges

As you can see, the only thing trans_choice() do is passing first argument as n parameter to trans() helper.

Plural offset

You can set offset for your plural rules. Consider this example:

You {n, plural, offset:1 =0{do not like this yet} =1{liked this} one{and one other person liked this} other{and # others liked this}}

Result:

You do not like this yet // n = 0
You liked this // n = 1
You and one other person liked this // n = 2
You and 2 others liked this // n = 3
You and 3 others liked this // n = 4

Plural rule are often very complex for languages. Intl does handle it for you.
For example in Polish few rule is applied when n % 10 = 2..4 and n % 100 != 12..14, while many rule is applied when n != 1 and n % 10 = 0..1 or n % 10 = 5..9 or n % 100 = 12..14.
In Serbian =1 will match when n = 1, but one will apply when n = 1, 21, 31, 41 etc.

Remember! You always have to provide other rule for plural translations.

For more details about pluralization please visit CLDR Plural Rules specificaton and CLDR Language plural rules.

Formatting in details

PHP's MessageFormatter also supports ordinal, spellout, number, date, time and duration formatting.
For detailed information please visit this great Yii2 Framework i18n Guide which covers every intl topic wonderfully.

skysplit/laravel5-intl-translation 适用场景与选型建议

skysplit/laravel5-intl-translation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 106.4k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2016 年 05 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 skysplit/laravel5-intl-translation 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 106.4k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 26
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-14