Automatically translate strings?

Is there a way to add strings to the localization translation through script?

1 Like

You can do this by first getting the Translator using the LocalizationService:GetTranslatorForPlayer() method for the player then using the Translator:Translate() method to translate your string using game as the Context.

--[[ Local Script NOT TESTED]]-
local text = "Hello"

local Players = game:GetService("Players")
local LocalizationSerivce = game:GetService("LocalizationService")
local localPlayer = Players.LocalPlayer
local translator = LocalizationService:GetTranslatorForPlayer(localPlayer)
local translatedText = translator:Translate(game, text)

print(translatedText)
2 Likes

Thanks! Is there a way to apply a specific language though? I’m assuming that it just grabs the player’s language in that script, correct me if I’m wrong!

Yeah! Alternatively to LocalizationService:GetTranslatorForPlayer() you can use LocalizationService:GetTranslatorForLocaleAsync() to get the Translator for a specific language. Remember that you need to use locales when accessing this function, you can a list of all locales here

--[[ Local Script NOT TESTED]]-
local text = "Hello"
local locale = "fr" -- fr for french

local Players = game:GetService("Players")
local LocalizationSerivce = game:GetService("LocalizationService")
local localPlayer = Players.LocalPlayer
local success, translator = pcall(function()
	return LocalizationService:GetTranslatorForLocaleAsync(locale)
end)

if success then
	local translatedText = translator:Translate(game, text)
	print(translatedText)
end
2 Likes

Does this HAVE to be in a local script, by any chance? I’m using it in a regular script as a plug-in

Reading over the documentation, I don’t see anything that requires it to be a local script.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.