mautic/transifex
Composer 安装命令:
composer require mautic/transifex
包简介
The Transifex API Package is a wrapper of the Transifex API available for PHP developers
关键字:
README 文档
README
This is fork of archived repository by Michael Babker. Mautic use this library and so we decided to maintain it.
Transifex API Package

The Transifex API Package is a PHP client for accessing the Transifex API.
Requirements
- PHP 7.4 or later
- Any PSR-17 compatible factories
- Any PSR-18 compatible HTTP client
Installation
Installation via GIT
Get the source code from GIT:
git clone git://github.com/mautic/Transifex-API.git
Installation via Composer
To include this package in your Composer project, run the following command from the command line:
composer require mautic/transifex
Documentation
The Transifex API package provides a PHP interface for interacting with the Transifex API.
Basic Use
The primary interface for interacting with the Transifex package is the Transifex class. This class serves as the API factory and allows developers to manage the options used by the API objects and HTTP connector as well as retrieve instances of the API objects. To create a Transifex object, you only need to instantiate it with the appropriate dependencies.
use Mautic\Transifex\ApiFactory; use Mautic\Transifex\Config; use Mautic\Transifex\Transifex; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\UriFactoryInterface; $client = new ClientInterface(); // or any PSR-18 HTTP client $requestFactory = new RequestFactoryInterface(); // or any PSR-17 Request factory $streamFactory = new StreamFactoryInterface(); // or any PSR-17 Stream factory $uriFactory = new UriFactoryInterface(); // or any PSR-17 URI factory $config = new Config(); $config->setApiToken('some-api-token'); $config->setOrganization('some-organization'); $config->setProject('some-project'); $transifex = new Transifex($client, $requestFactory, $streamFactory, $uriFactory, $config);
Follow the Transifex documentation on how to get the API Token.
The Config object can be also created from environmental variables:
use Mautic\Transifex\Config; putenv('TRANSIFEX_API_TOKEN=some-api-token'); putenv('TRANSIFEX_ORGANIZATION=some-organization'); putenv('TRANSIFEX_PROJECT=some-project'); $config = Config::fromEnv();
To retrieve an instance of an API object, you would use the get() method. API objects are named based on the documented sections of the Transifex API. To retrieve an object that can interface with the "resources" API section, you would use the following code:
use \Mautic\Transifex\Connector\Resources; $resources = $transifex->getConnector(Resources::class); \assert($resources instanceof Resources);
API Responses
This package returns a PSR-7 compatible response created by the underlying PSR-18 HTTP client.
This package is not catching Exceptions thrown by the HTTP client so users implementing this package should implement appropriate error handling mechanisms.
An exception of Mautic\Transifex\Exception\ResponseException will be thrown when the response code is > 400. The error message contains the URI, request and response. This simplifies debugging. The Request and Response object can be retreived from this exception.
Resources
Resources are the basic files that should be translated.
Get the Resource connector for the bellow examples like so:
use Mautic\Transifex\Connector\Resources; $resources = $transifex->getConnector(Resources::class); \assert($resources instanceof Resources);
Get all resources
Docs: https://developers.transifex.com/reference/get_resources
$response = $resources->getAll(); $body = json_decode($response->getBody()->__toString(), true);
Create a resource
This is a syncronous operation so you get the response rightaway. You need another request to upload a content to this resource though. See the docs for example responses.
For the list of available i18n formats (the third param) trigger this endpoint.
$response = $resources->create('Resource A', 'resource-a', 'INI'); $body = json_decode($response->getBody()->__toString(), true);
Upload resource content
This is an asynchronous operation so the first request will just add the task to the queue and returns a unique ID See the docs. Then we have to poll another endpoint for the status of this task.
use Mautic\Transifex\Promise; use Mautic\Transifex\Exception\ResponseException; use Psr\Http\Message\ResponseInterface; $response = $resources->uploadContent('resource-a', "something=\"Something\"\nsomething.else=\"Something Else\"\n"); $promise = $transifex->getApiConnector()->createPromise($response); $promises = new \SplQueue(); $promise->setFilePath('/some/file.ini'); // In the real world it is useful to map your file to this promise. Useful for later processing. $promises->enqueue($promise); // In the real world, there would be multiple promises. usleep(500000); // Give Transifex a 1/2 second so we make 1 request instead of 2. $transifex->getApiConnector()->fulfillPromises( $promises, function (ResponseInterface $response, Promise $promise) { echo "Resource for {$promise->getFilePath()} was uploaded successfully"; }, function (ResponseException $exception, Promise $promise) { echo "Resource upload for {$promise->getFilePath()} failed with {$exception->getMessage()}"; } );
The polling for promise status happens automatically every 0.5 seconds.
As fetching for the promise status happens asynchronously and the order of processed promises is not cretain there are callbacks for your implementation to react on successful and failed statuses. Another perk is that further processing is not blocked until all promisses are resolved.
Deleting a resource
Docs: https://developers.transifex.com/reference/delete_resources-resource-id
$response = $resources->delete('resource-a');
Translations
Translations translate the resource content to another languages.
Get the Translations connector for the bellow examples like so:
use Mautic\Transifex\Connector\Translations; $translations = $transifex->getConnector(Translations::class); \assert($translations instanceof Translations);
Upload a translation
Uploading a translation is also asynchronous. The worflow is similar to downloading translations or resources.
$response = $translations->upload(self::RESOURCE_SLUG, 'cs', "something=\"Něco\"\n"); $promise = $transifex->getApiConnector()->createPromise($response); $promises = new SplQueue(); $promise->setFilePath('/some/file.ini'); // In the real world it is useful to map your file to this promise. Useful for later processing. $promises->enqueue($promise); // In the real world, there would be multiple promises. usleep(500000); // Give Transifex a 1/2 second so we make 1 request instead of 2. $transifex->getApiConnector()->fulfillPromises( $promises, function (ResponseInterface $response, Promise $promise) { echo "Translation for {$promise->getFilePath()} was uploaded successfully"; }, function (ResponseException $exception, Promise $promise) { echo "Translation upload for {$promise->getFilePath()} failed with {$exception->getMessage()}"; } );
Download a translation
This is the endpoint you need to get the work of the translators back to your app. Also an async operation. Check the docs. It uses the same pattern as the others:
$response = $translations->download(self::RESOURCE_SLUG, 'cs'); $promise = $transifex->getApiConnector()->createPromise($response); $promises = new SplQueue(); $promise->setFilePath('/some/file.ini'); // In the real world it is useful to map your file to this promise. Useful for later processing. $promises->enqueue($promise); // In the real world, there would be multiple promises. usleep(500000); // Give Transifex a 1/2 second so we make 1 request instead of 2. // Assert that the translation content was downloaded successfully. $transifex->getApiConnector()->fulfillPromises( $promises, function (ResponseInterface $response) use (&$successCounter, &$translationContent) { $translationContent = $response->getBody()->__toString(); echo "Translation for {$promise->getFilePath()} was downloaded successfully. Here's the content:\n{$translationContent}"; }, function (ResponseException $exception) { echo "Translation download for {$promise->getFilePath()} failed with {$exception->getMessage()}"; } );
Tests
There is one functional test that can run all the covered API requests against an existing Transifex project. It needs a configuration to connect to that project. Copy the phpunit.xml.dist file and rename it to phpunit.xml. This creates an override of the default PHPUNIT configuration. Fill in the organization, project and API token values. The test project must have the cs translation available.
To run the live test execute composer test -- --testsuite=Functional
To run the unit tests execute composer test -- --testsuite=Unit
Run the whole test suite: composer test
Run static analysis checks: composer phpstan
Run code style checks: composer cs
Fix code style issues: composer fixcs
mautic/transifex 适用场景与选型建议
mautic/transifex 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 768.62k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「transifex」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mautic/transifex 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mautic/transifex 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mautic/transifex 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Alfabank REST API integration
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
The Transifex API Package is a wrapper of the Transifex API available for PHP developers
A lightweight plain-PHP framework for database-backed CRUD APIs.
bughq error tracking - PHP SDK
统计信息
- 总下载量: 768.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 11
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: GPL-2.0
- 更新时间: 2020-01-07