local U=game:GetService("\076\111\099\097\108\105\122\097\116\105\111\110\083\101\114\118\105\099\101")local c=game:GetService("\067\104\097\116")local t,d=pcall(function()return(UserSettings()):IsUserFeatureEnabled("\085\115\101\114\083\104\111\117\108\100\076\111\099\097\108\105\122\101\083\101\114\118\101\114\077\101\115\115\097\103\101\115")end)local u=t and d local j={System="\073\110\071\097\109\101\046\067\104\097\116\046\076\097\098\101\108\046\083\121\115\116\101\109\077\101\115\115\097\103\101\080\114\101\102\105\120";Team="\073\110\071\097\109\101\046\067\104\097\116\046\076\097\098\101\108\046\084\101\097\109\077\101\115\115\097\103\101\080\114\101\102\105\120";["\070\114\111\109 "]="\073\110\071\097\109\101\046\067\104\097\116\046\076\097\098\101\108\046\070\114\111\109";["\084\111 "]="\073\110\071\097\109\101\046\067\104\097\116\046\076\097\098\101\108\046\084\111"}local a={_hasFetchedLocalization=false}function a._getTranslator(t)if not t._translator and not t._hasFetchedLocalization then t._hasFetchedLocalization=true local d=c:WaitForChild("\067\104\097\116\076\111\099\097\108\105\122\097\116\105\111\110",4)if d then t._translator=d:GetTranslator(U.RobloxLocaleId);(U:GetPropertyChangedSignal("\082\111\098\108\111\120\076\111\099\097\108\101\073\100")):Connect(function()t._hasFetchedLocalization=false t._translator=nil end)else warn("\077\105\115\115\105\110\103 \067\104\097\116\076\111\099\097\108\105\122\097\116\105\111\110\046 \067\104\097\116 \105\110\116\101\114\102\097\099\101 \119\105\108\108 \110\111\116 \098\101 \108\111\099\097\108\105\122\101\100\046")end end return t._translator end function a.Get(d,U,c,t)local u=c pcall(function()local c=d:_getTranslator()if c then u=c:FormatByKey(U,t)else warn("\077\105\115\115\105\110\103 \084\114\097\110\115\108\097\116\111\114\046 \085\115\101\100 \100\101\102\097\117\108\116 \102\111\114",U)end end)return u end function a.LocalizeFormattedMessage(c,U)if not u then return U end local t,d=string.find(U,"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\075\069\089\125")if not t then return U end local j,a=string.find(U,"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\068\069\070\065\085\076\084\125")if not j then return U end local w,q=string.find(U,"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\080\065\082\065\077\083\125")local R=string.sub(U,d+1,j-1)local T if not w then T=string.sub(U,a+1)return c:Get(R,T)end T=string.sub(U,a+1,w-1)local o=string.sub(U,q+1)local e={}for U,c in string.gmatch(o,"\040\091\094\037\115\093\043\041\061\040\091\094\037\115\093\043\041")do e[U]=c end return c:Get(R,T,e)end function a.FormatMessageToSend(j,U,c,t,d)if u then if t and d then return"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\075\069\089\125"..(U..("\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\068\069\070\065\085\076\084\125"..(c..("\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\080\065\082\065\077\083\125"..(t..("\061"..d))))))else return"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\075\069\089\125"..(U..("\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\068\069\070\065\085\076\084\125"..c))end else if t and d then return string.gsub(j:Get(U,c),"\123"..(t.."\125"),d)else return j:Get(U,c)end end end


local LocalizationService = game:GetService("LocalizationService")
local ChatService = game:GetService("Chat")
local success, userShouldLocalizeServerMessages = pcall(function() return UserSettings():IsUserFeatureEnabled("UserShouldLocalizeServerMessages") end)
local userShouldLocalizeServerMessages = success and userShouldLocalizeServerMessages

local existingKey = {
	["System"] = "InGame.Chat.Label.SystemMessagePrefix",
	["Team"] = "InGame.Chat.Label.TeamMessagePrefix",
	["From "] = "InGame.Chat.Label.From",
	["To "] = "InGame.Chat.Label.To",
}
local ChatLocalization = {
	_hasFetchedLocalization = false,
}

function ChatLocalization:_getTranslator()
	if not self._translator and not self._hasFetchedLocalization then
		-- Don't keep retrying if this fails.
		self._hasFetchedLocalization = true
		
		local localizationTable = ChatService:WaitForChild("ChatLocalization", 4)
		if localizationTable then
			self._translator = localizationTable:GetTranslator(LocalizationService.RobloxLocaleId)
			LocalizationService:GetPropertyChangedSignal("RobloxLocaleId"):Connect(function()
				-- If RobloxLocaleId changes invalidate the cached Translator.
				self._hasFetchedLocalization = false
				self._translator = nil
			end)
		else
			warn("Missing ChatLocalization. Chat interface will not be localized.")
		end
	end
	return self._translator
end

function ChatLocalization:Get(key, default, extraParameters)
	local rtv = default
	pcall(function()
		local translator = self:_getTranslator()
		if translator then
			rtv = translator:FormatByKey(key, extraParameters)
		else
			warn("Missing Translator. Used default for", key)
		end
	end)
	return rtv
end

function ChatLocalization:LocalizeFormattedMessage(message)

	if not userShouldLocalizeServerMessages then
		return message
	end

	local keyStart, keyEnd = string.find(message, "{RBX_LOCALIZATION_KEY}")
	if not keyStart then
		return message
	end
	local defaultStart, defaultEnd = string.find(message, "{RBX_LOCALIZATION_DEFAULT}")
	if not defaultStart then
		return message
	end
	local paramStart, paramEnd = string.find(message, "{RBX_LOCALIZATION_PARAMS}")
	local key = string.sub(message, keyEnd + 1, defaultStart - 1)
	local default
	if not paramStart then
		default = string.sub(message, defaultEnd + 1)
		return self:Get(key, default)
	end
	default = string.sub(message, defaultEnd + 1, paramStart - 1)
	local params = string.sub(message, paramEnd + 1)
	local extraParameters = {}
	for k,v in string.gmatch(params,"([^%s]+)=([^%s]+)") do
		extraParameters[k] = v
	end

	return self:Get(key, default, extraParameters)
end

function ChatLocalization:FormatMessageToSend(key, defaultMessage, parameterName, value)
	if userShouldLocalizeServerMessages then
		if parameterName and value then
			return "{RBX_LOCALIZATION_KEY}"..key.."{RBX_LOCALIZATION_DEFAULT}"..defaultMessage
				.."{RBX_LOCALIZATION_PARAMS}"..parameterName.."="..value
		else
			return "{RBX_LOCALIZATION_KEY}"..key.."{RBX_LOCALIZATION_DEFAULT}"..defaultMessage
		end
	else
		if parameterName and value then
			return string.gsub(self:Get(key,defaultMessage), "{"..parameterName.."}", value)
		else
			return self:Get(key,defaultMessage)
		end
	end
end
