คุณสามารถใช้ API การแปลสำหรับงานแปลที่เฉพาะเจาะจงซึ่งไม่ได้ถูกจัดการโดยอัตโนมัติจากการ เพิ่มการแปล ลงในตารางการแปล Roblox มี LocalizationService เพื่อจัดการกับความต้องการการเขียนสคริปต์การแปลทั้งหมด ใช้ LocalizationService สำหรับงานต่อไปนี้:
หากคุณใช้ API การแปลใด ๆ เมื่อแปลเกมของคุณ ให้ฟังการเปลี่ยนแปลงของ LocaleID ของผู้ใช้เพื่อ ตอบสนองต่อการเปลี่ยนภาษาของผู้ใช้ ขณะอยู่ในเกม
เมื่อคุณต้องการใช้รหัสการแปลซ้ำ คุณควรใช้ TranslationHelper ModuleScript เพื่อจัดการกับข้อผิดพลาดและการแปลที่หายไป
การแปลภาพและเสียง
เพิ่มการแปลนอกเหนือจากข้อความในเกมของคุณโดยการจัดเตรียมภาพและเสียงที่ไม่ซ้ำกันตามภูมิภาคของผู้ใช้ เพื่อแปลสินทรัพย์ ให้อัปโหลด source และ target asset IDs ไปยัง tabel การแปลของเกมของคุณ จากนั้นใช้ API การแปลเพื่อดึงสินทรัพย์ที่แตกต่างกัน



ในการเริ่มการแปลภาพและเสียง ให้อัปโหลด source และ target asset IDs ของคุณไปยังตารางการแปล รายการ Asset ID ในตารางการแปลต้องรวม Key เป็นตัวระบุเพื่อให้ API เรียกใช้งาน
ตัวอย่างต่อไปนี้เป็นรายการในตารางการแปลโดยใช้ Asset IDs:
| Key | Source | es | pt |
|---|---|---|---|
| Key_JewelsImage | 2957093606 | 2957093671 | 2957093727 |
โค้ดต่อไปนี้จะเปลี่ยน Asset ID ของ ImageLabel ด้วย Asset ID สเปนที่ให้มาในตารางการแปล:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LocalizationService = game:GetService("LocalizationService")
-- ตัวแปรท้องถิ่น
local localizedImageID
local localizedImage = Instance.new("ImageLabel")
-- โหลด Translator สำหรับ "es". หุ้มหรือฟังก์ชันภายใน pcall() เพื่อป้องกันความผิดพลาด
local res, translator = pcall(function()
return LocalizationService:GetTranslatorForLocaleAsync("es")
end)
if res then
-- ดึง Asset ID จากตารางการแปลโดยการอ้างอิง Key
localizedImageID = translator:FormatByKey("Key_JewelsImage")
-- ตั้งค่าภาพ
localizedImage.Image = "rbxassetid://" .. localizedImageID
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. translator)
end
การแปลข้อความแต่ละข้อความ
ในบางกรณี คุณอาจต้องการมุ่งเป้าไปที่ข้อความแต่ละข้อความสำหรับการแปล Translator:Translate() สามารถดึงรายการแต่ละรายการในตารางการแปลตามข้อความต้นทางได้
ในตัวอย่างถัดไป รายการการแปลต่อไปนี้ถูกใช้งาน:
| Source | es | es | pt |
|---|---|---|---|
| Screen | Pantalla | 295093671 | 2957093727 |
สคริปต์ต่อไปนี้จะพิมพ์การแปลภาษาสเปนของ "Screen" ไปยังหน้าต่าง Output:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LocalizationService = game:GetService("LocalizationService")
-- โหลด Translator สำหรับ "es". หุ้มหรือฟังก์ชันภายใน pcall() เพื่อป้องกันความผิดพลาด
local res, translator = pcall(function()
return LocalizationService:GetTranslatorForLocaleAsync("es")
end)
if res then
-- ใช้ฟังก์ชัน Translate โดยให้บริบทของวัตถุและข้อความ
local sourceTranslation = translator:Translate(game, "Screen")
print(sourceTranslation) -- คาดหวัง Output: "Pantalla"
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. translator)
end
ใช้การแทนบริบท
มีบางกรณีที่ข้อความเดียวกันอาจมีความหมายหลายความหมาย ตัวอย่างเช่น คำว่า "Screen" สามารถบ่งชี้ทั้งหน้าจอคอมพิวเตอร์และหน้าต่างแต่คำแปลภาษาสเปนจะแตกต่างกันโดยสิ้นเชิง
คอลัมน์ Context ของตารางการแปลใช้สำหรับระบุการแปลผ่านการแทนบริบท ระบุวัตถุในเกมของคุณในตารางการแปลของคุณตามตัวอย่างต่อไปนี้:
| Context | Source | es |
|---|---|---|
| workspace.WindowScreen.SurfaceGui.TextLabel | Screen | Mosquitero |
| Screen | Pantalla |
สคริปต์ต่อไปนี้ใช้การแทนบริบทเพื่อให้การแปลเฉพาะเรื่องมีความสำคัญมากขึ้น:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LocalizationService = game:GetService("LocalizationService")
local Workspace = game:GetService("Workspace")
-- โหลด Translator สำหรับ "es". หุ้มหรือฟังก์ชันภายใน pcall() เพื่อป้องกันความผิดพลาด
local res, translator = pcall(function()
return LocalizationService:GetTranslatorForLocaleAsync("es")
end)
if res then
-- ใช้ฟังก์ชัน Translate โดยให้บริบทของวัตถุและข้อความ
local sourceTranslation = translator:Translate(Workspace.WindowScreen.SurfaceGui.TextLabel, "Screen")
print(sourceTranslation) -- คาดหวัง Output: Mosquitero
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. translator)
end
หลายบริบท
ในกรณีที่มีหลายบริบท บริการแปลจะเปรียบเทียบความสัมพันธ์ของวัตถุในฟิลด์ Context จาก ขวาไปซ้าย โดยใช้การจับคู่ที่ใกล้ที่สุด
ตัวอย่างเช่น ตารางการแปลในเกมของคุณอาจมีรายการ Source strings ร่วมกันต่อไปนี้:
| Context | Source | es |
|---|---|---|
| workspace.WindowScreen.SurfaceGui.TextLabel | Screen | Mosquitero |
| playerGui.ScreenGui.TextButton | Screen | Pantalla |
หากข้อความ "Screen" ถูกเพิ่มไปที่วัตถุ playerGui.ScreenGui.TextLabel ในเกมของคุณ บริการแปลจะแสดง "Mosquitero" เป็นการแปลภาษาสเปนเนื่องจากเป็นการจับคู่บริบทที่ใกล้ที่สุด
แทนที่พารามิเตอร์
เมื่อใช้พารามิเตอร์เพื่อแปล เนื้อหาที่มีพลศาสตร์ ให้ตั้งค่าเป็น ตาราง และส่งตารางเป็นอาร์กิวเมนต์ผ่าน API
ในตัวอย่างนี้ เกมมีตารางการแปลที่มีรายการต่อไปนี้:
| Key | Source | es |
|---|---|---|
| Key_Prize_1 | {1:int} jewels | {1:int} joyas |
| Key_Prize_2 | ${AmountCash:fixed} cash and {NumJewels:int} jewels | ${AmountCash:fixed} dinero y {NumJewels:int} joyas |
ใช้ตัวอย่างโค้ดต่อไปนี้เพื่อแปลข้อความเหล่านี้ด้วยค่าพารามิเตอร์:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LocalizationService = game:GetService("LocalizationService")
-- โหลด Translator สำหรับ "es". หุ้มหรือฟังก์ชันภายใน pcall() เพื่อป้องกันความผิดพลาด
local res, translator = pcall(function()
return LocalizationService:GetTranslatorForLocaleAsync("es")
end)
if res then
-- ตั้งค่าค่าพารามิเตอร์ใน "Key_Prize_1" เป็น 100
local keyTranslation1 = translator:FormatByKey("Key_Prize_1", {100})
print(keyTranslation1) -- คาดหวัง Output: 100 joyas
-- ตั้งค่าหลายพารามิเตอร์เป็น 500 และ 100 ตามชื่อ
local keyTranslation2 = translator:FormatByKey("Key_Prize_2", {AmountCash=500, NumJewels=100})
print(keyTranslation2) -- คาดหวัง Output: $500.00 dinero y 100 joyas
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. translator)
end
สลับภาษา
ในบางกรณี คุณอาจต้องการแสดงการแปลจากภาษาอื่นในเกมของคุณ คุณสามารถตั้งค่าผู้แปลใหม่ที่มีรหัสประเทศที่แตกต่างกันโดยใช้ LocalizationService:GetTranslatorForLocaleAsync()
ตัวอย่างโค้ดต่อไปนี้ตั้งค่าผู้แปลหนึ่งคนด้วยรหัสประเทศแบบแมนนวลและผู้แปลเพิ่มเติมตามการตั้งค่าภูมิภาคทั่วโลกของผู้ใช้:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")
-- ตัวแปรท้องถิ่น
local player = Players.LocalPlayer
-- โหลด Translator สำหรับ "pt". หุ้มหรือฟังก์ชันภายใน pcall() เพื่อป้องกันความผิดพลาด
local res1, translator = pcall(function()
return LocalizationService:GetTranslatorForLocaleAsync("pt")
end)
-- โหลดผู้แปลที่สองด้วยภาษาของผู้เล่น ในตัวอย่างนี้ "es"
local res2, fallbackTranslator = pcall(function()
return LocalizationService:GetTranslatorForPlayerAsync(player)
end)
-- ใช้ฟังก์ชัน Translate กับผู้แปลแรก
if res1 then
local translate1 = translator:Translate(game, "jewels")
print(translate1) -- คาดหวัง Output ใน pt: joyas
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. translator)
end
-- ใช้ฟังก์ชัน Translate กับผู้แปลที่สอง
if res2 then
local translate2 = fallbackTranslator:Translate(game, "jewels")
print(translate2) -- คาดหวัง Output ถ้าผู้ใช้ตั้งไว้ที่ 'es': jóias
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. fallbackTranslator)
end
ตอบสนองต่อการเปลี่ยนภาษาของผู้ใช้
ผู้ใช้สามารถเปลี่ยนการตั้งค่าภาษาของตนได้ตลอดเวลาผ่านเมนูการตั้งค่าในเกม การเปลี่ยนการตั้งค่าเหล่านี้จะทำการอัปเดตสินทรัพย์การแปลแบบไม่เขียนสคริปต์โดยอัตโนมัติ เช่น ข้อความที่จัดการโดย การแปลอัตโนมัติ แต่การเปลี่ยนแปลงการแปลที่เขียนไว้ด้วยสคริปต์ซึ่งได้เรนเดอร์ไปแล้ว เช่น ภาพ GUI หรือเสียง อาจไม่ได้รับการอัปเดต


เพื่อให้แน่ใจว่าสินทรัพย์การแปลที่เขียนสคริปต์ของคุณอัปเดตอย่างถูกต้อง ให้ฟังเหตุการณ์ GetPropertyChangedSignal สำหรับการเปลี่ยนแปลงในคุณสมบัติ LocaleID ของอินสแตนซ์ Translator ที่ส่งคืนโดย LocalizationService.GetTranslatorForPlayerAsync เมื่อใช้ LocalizationService.GetTranslatorForPlayerAsync ให้หุ้มฟังก์ชันไว้ใน pcall() ในกรณีที่เกิดข้อผิดพลาด
ตัวอย่างโค้ดต่อไปนี้พิมพ์ Locale ID ของผู้ใช้และ Locale ID ของอินสแตนซ์ Translator สำหรับผู้ใช้เมื่อผู้ใช้เปลี่ยนภาษา:
local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
-- หาก GetTranslatorForPlayerAsync ประสบความสำเร็จ จะคืนค่าผู้แปลสำหรับภาษาในปัจจุบันของผู้เล่น
local res, translator = pcall(function()
return LocalizationService:GetTranslatorForPlayerAsync(player)
end)
-- ฟังก์ชันที่ถูกเรียกเมื่อมีการเปลี่ยนแปลงใน locale ID ของผู้เล่น
local function OnLocaleIdChanged()
print("ผู้แปลได้ถูกเปลี่ยนเป็น: " .. translator.LocaleId)
-- คุณควรทำการแปลสินทรัพย์ที่แปลด้วย API การแปลใหม่ตามภาษาของผู้เล่นที่นี่
end
-- ตรวจสอบว่า GetTranslatorForPlayerAsync ประสบความสำเร็จ
if res then
-- หากสำเร็จ ให้แปลสินทรัพย์ที่นี่โดยใช้ผู้แปล
-- ฟังการเปลี่ยนแปลงใน locale ID ของผู้เล่น
translator:GetPropertyChangedSignal("LocaleId"):Connect(OnLocaleIdChanged)
else
print('GetTranslatorForPlayerAsync ล้มเหลว: ' .. translator)
end
สร้างโมดูล TranslationHelper
เมื่อคุณโหลดผู้แปลตามภาษาดีฟอลต์ของผู้เล่น คุณอาจต้องการใช้โค้ดซ้ำ เพื่อใช้โค้ดซ้ำ ตั้งค่าช่วยเหลือ ModuleScript ที่โหลดผู้แปลอย่างปลอดภัยตามภาษาดีฟอลต์ของผู้เล่น และรวมฟังก์ชันในการให้การแปลเฉพาะและสลับภาษา
ตัวอย่างโค้ดต่อไปนี้ implements TranslationHelper ที่คุณสามารถคัดลอกไปยังโปรเจกต์ของคุณเป็น ModuleScript ใน ReplicatedStorage:
local TranslationHelper = {}
local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")
-- ตัวแปรท้องถิ่น
local player = Players.LocalPlayer
local sourceLanguageCode = "en"
-- รับผู้แปล
local playerTranslator, fallbackTranslator
local foundPlayerTranslator = pcall(function()
playerTranslator = LocalizationService:GetTranslatorForPlayerAsync(player)
end)
local foundFallbackTranslator = pcall(function()
fallbackTranslator = LocalizationService:GetTranslatorForLocaleAsync(sourceLanguageCode)
end)
-- สร้างวิธีการ TranslationHelper.setLanguage เพื่อโหลดการแปลใหม่สำหรับ TranslationHelper
function TranslationHelper.setLanguage(newLanguageCode)
if sourceLanguageCode ~= newLanguageCode then
local success, newPlayerTranslator = pcall(function()
return LocalizationService:GetTranslatorForLocaleAsync(newLanguageCode)
end)
-- เพียงแค่เขียนทับ current playerTranslator หากผู้แปลใหม่ถูกต้อง (fallbackTranslator ยังคงเป็นภาษาหลักของเกม)
if success and newPlayerTranslator then
playerTranslator = newPlayerTranslator
return true
end
end
return false
end
-- สร้างฟังก์ชัน Translate ที่ใช้ผู้แปลสำรองหากฟังก์ชันแรกล้มเหลวในการโหลดหรือคืนค่าอย่างถูกต้อง คุณยังสามารถตั้งค่าวัตถุที่อ้างอิงให้เป็นวัตถุทั่วไป
function TranslationHelper.translate(text, object)
if not object then
object = game
end
if foundPlayerTranslator then
return playerTranslator:Translate(object, text)
end
if foundFallbackTranslator then
return fallbackTranslator:Translate(object, text)
end
return false
end
-- สร้างฟังก์ชัน FormatByKey() ที่ใช้ผู้แปลสำรองหากฟังก์ชันแรกล้มเหลวในการโหลดหรือคืนค่าอย่างถูกต้อง
function TranslationHelper.translateByKey(key, arguments)
local translation = ""
local foundTranslation = false
-- ครั้งแรกพยายามแปลสำหรับภาษาของผู้เล่น (หากพบผู้แปล)
if foundPlayerTranslator then
foundTranslation = pcall(function()
translation = playerTranslator:FormatByKey(key, arguments)
end)
end
if foundFallbackTranslator and not foundTranslation then
foundTranslation = pcall(function()
translation = fallbackTranslator:FormatByKey(key, arguments)
end)
end
if foundTranslation then
return translation
else
return false
end
end
return TranslationHelper
เมื่อโมดูลอยู่ใน ReplicatedStorage ให้เรียกใช้จาก LocalScript เพื่อเรียกฟังก์ชันของโมดูล ฟังก์ชันนี้ใช้ฟังก์ชันช่วยจากโมดูลในการแปลข้อความแต่ละข้อความ:
local ReplicatedStorage = game:GetService("ReplicatedStorage")-- เรียกใช้โมดูลการแปลlocal TranslationHelper = require(ReplicatedStorage:WaitForChild("TranslationHelper"))-- ใช้ฟังก์ชันที่ให้มาใน TranslationHelperTranslationHelper.setLanguage("es")local sourceTranslation = TranslationHelper.translate("Screen")print(sourceTranslation) -- คาดหวัง Output ใน 'es': "Pantalla"