標準翻譯工作流程會根據玩家查看的頻率偵測您體驗中的字串,並將它們添加到本地化表中以供翻譯。它可能會錯過在遊戲期間生成的罕見字串和/或在遊戲期間創建的文字,例如動態生成的文字或玩家創建的文字。您可以使用翻譯文字 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 |
波蘭語 | pl-pl |
葡萄牙語 | pt-br |
俄語 | ru-ru |
西班牙語 | es-es |
泰語 | zh-tw |
土耳其語 | tr-tr |
越南语 | vi-vn |