topview-digital/laravel-translation-helper

1.1.2 2019-03-20 08:52 UTC

README

GitHub release Scrutinizer Code Quality Build Status Code Intelligence Status Language License Total Downloads HitCount

Laravel 翻译助手

将代码中的术语本地化,并将翻译存储在表中或导出到文本文件。

实现字符串本地化和存档翻译到表或导出到文本文件的内联翻译,当您启用了默认队列的 Google 访问和队列功能时,它将帮助您通过 Google 翻译自动生成其他所需语言。

要求

  • PHP >= 7.0
  • MySQL >= 5.7
  • Laravel >= 5.5

安装

通过 Composer 需求包

composer require topview-digital/laravel-translation-helper

Laravel 将自动注册 ServiceProvider

发布包

安装后,请通过以下命令发布资产

php artisan trans-helper:publish

配置包

请在 config/trans-helper.php 文件中配置您的设置,它应该看起来像下面这样

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Laravel-Translation-Helper Database Settings
    |--------------------------------------------------------------------------
    |
    | Here are database settings for Laravel-Translation-Helper builtin tables connction.
    |
    */

    'database' => [
        // Database connection for following tables.
        'connection' => '',

    ],

    /*
    |--------------------------------------------------------------------------
    | Laravel-Translation-Helper Citing Settings
    |--------------------------------------------------------------------------
    |
    | Here are citing settings for Laravel-Translation-Helper.
    |
    */

    'cite' => [
        'enable' => true,
        'async' => true,

    ],

    /*
    |--------------------------------------------------------------------------
    | Laravel-Translation-Helper Translation Settings
    |--------------------------------------------------------------------------
    |
    | Here are translating settings for Laravel-Translation-Helper trigger auto translation.
    |
    */

    // Translation mode setting
    'translation' => [
        'broker' => TopviewDigital\TranslationHelper\Service\GoogleTranslator::class,
        'mode' => 'auto',
    ],

    /*
    |--------------------------------------------------------------------------
    | Laravel-Translation-Helper Exporting Settings
    |--------------------------------------------------------------------------
    |
    | Here are exporting settings for Laravel-Translation-Helper.
    |
    */

    // Exporting data config setting.
    'export' => [
        'path' => realpath(base_path('resources/lang')),
    ],
];

一旦您配置了设置,您可以通过运行安装命令来设置包的表。

php artisan trans-helper:install

配置队列

如果您想使用自动翻译功能,请也配置您的队列配置文件和 .env 文件。如果您已为默认队列启用了队列功能,请跳过以下说明。config/queue.php[示例]

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Queue Connection Name
    |--------------------------------------------------------------------------
    |
    | Laravel's queue API supports an assortment of back-ends via a single
    | API, giving you convenient access to each back-end using the same
    | syntax for every one. Here you may define a default connection.
    |
    */

    'default' => env('QUEUE_CONNECTION', 'sync'),

    /*
    |--------------------------------------------------------------------------
    | Queue Connections
    |--------------------------------------------------------------------------
    |
    | Here you may configure the connection information for each server that
    | is used by your application. A default configuration has been added
    | for each back-end shipped with Laravel. You are free to add more.
    |
    | Drivers: "sync", "database", "beanstalkd", "sqs", "redis", "null"
    |
    */

    'connections' => [

        'sync' => [
            'driver' => 'sync',
        ],

        'database' => [
            'driver' => 'database',
            'table' => '_jobs',
            'queue' => 'default',
            'retry_after' => 90,
        ],

        'beanstalkd' => [
            'driver' => 'beanstalkd',
            'host' => 'localhost',
            'queue' => 'default',
            'retry_after' => 90,
            'block_for' => 0,
        ],

        'sqs' => [
            'driver' => 'sqs',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
            'queue' => env('SQS_QUEUE', 'your-queue-name'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | Failed Queue Jobs
    |--------------------------------------------------------------------------
    |
    | These options configure the behavior of failed queue job logging so you
    | can control which database and table are used to store the jobs that
    | have failed. You may change them to any database / table you wish.
    |
    */

    'failed' => [
        'database' => env('DB_CONNECTION', 'mysql'),
        'table' => '_failed_jobs',
    ],

];

.env[队列部分]

QUEUE_CONNECTION=database

配置完成后,请确保您的队列已启动并运行。简单的方法是运行

php artisan queue:work --queue=translation &
php artisan queue:work --queue=cite &

并在后台持续运行。

标记引用代码功能

您可以通过设置网络界面来跟踪翻译。然而,有时很难回忆起术语在哪里被使用。您可以开启引用功能,这将帮助您记录术语在代码中的引用位置。

定义自己的翻译器

您可以通过引用以下代码来定义自己的翻译器,并将其设置在配置中。

<?php

namespace TopviewDigital\TranslationHelper\Service;

use Campo\UserAgent;
use Stichoza\GoogleTranslate\GoogleTranslate;
use TopviewDigital\TranslationHelper\Interfaces\TranslatorInterface;

class GoogleTranslator implements TranslatorInterface
{
    protected $break = 0;
    protected $called = 0;
    protected $word;
    protected $source_locale = null;
    protected $target_locale;

    public function __construct()
    {
        $this->target_locale = config('app.locale');
    }

    public function word(string $word)
    {
        $this->word = $word;
        return $this;
    }

    public function targetLocale(string $target_locale)
    {
        $this->target_locale = $target_locale;
        return $this;
    }

    public function sourceLocale(string $source_locale)
    {
        $this->source_locale = $source_locale;
        return $this;
    }

    public function translate()
    {
        $translated = '';
        $translator = new GoogleTranslate();
        try {
            $translated = is_null($this->source_locale)
                ? $translator
                ->setSource()
                ->setTarget($this->target_locale)
                ->translate($this->word)
                : $translator
                ->setSource($this->source_locale)
                ->setTarget($this->target_locale)
                ->translate($this->word);
        } catch (Exception $e) {
            return null;
        }
        return $translated;
    }
}

用法

对于以下示例

翻译

您可以使用 helper 函数 localize() 包装您的字符串,没有参数涉及

$form->select('mode', localize('项目模式'))->options([localize('1对1单人模式'), localize('团队多人模式')]);

并且辅助程序将根据您当前的 Laravel 用户区域设置将字符串翻译成相关语言,同时您启用了 Laravel 队列功能,并且队列默认在后台运行。

清扫

随着代码中字符串的处理变化很大,您可能需要手动运行命令

php artisan trans-helper:sweep

或通过 helper 函数 sweep() 在您的代码中调用清扫操作

您也可以在不运行代码之前手动触发自动翻译,通过调用命令

php artisan trans-helper:trans

或在您的代码中通过调用 helper 函数 translate($locales=[]), 入参是你想要翻译的区域代码,如 ['en','zh-CN','br','de'...], 默认区域代码是 config('app.locale'), config('app.fallback_locale'), config('app.faker_locale').

导出

您可以使用翻译功能而不需要文本语言文件,您真的不需要它们。您可以使用导出命令来获取它们

php artisan trans-helper:export

或通过代码或 tinker 环境中的 helper 命令 export($path=null,$locales=null) 来调用它,它将帮助您导出已翻译到表中的所有区域或您指定的区域。注意:如果您指定的区域还没有任何翻译,它将使用区域 config('app.locale') 的翻译。

导出的语言文件将使用名为 localize() 的辅助函数调用的文件命名空间命名。

希望您喜欢它!谢谢!

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件