tleckie/translate
Composer 安装命令:
composer require tleckie/translate
包简介
Fast, powerful, scalable and customizable php translator library
README 文档
README
Fast, powerful, scalable and customizable php translator library
You can install the package via composer:
composer require tleckie/translate
Usage
Create an instance of the Translator class to which you will assign a loader. The loader must have the directory in which the translation files are stored, it must also indicate the extension of the translation files.
<?php use Tleckie\Translate\Loader\ArrayLoader; use Tleckie\Translate\Translator; $trans = new Translator( new ArrayLoader('./translations/','php'), 'es_ES' ); $trans->trans('hello'); $trans->trans('other.value');
Your translations file should look like this:
<?php return [ 'hello' => "Hola! Bienvenido a mi sitio web!", 'other.value' => "Otro valor", . . . ];
The name of the file that stores the translation should look like this:
/translations/es_ES.php
The trans() method takes multiple arguments. The first is the key of the translation array, it is the value that will be searched for in its corresponding file.
The second argument (optional) is an array of values to be replaced in the translated value. If you want to add values to your translated text you must indicate it with "%s". Note that the number of arguments must match the number of "%s".
<?php use Tleckie\Translate\Loader\ArrayLoader; use Tleckie\Translate\Translator; $trans = new Translator( new ArrayLoader('./translations/','php'), 'es_ES' ); $trans->trans('hello',['John']);
Your translations file should look like this:
<?php return [ 'hello' => "Hola %s! Bienvenido a mi sitio web!" ];
Even if you configure your translator to load a specific language, you can also change the language at any time if you need.
<?php use Tleckie\Translate\Loader\ArrayLoader; use Tleckie\Translate\Translator; $trans = new Translator( new ArrayLoader('./translations/','php'), 'es_ES' ); $trans->trans('hello',['John'],null, 'en_GB');
You can create your translation files by specifying the language and country defined by the provided locale. You can also decide whether to use the same language for different countries. Regional configuration example: "en_US" and "en_GB" In this case, you just need to create a file that has the following name:
/translations/en.php
In that case the following calls will have the same result and will load the translations from the same file.
<?php use Tleckie\Translate\Loader\ArrayLoader; use Tleckie\Translate\Translator; $trans = new Translator( new ArrayLoader('./translations/','php'), 'es_ES' ); $trans->trans('hello',['John'],null, 'en_GB'); $trans->trans('hello',['John'],null, 'en_US');
You can also implement your own loader to connect to your preferred data source. You simply have to implement the LoaderInterface interface.
That's all! I hope this helps you ;)
tleckie/translate 适用场景与选型建议
tleckie/translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 387 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「translator」 「translate」 「translated」 「php-8」 「tleckie」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tleckie/translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tleckie/translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tleckie/translate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use i18n translation PHP class for multi-language websites
Store your language lines in the database, yaml or other sources
An Astrotomic Translatable extension for Laravel Nova.
腾讯翻译君翻译、百度翻译 、谷歌翻译(Google 翻译)
DeepL integration for Craft CMS
Yii2 integration for AirBnB Polyglot.js
统计信息
- 总下载量: 387
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-05-12