weglot/weglot-php

Weglot API 的 PHP 库

此软件包的官方仓库似乎已消失,因此软件包已被冻结。


README

68747470733a2f2f63646e2e7765676c6f742e636f6d2f6c6f676f2f6c6f676f2d686f722e706e67

PHP 库

WeglotSlack Latest Stable Version BuildStatus Code Climate License

概述

此库允许您通过 PHP 快速轻松地使用 Weglot API。它处理与 Weglot API 的所有通信,并为您提供完全功能的解析器,以便轻松处理 HTML 页面。

要求

  • PHP 版本 5.4 及以上
  • Weglot API 密钥,从免费级别开始

安装

您可以通过 Composer 安装库。运行以下命令

composer require weglot/weglot-php

要使用库,请使用 Composer 的 自动加载

require_once __DIR__. '/vendor/autoload.php';

入门

简单使用 Parser

// Url to parse
$url = 'https://foo.bar/baz';

// Config with $_SERVER variables
$config = new ServerConfigProvider();

// Fetching url content
$content = '...';

// Client
$client = new Client(getenv('WG_API_KEY'));
$parser = new Parser($client, $config);

// Run the Parser
$translatedContent = $parser->translate($content, 'en', 'de');

有关更多详细信息,请查看相应的示例或文档

示例

有关更多使用示例,例如:其他端点、缓存、解析。

您可以在示例文件夹中查看。您将找到一个简短的 README 文件,其中包含有关每个示例的详细信息。

参考

客户端

客户端是与 Weglot API 通信的所有类。

客户端 & 个人资料

Weglot\Client\Client 是此库的主要类。基本上它管理请求,仅此而已。

使用此类,我们有 Weglot\Client\Profile,它表示基于 API 密钥长度的特定属性。今天我们有两种类型的 API 密钥

  • 35 位 API 密钥:没有启用自定义功能的常规 API 密钥。
  • 36 位 API 密钥:添加 ignoredNodes 行为,基本上我们跳过某些标签作为句子进行解析(例如 strongem 等),以生成更长的句子。

API

包括与 API 通信的所有对象(作为输入或输出)。

  • Weglot\Client\Api\WordEntry:在 API 对象中定义单个句子
  • Weglot\Client\Api\WordCollection:定义多个 WordEntry
  • Weglot\Client\Api\LanguageEntry:在 API 对象中定义单个语言
  • Weglot\Client\Api\LanguageCollection:定义多个 LanguageEntry
  • Weglot\Client\Api\TranslateEntry:定义一个翻译接口,用于作为/translate端点的输入/输出

以下是一些快速示例,说明如何使用其中的一些功能

// creating some WordEntry objects
$firstWord = new WordEntry('This is a blue car', WordType::TEXT);
$secondWord = new WordEntry('This is a black car', WordType::TEXT);

// then create our TranslateEntry object to use later with /translate
$translateEntry = new TranslateEntry([
    'language_from' => 'en',
    'language_to' => 'de',
    'title' => 'Weglot | Translate your website - Multilingual for WordPress, Shopify, ...',
    'request_url' => 'https://weglot.com/',
    'bot' => BotType::HUMAN
]);
$translateEntry->getInputWords()->addMany([$firstWord, $secondWord]);

就像你刚刚看到的,你可以找到一些快速枚举来设置与API相关的数据,例如这些

  • Weglot\Client\Api\Enum\WordType:用于提供关于我们希望翻译的文本来源的上下文。
  • Weglot\Client\Api\Enum\BotType:用于定义请求的来源。

缓存

我们使用一个主要接口管理缓存:Weglot\Client\Caching\CacheInterface,它内部使用PSR-6 RFC

我们在Weglot\Client\Caching\Cache类中添加了一个此接口的实现,它使用PSR-6 CacheItemPoolInterface作为存储。您可以像以下这样从客户端方法中设置此选项

$client = new Client('wg_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$client->setCacheItemPool($myCacheItemPool);

给定的$myCacheItemPool可以是任何PSR-6兼容的对象。我建议您查阅php-cache缓存池实现,它应包含您需要插入缓存库的库。

端点

以下是用于与API端点通信的类。今天我们共有3个

  • Weglot\Client\Endpoint\Translate:用于对/translate端点进行请求
  • Weglot\Client\Endpoint\Status:用于对/status端点进行请求
  • Weglot\Client\Endpoint\LanguagesList:用于从“假”API获取所有与语言相关的数据

根据我们的API对象示例,以下是如何使用Translate类的方法

$client = new Client('wg_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
$translateEntry = new TranslateEntry(...); // Check API part for more details about this one

$translate = new Translate($translateEntry, $client);
$translated = $translate->handle();

根据此示例,$translated对象将包含一个包含从API返回的所有数据的TranslateEntry对象。您可以在这里找到完整的工作示例

工厂

这些是用于端点类和返回的API对象之间的类。它处理从API返回的JSON到良好格式化的API对象的全部转换。

这里没有示例,因为它只用于内部使用。

HttpClient

另一组内部类。

这些由一个简单接口组成,用于管理请求:Weglot\Client\HttpClient,以及一个使用cURL作为HTTP提供者的实现:Weglot\Client\CurlClient(此类实际上深受Stripe的CurlClient的启发)

解析器

解析器是我们开发者工具包的重要组成部分。

它用于将句子与DOM中的要翻译的句子匹配,并创建干净的API对象,通过客户端发送。

目前没有关于解析器的文档,因为我们计划在下个月对它进行重大重写,重写后我们将确保有文档。

Util

这里有一些简短的类,用于管理一些简单的实用程序,例如

Weglot\Util\JsonLd

管理与在JsonLd结构中恢复和存储数据相关的所有操作

Weglot\Util\Server

所有 $_SERVER 相关的实用程序

$fullUrl = Server::fullUrl($_SERVER);
// will return the current url seen by $_SERVER, for example: https://weglot.com/es/pricing

Weglot\Util\Site

用于从远程网站获取内容

Weglot\Util\Text

所有经典的文本实用程序,如contains()

Weglot\Util\Url

我们有一个名为 Weglot\Util\Url 的类,它是我们库中 URL 管理的基石之一。通过获取当前 URL 和 Weglot 配置,它可以实现以下功能:

  • 确定当前 URL 上的语言
  • 生成其他语言的 URL
  • 确定当前 URL 是否可翻译(基于 excludedUrls 列表)
  • 获取基于当前 URL 的所有翻译 URL
  • 基于所有翻译 URL 生成 hreflang 标签

以下是一些快速示例

$url = new Url('https://weglot.com/es/pricing', 'en', ['fr', 'es', 'de']);

$currentLang = $url->detectCurrentLanguage();
// $currentLang will contain 'es'

$frUrl = $url->getForLanguage('fr');
// $frUrl will contain 'https://weglot.com/fr/pricing'

$translable = $url->isTranslable();
// $translable will contain true since we have no excluded urls

$url->setExcludedUrls(['/pricing']);
$translable = $url->isTranslable();
// $translable will contain false since we added `/pricing` to the excluded urls

$urls = $url->currentRequestAllUrls();
/**
 * $urls will contain following array:
 * Array(
 *   'en' => 'https://weglot.com/pricing',
 *   'fr' => 'https://weglot.com/fr/pricing',
 *   'es' => 'https://weglot.com/es/pricing',
 *   'de' => 'https://weglot.com/de/pricing'
 * );
 */

$hreflang = $url->generateHrefLangsTags()
/**
 * $hreflang will contain following string:
 * <link rel="alternate" href="https://weglot.com/pricing" hreflang="en"/>
 * <link rel="alternate" href="https://weglot.com/fr/pricing" hreflang="fr"/>
 * <link rel="alternate" href="https://weglot.com/es/pricing" hreflang="es"/>
 * <link rel="alternate" href="https://weglot.com/de/pricing" hreflang="de"/>
 */

## 仅测试解析器 ./vendor/codeception/codeception/codecept run -g parser

完整测试 ./vendor/codeception/codeception/codecept run

关于

weglot-php 由 Weglot 开发团队指导和支持。

weglot-php 由 Weglot SAS 维护和资助。 weglot-php 的名称和标志是 Weglot SAS 的商标。

许可证

MIT 许可证 (MIT)