marketforce-info/azure-translator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

marketforce-info/azure-translator

Composer 安装命令:

composer require marketforce-info/azure-translator

包简介

Azure Translator taking ICU message format into account

README 文档

README

Code Checks Latest Stable Version Total Downloads Licence

Sends batches of messages to be translated to the Azure translate service. Has the option of handling variables as a formatted message.

Installation

$ composer require marketforce-info/azure-translator

Requirements

  • Access to Azure authentication details. Supports subscription key and authorization token mechanisms.
  • Depends on a concrete implementation of psr/http-client

Usage

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory as GuzzleFactory;
use MarketforceInfo\AzureTranslator\Translator;

$factory = new GuzzleFactory();
$client = new Translator\Client(
    new GuzzleClient(),
    new Translator\RequestFactory($factory, $factory, [Translator\Language::french])
);

$translator = new Translator($client);
$translator->onTranslate(static function (Translator\Translation $translation) {
        // do something with the message
    })
    ->begin(static function (Translator\Delegate $translator) use ($messages) {
        foreach ($messages as $message) {
            $translator->translate($message);
        }
    });

More in depth example of using the Builder class.

use \MarketforceInfo\AzureTranslator\Builder;
use \MarketforceInfo\AzureTranslator\MessageFormatter\BasicFormatter;
use \MarketforceInfo\AzureTranslator\Translator;

$factory = new GuzzleFactory();
$translator = (new Builder())
    ->withBaseUrl(Translator\RequestFactory::BASE_URL_US)
    ->withHttp(new GuzzleClient(), $factory, $factory)
    ->withLanguages([Translator\Language::arabic], Translator\Language::french)
    ->withBearerToken('<bearer-token>')
    ->withMessageFormatter(new BasicFormatter('[', ']'))
    ->withTraceIdCallback(fn () => 'xxxx-XXXX-xxxx-XXXX-xxxx')
    ->when(
        $_ENV['delete_profanity'] === true,
        fn (Builder $builder) => $builder->withProfanityDeleted(),
        fn (Builder $builder) => $builder->withProfanityMarked(static fn (string $word) => '*censored*')
    )
    ->create();

Translated Message

The onTranslate callback method receives a DTO translation of an individual translated message for a language. It contains four properties:

$message is a string of the translated message.

$language is a Language enum of the language the message has been translated into.

$traceId is a trace ID used to track requests (see below for more information).

$state is user specified data specific to the original untranslated message.

The state is something that can be optionally set at the point of request (translate).

foreach ($messages as $messageId => $message) {
    $translator->translate($message, ['id' => $messageId]);
}

Then in the onTranslate callback.

static function (Translation $translation) use ($db) {
    $db->replace(
        table: 'translations',
        where: [
            'id' => $translation->state['id'],
            'language' => $translation->language->value
        ],
        data: ['message' => $translation->message]
    );
};

Translate Request

Batching of the requested messages is handled automatically. The ability to pass messages to be translated is done via the Delegate class. The begin method can not be called until the onTranslate behaviour has been defined.

$translator->begin(static function (Delegate $translator) use ($untranslatedMessages) {
    foreach ($untranslatedMessages as $message) {
        $translator->translate($message);
    }
});

Features

HTTP Client

This library relies on three PSR interface implementations to be provided. ClientInterface for the HTTP client, RequestFactoryInterface to create a request and StreamFactoryInterface to create the request body. For example, in the case of the Guzzle implementation, the request and stream factory are the same implementation.

$builder->withHttp($client, $requestFactory, $streamFactory);

Providers

Authentication

There are two methods of specifying an authentication token.

$builder->withSubscriptionKey('<subscription-key>');
// or
$builder->withBearerToken('<bearer-token>');

Anything more complicated should be handled through plugins/middleware in the HTTP Client.

Message Format

By default, there is no message formatting. The following outlines alternatives to allow for different translation behaviours.

Basic

Allows for basic syntax substitution in messages. For example

Welcome {name} to the monthly newsletter from {company}

The formatter will substitute the variables, so they won't be translated and then replace them in the final translated message.

use \MarketforceInfo\AzureTranslator\MessageFormatter\BasicFormatter;
$builder->withMessageFormatter(new BasicFormatter());

Basic formatter defaults to {} style syntax but this can be changed in the constructor.

ICU Message Format

Requires an additional component which parses the ICU message format so that a representation can be sent to the translation service.

Installation

$ composer require marketforce-info/message-format-parser
Usage
use \MarketforceInfo\AzureTranslator\MessageFormatter\IcuFormatter;
$builder->withMessageFormatter(new IcuFormatter());
Caveat

The process of creating a representation to send to the translation service produces a verbose output which could have a detrimental effect on the character count and subsequently the billing by the Azure service. Additionally, the ICU parsing attempts to achieve the best translation outcome by composing variations of the messages when the format of a ICU message uses select, selectordinal or plural.

Custom Message Format

It's possible to create a custom message format class. The withMessageFormatter method accepts any implementation of the MessageFormatter interface.

Tracing Requests

Each individual request to the Azure service includes a Client Trace ID. By default, this is handled automatically. This can be overridden with the following method.

User defined function

$builder->withTraceIdCallback(fn () => 'xxxx-XXXX-xxxx-XXXX-xxxx');
// or
$builder->withTraceIdCallback([$this, 'traceFunction']);
// or
$builder->withTraceIdCallback(fn (\Closure $generatorFn) => $generatorFn());

Generator function

As shown, the call back is passed a parameter which allows the use of the internal generator function. This is useful in scenarios where a trace ID is recorded for every request made.

Handling Profanity

Azure has three options for handling profanity. None, deleted or marked with asterisks or tags. The component allows this to be specified. By default, no profanity handling will be enabled. It allows for a callback to customise the way profane phrases are displayed.

Methods available

$builder->withoutProfanityHandling(); // default
$builder->withProfanityDeleted();
$builder->withProfanityMarked(); // words replaced with *
$builder->withProfanityMarked(static function (string $phrase) {
    return '<span class="profanity">' . str_pad('', mb_strlen($phrase), 'x') . '</span>';
});

Getting Metric Data

On each request to the translate service, information about the number of characters processed is returned in the process. This can be retrieved on completion.

$translator->getMeteredUsage();

This returns an array of each request made and the data returned.

{
    "1234-1234-1234-1234": [
        "182"
    ]
}

Contributions

Contributions gratefully accepted in the form issues or PRs.

Security

If you discover any security related issues, please email appsupport_uk@marketforce.com instead of using the issue tracker.

License

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

marketforce-info/azure-translator 适用场景与选型建议

marketforce-info/azure-translator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 10 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「translator」 「azure」 「icu」 「messageformat」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 marketforce-info/azure-translator 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-10-15