Module:Translations

From the RuneScape Wiki, the wiki for all things RuneScape
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Translations/doc. [edit] [history] [purge]
Module:Translations's function main is invoked by Calculator:Translations/template.
Module:Translations requires Module:Array.
Module:Translations requires Module:Mw.html extension.
Module:Translations loads data from Module:Translations/achievements.
Module:Translations loads data from Module:Translations/items.
Module:Translations loads data from Module:Translations/music.
Module:Translations loads data from Module:Translations/npcs.
Module:Translations loads data from Module:Translations/quests.
Function list
L 12 — get_data
L 32 — p.main
L 38 — p._main
L 126 — p._simple

Allows translation of item, npc, music and quest names. Data is stored in subpages of this module, see data modules below. Function main is invoked by the Calculator:Translations/template.

Helper Functions

This module is a helper module to be used by other modules; it may not be designed to be invoked directly. See RuneScape:Lua/Helper modules for a full list and more information. For a full list of modules using this helper click here

FunctionTypeUse
_main(inp, opts)string/number, tableGenerates a table of translations for the given input inp. Options are passed as a table of key-value pairs, available options are
  • dataType the type of thing being translated, one of item, npc, quest or music. Defaults to item.
  • fuzzy whether the input should be seen as imprecise true or exact false. Defaults to false.
Returns a table containing the id and all available translations (en, pt, fr, de).
_simple(inp, lang, dtype, out)string, string, string, stringReturns the requested translation for the given input as a plain string. This looks for exact values.
  • inp the input to look for, based on lang
  • lang the language of the input, one of id, en, pt, fr, or de.
  • dtype the type of the inputm one of item, npc, quest or music.
  • out the desired output language, one of id, en, pt, fr, or de.
Example:
local trans = require( 'Module:Translations' )
mw.log( trans._main( 'Bond' ) ) --> <table class="wikitable sortable"><th>ID</th><th>English</th><th>Portuguese</th><th>French</th><th>German</th><tr><td>29492</td><td>Bond</td><td>Nota</td><td>N/A</td><td>N/A</td></tr></table>
mw.log( trans._simple( 'Bond', 'en', 'item', 'pt') ) --> Nota

Data Modules


local p = {}
local arr = require('Module:Array')
local data = {
	['npcs'] = mw.loadData('Module:Translations/npcs'),
	['music'] = mw.loadData('Module:Translations/music'),
	['quests'] = mw.loadData('Module:Translations/quests'),
	['achievements'] = mw.loadData('Module:Translations/achievements'),
	['items'] = mw.loadData('Module:Translations/items'),
}
require('Module:Mw.html extension')

local function get_data(dtype)
	local hasfrde = false
	if dtype == 'npc' or dtype == 'npcs' or dtype == 'monster' then
		dtype = 'npcs'
		hasfrde = true
	elseif dtype == 'music' or dtype == 'track' or dtype == 'tracks' then
		dtype = 'music'
	elseif dtype == 'quest' or dtype == 'quests' then
		dtype = 'quests'
		hasfrde = true
	elseif dtype == 'achievement' or dtype == 'achievements' then
		dtype = 'achievements'
		hasfrde = true
	elseif dtype == 'item' or dtype == 'items' then
		dtype = 'items'
	end

	return data[dtype] or false, hasfrde
end

function p.main(frame)
	local args = frame:getParent().args
	local fuzzy = args.fuzzy == 'true'
	return p._main(args.input, {dataType=args.dataType, fuzzy=fuzzy})
end

function p._main(inp, opts)
	local inp = tonumber(inp) or inp
	if not inp then
		return 'No search term given!'
	end

	opts = opts or {}
	mw.logObject( opts )
	local dtype = string.lower(opts.dataType or 'item')
	mw.log(dtype)
	local transl = {}

	local data, hasfrde = get_data(dtype)
	if data == false then
		return 'Unknown search type given!'
	end

	if type(inp) == 'string' and opts.fuzzy == true and string.find(inp, '%S') then
		local query = string.lower(inp)
		for k, v in pairs(data.idTbl) do
			local search = (v.en or '$NO_NAME$') .. '%%' ..  (v.pt or '$NO_NAME$')
			if hasfrde then
				search = search .. '%%' .. (v.fr or '$NO_NAME$') .. '%%' .. (v.de or '$NO_NAME$')
			end
			search = string.lower(search)

			if string.find(search, query) then
				table.insert(transl, v)
			end
		end
	else
		local res =  false
		if data.idTbl[inp] then
			res = data.idTbl[inp]
		elseif hasfrde then
			res = data.enTbl[inp] or data.ptTbl[inp] or data.frTbl[inp] or data.deTbl[inp] or false
		else
			res = data.enTbl[inp] or data.ptTbl[inp] or false
		end

		if res then
			table.insert(transl, res)
		end
	end

	transl = arr.unique(transl, function(x) return (x.pt or '') .. (x.en or '') .. (x.fr or '') .. (x.de or '') end)
	table.sort(transl, function(x, y)
		if x.en and y.en then
			return x.en < y.en
		elseif x.pt and y.pt then
			return x.pt < y.pt
		end
	end)

	local ret = mw.html.create('table'):addClass('wikitable sortable')
	ret
		:tr()
			:th('ID')
			:th('English')
			:th('Portuguese')
			:IF(hasfrde)
				:th('French')
				:th('German')
			:END()
		:done()

	local na = '<td class="table-na" style="text-align:center;" data-sort-value="0">N/A</td>'

	if #transl == 0 then
		ret:tr():td{'No results found!', attr={'colspan', hasfrde and '5' or '3'}, css={'text-align', 'center'}}
		return ret:allDone()
	end

	for _, v in ipairs(transl) do
		ret
			:tr()
				:IF(v.id):td(v.id):addClass('translations-id'):ELSE():node(na):END()
				:IF(v.en):td(v.en):addClass('translations-en copyable'):ELSE():node(na):END()
				:IF(v.pt):td(v.pt):addClass('translations-pt copyable'):ELSE():node(na):END()
				:IF(hasfrde)
					:IF(v.fr):td(v.fr):addClass('translations-fr copyable'):ELSE():node(na):END()
					:IF(v.de):td(v.de):addClass('translations-de copyable'):ELSE():node(na):END()
				:END()
	end

	return ret
end

function p._simple(inp, lang, dtype, out)
	dtype = string.lower(dtype) or 'item'
	local data = get_data(dtype)
	if not data then
		mw.log('Invalid data type: '..dtype)
		return false
	end

	lang = string.lower(lang) or 'id'
	local res = false
	if data[lang..'Tbl'] then
		res = data[lang..'Tbl'][inp]
	else
		res = data.enTbl[inp]
	end

	if not res then
		mw.log('Not found\nInput: '..inp..'\nLang: '..lang)
		return false
	end

	out = string.lower(out) or 'pt'
	if res[out] then
		return res[out]
	else
		mw.log('No "'..out..'" translation found for:')
		mw.logObject(res)
		return false
	end
end

return p