标准翻译工作流程根据玩家查看字符串的频率检测游戏中的字符串,并将它们添加到本地化表中以进行翻译。它可能会遗漏不常见的字符串和/或在游戏过程中生成的字符串,例如动态生成的文本或玩家创建的文本。您可以使用翻译文本 API 实时生成这些字符串的翻译,确保您的游戏完全本地化。
将文本翻译成玩家的语言
要将文本翻译成玩家的语言,请将他们的 Player.LocaleId 作为目标语言代码传递。以下是如何在客户端脚本中获取玩家的区域 ID,然后将其传递给 Script 在 ServerScriptService 中进行翻译请求的示例。
- 您还必须在游戏中包含 Open Cloud 客户端包;服务器脚本需要它。
客户端脚本local ReplicatedStorage = game:GetService("ReplicatedStorage")local httpRequestFunction = ReplicatedStorage:WaitForChild("TranslateTextFunction")-- 要翻译的文本local textToTranslate = "这是要翻译的示例文本"-- 获取玩家的区域local Players = game:GetService("Players")local player = Players.LocalPlayer-- 获取本地玩家区域的区域 ID 或设置为任何支持的区域字符串local locale = player.LocaleIdlocal translatedText = httpRequestFunction:InvokeServer(textToTranslate, locale)print("翻译后的文本: ", translatedText)
ServerScriptService 中的服务器脚本
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local oc = require(ServerScriptService.OpenCloud.V2)
-- 在 https://create.roblox.com/dashboard/creations 中的游戏图块溢出菜单中查找
local universeID = <your_universe_id>
-- 创建 RemoteFunction
local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Name = "TranslateTextFunction"
remoteFunction.Parent = ReplicatedStorage
remoteFunction.OnServerInvoke = function(player, text, locale, uni)
print(player.Name .. " 请求翻译文本: " .. text .. " 到区域: " .. locale)
-- 准备翻译请求
local request : oc.TranslateTextRequest = {
path = oc:UniversePath(universeID),
text = text,
-- 目标语言代码支持多个区域的列表进行翻译。
-- 这里我们仅传递一种语言:
-- 本地脚本中检索到的玩家区域
target_language_codes = {locale}
}
local result = oc:TranslateText(request)
if result.Error == nil then
return result.Response.translations[locale] -- 假设 translations[locale] 包含翻译后的文本
else
return "错误: " .. result.Error.message
end
end
测试
实时翻译 API 目前仅支持 RCC 身份验证。因此,您必须将代码部署到测试实例中,以便从 Studio 测试 API。使用 协作测试 将脚本部署到测试实例并测试您的更改。
翻译 API 参考
API 请求参数
| 参数名称 | 类型 | 描述 |
|---|---|---|
| path | string | 宇宙的路径。必填。 |
| text | string | 要翻译的文本。必填。 |
| source_language_code | string | 表示输入文本语言的 IETF BCP-47 语言代码。如果未提供,系统将自动检测源语言。 |
| target_language_codes | Array<string> | 用于翻译的 IETF BCP-47 格式的目标语言代码列表。 |
API 响应字段
| 字段名称 | 类型 | 描述 |
|---|---|---|
| source_language_code | string | 表示已检测到或用户指定的源文本语言的 IETF BCP-47 语言代码。 |
| translations | Dictionary<string, string> | 包含请求的翻译的映射。键是 IETF BCP-47 语言代码,值是该语言的翻译文本。该映射将包含所有请求的翻译。如果源文本被过滤,该映射将为空。 |
限制
Roblox 使用以下公式根据游戏中的玩家数量对该 API 的请求进行限流:
每个游戏每分钟最大请求数 = 600 + (1.5 * 同时在线用户数量)
此外,对于所有 Open Cloud API,每个游戏服务器每分钟的请求总数限制为 150。
支持的语言
实时翻译 API 目前支持以下语言,这与 自动翻译的支持语言 略有不同。
| 语言 | 语言代码 |
|---|---|
| 中文(简体) | zh-cn |
| 中文(繁体) | zh-tw |
| 英语 | en-us |
| 法语 | fr-fr |
| 德语 | de-de |
| 印尼语 | id-id |
| 意大利语 | it-it |
| 日语 | ja-jp |
| 韩语 | ko-kr |
| 波兰语 | pl-pl |
| 葡萄牙语 | pt-br |
| 俄语 | ru-ru |
| 西班牙语 | es-es |
| 泰语 | th-th |
| 土耳其语 | tr-tr |
| 越南语 | vi-vn |