承接 joeybab3/phphtmltranslator 相关项目开发

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

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

joeybab3/phphtmltranslator

最新稳定版本:v1.1.1

Composer 安装命令:

composer require joeybab3/phphtmltranslator

包简介

PHP HTML Translator

README 文档

README

PHP HTML Translator

Translates HTML into a destination language using Stichoza's Google Translate PHP: https://github.com/Stichoza/google-translate-php

It has all the same limitations, namely that since it uses the free Google Translate API, you may run into rate limits or a temporary IP Ban.

It uses a SQL database to cache previously translated tokens so running html through it repeatedly with only minor changes will be much faster.

Setup

composer require joeybab3/phphtmltranslator

Once installed run the following PHP somewhere:

<?php
require_once('vendor/autoload.php');
use Joeybab3\HTMLTranslator\HTMLTranslator as Translator;

$dsn = "mysql:host=127.0.0.1;dbname=database_name;charset=$charset";
$username = "";
$password = "";
try {
    $db = new PDO($dsn, $username, $password);
} catch (\PDOException $e) {
    throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

Translator::createTranslationCacheTable($db);

This will create the cache table. Alternatively, create the table yourself:

CREATE TABLE `translations` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `text` text,
  `result` text,
  `lang` varchar(4) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

Then you can use it to translate HTML:

<?php
require_once('vendor/autoload.php');
use Joeybab3\HTMLTranslator\HTMLTranslator as Translator;

$TR = new Translator($db, "es"); // translate to Spanish
$html = "<div class='test'><span>This text is not bold but <b>this text is bold</b></span><span>This text, on the other hand, will be a separate translation entirely.</span>";

$result = $TR->tokenizedTranslate($html);

// <div class='test'><span>Este texto no está en negrita pero <b>este texto está en negrita</b></span><span>Este texto, por otro lado, será una traducción completamente separada. </span>

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固