Quantcast
--[[
	This Lua script generates the localization data file from the information at:
	http://wow.curseforge.com/addons/ovale/localization/
--]]

-- LuaSocket library.
local http = require("socket.http")

-- The name of the file to append the localization data.
local localizationFileName = "../Localization.lua"

-- The string delimiting the portion of the file that is preserved from the
-- rest of the file which is automatically generated.
local delimiter = "THE REST OF THIS FILE IS AUTOMATICALLY GENERATED."
local delimiterFound = false

-- The base URL of the localization web application.
local baseURL = "http://wow.curseforge.com/addons/ovale/localization/"

local function GetLocalization(locale)
	local params = {
		{ namespace = "" },
		{ format = "lua_additive_table" },
		{ language = locale },
		{ handle_unlocalized = "blank" },
		{ escape_non_ascii = "" },
		{ handle_subnamespaces = "concat" },
	}
	local query = {}
	for _, data in ipairs(params) do
		for key, value in pairs(data) do
			table.insert(query, key .. "=" .. value)
		end
	end
	local url = baseURL .. "export.txt?" .. table.concat(query, "&")

	local b, c, h, s = http.request(url)
	return b
end

-- Save original input and output handles.
local saveInput = io.input()
local saveOutput = io.output()

local output = {}

-- Read all of the lines from the file up to and including the delimiter.
do
	local localizationFile = assert(io.open(localizationFileName, "r"))
	for line in localizationFile:lines() do
		output[#output + 1] = line
		if string.find(line, delimiter) then
			delimiterFound = true
			output[#output + 1] = "-- " .. baseURL
			output[#output + 1] = ""
			break
		end
	end
	localizationFile:close()
end

if delimiterFound then
	local locales = {
		"deDE",	-- German
		"enUS", -- American English
		"esES",	-- Spanish
		"esMX",	-- Latin American Spanish
		"frFR", -- French
		"itIT",	-- Italian
		"koKR",	-- Korean
		"ptBR", -- Brazilian Portuguese
		"ruRU",	-- Russian
		"zhCN",	-- Simplified Chinese
		"zhTW",	-- Traditional Chinese
	}

	local count = 0
	for _, locale in ipairs(locales) do
		print("Fetching " .. locale .. " locale data.")
		local localization = GetLocalization(locale)
		if localization then
			count = count + 1
			output[#output + 1] = "local locale = GetLocale()"
			output[#output + 1] = ""
			output[#output + 1] = string.format("------------------------ Locale: %s ------------------------", locale)
			if count == 1 then
				output[#output + 1] = string.format('if locale == "%s" then', locale)
			else
				output[#output + 1] = string.format('elseif locale == "%s" then', locale)
			end
			output[#output + 1] = ""
			output[#output + 1] = localization
		else
			print("    No " .. locale .. " locale data received.")
		end
	end
	if count > 0 then
		output[#output + 1] = "end"
	end
	output[#output + 1] = ""
end

-- Overwrite file with new output.
do
	local localizationFile = io.open(localizationFileName, "w")
	io.output(localizationFile)
	io.write(table.concat(output, "\n"))
	localizationFile:close()
end

-- Restore original input and output handles.
io.input(saveInput)
io.output(saveOutput)