local w=game:GetService("\072\116\116\112\083\101\114\118\105\099\101")local z={}local function g(w,z,H,C)if H~=""then H=H.."\046"end for C,x in pairs(C)do local Q=H..C if type(x)=="\116\097\098\108\101"then g(w,z,Q,x)elseif type(x)=="\115\116\114\105\110\103"then local g=""local H=""if z=="\101\110"then g=x end w:SetEntryValue(Q,g,H,z,x)else error("\066\097\100 \116\121\112\101 \102\111\114 \118\097\108\117\101 \105\110 \'"..(Q.."\'\046"))end end end function z.localeFromName(w)if w:sub(-5)=="\046\106\115\111\110"then return w:sub(1,#w-5)else return w end end function z.loadFolder(w)local g=Instance.new("\076\111\099\097\108\105\122\097\116\105\111\110\084\097\098\108\101")for w,H in pairs(w:GetChildren())do if H:IsA("\083\116\114\105\110\103\086\097\108\117\101")then local w=z.localeFromName(H.Name)z.addJsonToTable(g,w,H.Value)end end return g end function z.addJsonToTable(z,H,C)local x=w:JSONDecode(C)g(z,H,"",x)end return z
--- Utility to build a localization table from json, intended to be used with rojo
-- @module JsonToLocalizationTable

local HttpService = game:GetService("HttpService")

local lib = {}

--- Recursively iterates through the object to construct strings and add it to the localization table
-- @param localeId The localizationid to add
-- @param baseKey the key to add
-- @param object The value to iterate over
local function recurseAdd(localizationTable, localeId, baseKey, object)
	if baseKey ~= "" then
		baseKey = baseKey .. "."
	end

	for index, value in pairs(object) do
		local key = baseKey .. index
		if type(value) == "table" then
			recurseAdd(localizationTable, localeId, key, value)
		elseif type(value) == "string" then
			local source = ""
			local context = ""

			if localeId == "en" then
				source = value
			end

			localizationTable:SetEntryValue(key, source, context, localeId, value)
		else
			error("Bad type for value in '" .. key .. "'.")
		end
	end
end

--- Extracts the locale from the name
-- @param name The name to parse
-- @return The locale
function lib.localeFromName(name)
	if name:sub(-5) == ".json" then
		return name:sub(1, #name-5)
	else
		return name
	end
end

--- Loads a folder into a localization table
-- @parm folder A Roblox folder with StringValues containing JSON, named with the localization in mind
function lib.loadFolder(folder)
	local localizationTable = Instance.new("LocalizationTable")
	for _, item in pairs(folder:GetChildren()) do
		if item:IsA("StringValue") then
			local localeId = lib.localeFromName(item.Name)
			lib.addJsonToTable(localizationTable, localeId, item.Value)
		end
	end
	return localizationTable
end

--- Adds json to a localization table
-- @param localizationTable The localization table to add to
-- @param localeId The localeId to use
-- @param json The json to add with
function lib.addJsonToTable(localizationTable, localeId, json)
	local object = HttpService:JSONDecode(json)
	recurseAdd(localizationTable, localeId, "", object)
end

return lib

