標準翻譯工作流程會根據玩家查看字串的頻率檢測您遊戲中的字串,並將其添加到您的本地化表中以供翻譯。它可能會漏掉不常見的字串或在遊玩過程中生成的字串,例如動態生成的文本或玩家創建的文本。您可以使用翻譯文本 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 |