本地化服務

LocalizationService是一種幫助你管理多語言並將你的遊戲本地化給全球玩家的服務。

它允許你將遊戲的文本和其他內容翻譯並適應不同語言和地區,使你的遊戲對全球受眾更具可訪性。

local LocalizationService = game:GetService("LocalizationService")

local function createLocalizationTable(entries)
	local localTable = Instance.new("LocalizationTable")
	localTable.DevelopmentLanguage = LocalizationService.SystemLocaleId
	localTable:SetEntries(entries)
	localTable.Name = "GameLocalization"
	localTable.Parent = LocalizationService
	return localTable
end

local entries = {
	{
		Key = "Hello_World",
		Values = { 
			["en"] = "Hello world!", -- English
			["fr"] = "Bonjour le monde!", -- French
			["de"] = "Hallo Welt!", -- German
		}
	}
}

createLocalizationTable(entries)
local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")

local helloWorldTable = LocalizationService:WaitForChild("GameLocalization")

local function getShortLocale(locale)
	return string.sub(locale, 1, 2)
end

local function greetPlayer(player)
	local locale = getShortLocale(player.LocaleId)
	local message = helloWorldTable:GetString(locale, "Hello_World")
	print(message)
end


Players.PlayerAdded:Connect(function(player)
	greetPlayer(player)
end)

for _, player in pairs(Players:GetPlayers()) do
	greetPlayer(player)
end
local LocalizationService = game:GetService("LocalizationService")
local Players = game:GetService("Players")

local helloWorldTable = LocalizationService:WaitForChild("GameLocalization")

local function getShortLocale(locale)
	return string.sub(locale, 1, 2)
end

local function greetPlayer(player)
	local locale = getShortLocale(player.LocaleId)
	local message = helloWorldTable:GetString(locale, "Hello_World")
	print(message)
end

local player = Players.LocalPlayer
greetPlayer(player)

如果您覺得本教程對您有幫助,並且願意支持我的工作,請考慮請我喝杯咖啡。

非常感謝您的支持!

請我喝杯咖啡