joggapp/laravel-google-translate
最新稳定版本:v9.0.0
Composer 安装命令:
composer require joggapp/laravel-google-translate
包简介
Laravel package for the Google Translate API
README 文档
README
Laravel package for the Google Translate API
This package makes using the Google Translate API in your laravel app a breeze with minimum to no configuration, clean syntax and a consistent package API.
Installation
- You can install this package via composer using this command:
composer require joggapp/laravel-google-translate
-
The package will automatically register itself.
-
We have documented how to setup the project and get the necessary configurations from the Google Cloud Platform console in a step by step detailed manner over here.
-
You can publish the config file using the following command:
php artisan vendor:publish --provider="JoggApp\GoogleTranslate\GoogleTranslateServiceProvider"
This will create the package's config file called googletranslate.php in the config directory. These are the contents of the published config file:
return [ /* |---------------------------------------------------------------------------------------------------- | The ISO 639-1 code of the language in lowercase to which the text will be translated to by default. |---------------------------------------------------------------------------------------------------- */ 'default_target_translation' => 'en', /* |------------------------------------------------------------------------------- | Path to the json file containing the authentication credentials. | | The process to get this file is documented in a step by step detailed manner | over here: | https://github.com/JoggApp/laravel-google-translate/blob/master/google.md |------------------------------------------------------------------------------- */ 'api_key' => env('GOOGLE_TRANSLATE_API_KEY'), ];
How to use
-
After setting up the config file values you are all set to use the following methods ????
-
Detecting the language. You can pass both, a single string or an array of multiple strings to it:
GoogleTranslate::detectLanguage('Hello world'): array GoogleTranslate::detectLanguage(['Hello world', 'Laravel is the best']); // Returns multi-dimensional array containing result set for all the array elements.
- Translating the string(s): The
translatemethod accepts a third optional argument which can be the code of the language you want the string to be translated in. You can specify the default option in the config file:
// GoogleTranslate::translate($input, $from = null, $to = null, $format = 'text'): array GoogleTranslate::translate('Hello world'): array GoogleTranslate::translate(['Hello world', 'Laravel is the best']); // Returns multi-dimensional array containing result set for all the array elements.
- Get all the available translations from 'Google Translation' for a particular language by passing its language code:
GoogleTranslate::getAvailableTranslationsFor('en'): array
- Translate unless the language is same as the first argument. This method is a clean way to ask the package to detect the language of the given string, if it is same as the first argument, translation isn't performed. It accepts an optional third argument which is the language code you want the string to be translated in. You can specify the default option in the config file. If the languages are same, the input string is returned as it is, else an array is returned containing the translation results:
GoogleTranslate::unlessLanguageIs('en', string $text);
- Translating and just returning back the translated string. It accepts an optional second argument which is the language code you want the string to be translated in. You can specify the default option in the config file.
GoogleTranslate::justTranslate(string $text): string
- There is is an optional third parameter for format to take advantage for better html translation support. Google Translate API currently supports 'text' and 'html' as parameters. The default for this parameter is 'text' as it has the best use case for most translations. Google Translate API Docs
GoogleTranslate::unlessLanguageIs('en', string $text, string $format);
- There is also a nice blade helper called
@translatethat comes with the package to make its use more neat in the view files. It accepts an optional second argument which is the language code you want the string to be translated in. You can specify the default option in the config file.
@translate('Hello World')
Testing
You can run the tests with:
vendor/bin/phpunit
Changelog
Please see the CHANGELOG for more information about what has changed recently.
Security
If you discover any security related issues, please email them to harish@jogg.co instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see the License File for more information.
joggapp/laravel-google-translate 适用场景与选型建议
joggapp/laravel-google-translate 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 236k 次下载、GitHub Stars 达 169, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「api」 「google」 「laravel」 「translate」 「google translate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 joggapp/laravel-google-translate 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 joggapp/laravel-google-translate 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 joggapp/laravel-google-translate 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PSR-7 compatible library for making CRUD API endpoints
Magento 2 Social Login extension is designed for quick login to your Magento 2 store without procesing complex register steps
Simple ASCII output of array data
Client for Google Directions API to add interpolated points to a route consisting of given coordinates.
Package for view storage in laravel
统计信息
- 总下载量: 236k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 170
- 点击次数: 14
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 未知
