标准的翻译工作流程根据玩家查看频率检测您体验中的字符串,并将它们添加到本地化表中进行翻译。它可能会错过在游戏期间生成的罕见字符串和/或在游戏期间生成的文本,例如由动态生成的文本或由玩家创建的文本您可以使用翻译文本 API 在实时生成这些字符串的翻译,确保您的体验完全本地化。
将文本翻译为玩家语言
要将文本翻译为玩家的语言,将其 Player.LocaleId 传递为目标语言代码。以下是您在客户端脚本中获取玩家本地 ID 的示例,然后将其传给 Script 中的 ServerScriptService 进行翻译请求。
- 您还必须将 打开云客户端包 包含在体验中;服务器脚本需要它。
客戶端腳本
local ReplicatedStorage = game:GetService("ReplicatedStorage")local httpRequestFunction = ReplicatedStorage:WaitForChild("TranslateTextFunction")-- 要翻译的文本local textToTranslate = "This is the example text to translate"-- 获取播放器的本地local Players = game:GetService("Players")local player = Players.LocalPlayer-- 获取本地玩家的本地ID或设置为任何支持的本地字符串local locale = player.LocaleIdlocal translatedText = httpRequestFunction:InvokeServer(textToTranslate, locale)print("Translated text: ", 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>
-- 创建远程函数
local remoteFunction = Instance.new("RemoteFunction")
remoteFunction.Name = "TranslateTextFunction"
remoteFunction.Parent = ReplicatedStorage
remoteFunction.OnServerInvoke = function(player, text, locale, uni)
print(player.Name .. " requested translation for text: " .. text .. " to locale: " .. 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] -- 假设翻译[locale]包含翻译的文本
else
return "Error: " .. result.Error.message
end
end
测试中
实时翻译 API 目前仅支持 RCC 验证。因此,您必须将代码部署到测试实例才能在 Studio 测试 API。使用团队测试将脚本部署到测试实例并测试您的更改。
翻译API参考
API 请求参数
参数名称 | 型号 | 概述 |
---|---|---|
路径 | 字符串 | 宇宙的路径。必需。 |
字符串 | 字符串 | 需要翻译的文本。必需。 |
源语言代码 | 字符串 | 代表输入文本语言的 IETF BCP-47 语言代码。如果未提供,系统将自动检测源语言。 |
目标语言代码 | Array<string> | 一个列出 IETF BCP-47 格式的目标语言代码的列表,用于翻译。 |
API 响应字段
字段名称 | 型号 | 概述 |
---|---|---|
源语言代码 | 字符串 | IETF BCP-47 语言代码代表源文本的检测或用户指定语言。 |
翻译 | Dictionary<string, string> | 包含需要翻译的地图。关键是 IETF BCP-47 语言代码,值为该语言的翻译文本。地图将包含所有需要的翻译。如果源文本被过滤,这张地图将为空。 |
约束
Roblox 使用以下公式来根据体验中的玩家数量限制此 API 的请求:
max requests per minute per experience = 600 + (1.5 * number_of_concurrent_users)
还有一个结合的限制,每分钟 150 个请求,每个游戏服务器对所有开放云 API。
支持的语种
实时翻译 API 目前支持以下语言,与 自动翻译支持的语言略有不同 。
语言 | 语言代码 |
---|---|
简体中文 | zh-cn |
中文(传统) | zh-tw |
英语 | en-us |
法语 | fr-fr |
德语 | de-de |
印尼语 | id-id |
意大利语 | 它-它 |
日语 | ja-jp |
韩语 | ko-kr |
波兰语 | 卸载-卸载 |
葡萄牙语 | pt-br |
俄语 | ru-ru |
西班牙语 | es-es |
泰语 | th-th |
土耳其语 | tr-tr |
越南语 | vi-vn |