local q=game:GetService("\076\111\099\097\108\105\122\097\116\105\111\110\083\101\114\118\105\099\101")local U=game:GetService("\067\104\097\116")local Q,T=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 R=Q and T local D={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 L={_hasFetchedLocalization=false}function L._getTranslator(Q)if not Q._translator and not Q._hasFetchedLocalization then Q._hasFetchedLocalization=true local T=U:WaitForChild("\067\104\097\116\076\111\099\097\108\105\122\097\116\105\111\110",4)if T then Q._translator=T:GetTranslator(q.RobloxLocaleId);(q:GetPropertyChangedSignal("\082\111\098\108\111\120\076\111\099\097\108\101\073\100")):Connect(function()Q._hasFetchedLocalization=false Q._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 Q._translator end function L.Get(T,q,U,Q)local R=U pcall(function()local U=T:_getTranslator()if U then R=U:FormatByKey(q,Q)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",q)end end)return R end function L.LocalizeFormattedMessage(U,q)if not R then return q end local Q,T=string.find(q,"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\075\069\089\125")if not Q then return q end local D,L=string.find(q,"\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 D then return q end local Z,e=string.find(q,"\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 J=string.sub(q,T+1,D-1)local a if not Z then a=string.sub(q,L+1)return U:Get(J,a)end a=string.sub(q,L+1,Z-1)local S=string.sub(q,e+1)local x={}for q,U in string.gmatch(S,"\040\091\094\037\115\093\043\041\061\040\091\094\037\115\093\043\041")do x[q]=U end return U:Get(J,a,x)end function L.FormatMessageToSend(D,q,U,Q,T)if R then if Q and T then return"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\075\069\089\125"..(q..("\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"..(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"..(Q..("\061"..T))))))else return"\123\082\066\088\095\076\079\067\065\076\073\090\065\084\073\079\078\095\075\069\089\125"..(q..("\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"..U))end else if Q and T then return string.gsub(D:Get(q,U),"\123"..(Q.."\125"),T)else return D:Get(q,U)end end end
--!nocheck

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
