naimkhalifa / laravel-google-cloud-translation
一个用于轻松集成 Google Cloud Translation API 与 Laravel 的包
Requires
- php: ^8.1
- google/cloud-translate: ^1.15
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- mockery/mockery: ^1.6
- nunomaduro/collision: ^7.9
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^8.8
- pestphp/pest: ^2.0
- pestphp/pest-plugin-arch: ^2.0
- pestphp/pest-plugin-laravel: ^2.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
This package is auto-updated.
Last update: 2024-09-08 22:04:54 UTC
README
安装
您可以通过 composer 安装此包
composer require naimkhalifa/laravel-google-cloud-translation
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="NaimKhalifa\GoogleCloudTranslation\GoogleCloudTranslationServiceProvider"
这是已发布配置文件的内容
return [ /* |-------------------------------------------------------------------------- | Google Cloud Translation API Key |-------------------------------------------------------------------------- | | Your Google Cloud Translation API Key. | You can get it from https://console.cloud.google.com/apis/credentials | More info at https://cloud.google.com/translate/docs/setup | */ 'api_key' => env('GOOGLE_CLOUD_TRANSLATION_API_KEY'), /* |-------------------------------------------------------------------------- | Default Source Language |-------------------------------------------------------------------------- | | The default source language to translate from. | You can find the list of supported languages here: | https://cloud.google.com/translate/docs/languages | */ 'default_source_language' => 'en', /* |-------------------------------------------------------------------------- | Default Target Language |-------------------------------------------------------------------------- | | The default target language to translate to. | Obviously, it must be different from the default source language. | */ 'default_target_language' => 'es', /* |-------------------------------------------------------------------------- | Default Format |-------------------------------------------------------------------------- | | The default format to translate to. | Acceptable values are html or text. | */ 'default_format' => 'text', ];
使用方法
GoogleCloudTranslation::translate(string $text, array $options)
如果您想翻译单个文本字符串,请使用此方法。
# This would translate using default_source_language # and default_target_language config values GoogleCloudTranslation::translate('Hello World'); # with options GoogleCloudTranslation::translate('Hello World', [ 'source' => 'en', 'target' => 'fr', 'format' => 'text', # or 'html' ]);
GoogleCloudTranslation::translateBatch(array $input, array $options)
如果您想一次翻译多个字符串,请使用此方法。
GoogleCloudTranslation::translateBatch([ 'Something else', 'Another string to translate', 'And a third one because we can' ]) # You can also pass options to this method GoogleCloudTranslation::translateBatch('Hello World', [ 'source' => 'en', 'target' => 'fr', 'format' => 'text', # or 'html' ]);
命令
翻译语言文件
此 Artisan 命令允许您使用 Google Cloud Translation 翻译语言文件中的字符串。
您要翻译的文件的格式应遵循您在 Laravel /lang
文件夹下通常找到的格式。
php artisan google-cloud-translation:translate-lang-file [options]
选项
--source
: 指定翻译的源语言。(必需)
--target
: 指定翻译的目标语言。(必需)
--file
: 指定要翻译的语言文件的路径。(必需)
--dryrun
: 启用 dry run 模式,将显示翻译而不会保存到文件中。 (可选,默认启用)
示例
php artisan google-cloud-translation:translate-lang-file --source=en --target=fr --file=resources/lang/en/messages.php
这是您在启用 --dryrun 选项的情况下运行命令时的情况(默认启用)。
此命令将翻译 messages.php 语言文件中的所有字符串,从英语(en)翻译成法语(fr),并在终端显示翻译结果。
然后您需要通过提示确认以将翻译保存到目标文件(在本例中,为 resources/lang/fr/messages.php
)。
请注意,这将覆盖该路径上存在的任何现有文件!基本上是替换它。
如果您想绕过确认并直接写入目标,您可以禁用 dryrun 模式(--dryrun=false
)
php artisan google-cloud-translation:translate-lang-file --source=en --target=fr --file=resources/lang/en/messages.php --dryrun=false
测试
composer test
更新日志
有关最近更改的更多信息,请参阅 更新日志。
贡献
有关详细信息,请参阅 贡献指南。
安全漏洞
有关如何报告安全漏洞的信息,请参阅 我们的安全策略。
致谢
许可协议
MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件。