承接 topview-digital/laravel-translation-helper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

topview-digital/laravel-translation-helper

Composer 安装命令:

composer require topview-digital/laravel-translation-helper

包简介

Laravel Translation Helper

README 文档

README

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

Laravel Translation Helper

Localize the terms in your code and store translations in the tables or export to text files.

Implementations of inline translation for your strings required localization and archiving the translations int tables or exporting to text files, while you have google access and queue function enabled for default queue, it will help you to generate the other required languages automatically via google translation.

Requirements

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

Installation

Require the package via Composer:

composer require topview-digital/laravel-translation-helper

Laravel will automatically register the ServiceProvider.

Publish Package

After installation, please publish the assets by below commands

php artisan trans-helper:publish

Configure Package

Please config your settings in config/trans-helper.php file, it should looks like below

<?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')),
    ],
];

Once you confired your settings, you may run install command to setup the tables for the package.

php artisan trans-helper:install

Configure Queue

If you want use the auto translation feature, please also config your queue config file and .env file. If you have enabled the queue feature for default queue, please skipp below instructions. config/queue.php[example]

<?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 section]

QUEUE_CONNECTION=database

After your configuration done, please ensure the your queue is up and running. Simple way is run

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

and make it running all the time in the background.

Mark the Cited Code Feature

You could follow up on the translation later by setup the web interface to fine-tuning the interpretations. However, sometimes it's difficult to recall where the terms are used. You can turn on the feature of cite, and it will help you to record the place you cited the term in your code.

Define Your Own Translator

You can define your own translator by reference the code below, and set it in config.

<?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;
    }
}

Usage

For the following examples

Translation

You can wrap your strings, NO parameters invovled, in helper function localize()

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

And the helper will translate the string into relavent languages accroding to your current locale of laravel user while you have laravel queue function enabled and queue default is running in background.

Sweep

As the process of development the strings in the code changes a lot, you may manually run command

php artisan trans-helper:sweep

or call the sweep action in your code by helper function sweep()

And you also can manually trigger the auto translation without/before running your code by calling command

php artisan trans-helper:trans

or call the translation in your code by calling helper function translate($locales=[]), the inbound parameter is the locale codes you want to translate like ['en','zh-CN','br','de'...], default locales are the config('app.locale'), config('app.fallback_locale'), config('app.faker_locale').

Export

You can use the translation feature without text lang files, you really need them. You can use export command to get them

php artisan trans-helper:export

or call it in your code or tinker enviroment by helper command export($path=null,$locales=null), it will help your to export all locales that has translated in the tables or the locales that your assigned. Noticed: if the locale that you assigned not have any tranlsations yet, it will use the translations of locale config('app.locale').

The export language files will named with the helper function localize() called file namespace.

Hope you enjoy it! Thanks!

License

The MIT License (MIT). Please see License File for more information.

topview-digital/laravel-translation-helper 适用场景与选型建议

topview-digital/laravel-translation-helper 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 03 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 topview-digital/laravel-translation-helper 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 25
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 9
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-03-15