Module:languages/templates: Difference between revisions

No edit summary
No edit summary
 
Line 1: Line 1:
local export = {}
local concat = table.concat
local concat = table.concat
local insert = table.insert
local insert = table.insert
local sort = table.sort
local sort = table.sort
local scripts_module = "Module:scripts"


local export = {}
local function get_script_by_code(...)
    get_script_by_code = require(scripts_module).getByCode
    return get_script_by_code(...)
end


function export.exists(frame)
function export.exists(frame)
Line 15: Line 21:
do
do
local function getByCode(frame, allow_etym)
local function getByCode(frame, allow_etym)
local plain = {}
local plain = true
local args = require("Module:parameters").process(frame.args, {
local args = require("Module:parameters").process(frame.args, {
[1] = {required = true, type = allow_etym and "language" or "full language"},
[1] = {required = true, type = allow_etym and "language" or "full language"},
[2] = {required = true},
[2] = {required = true},
[3] = plain,
[3] = plain,
[4] = {type = "script"},
[4] = plain,
[5] = plain,
[5] = plain,
})
})
local function check_empty(...)
local varargs = {...}
for i = 1, select("#", ...) do
if args[varargs[i]] then
error(("Cannot specify a value for argument %s= when using subfunction '%s' of getByCode()"):format(
varargs[i], args[2]))
end
end
end
return require("Module:language-like").templateGetByCode(args,
return require("Module:language-like").templateGetByCode(args,
function(itemname)
function(itemname)
Line 34: Line 51:
end
end
if list then
if list then
local retval = list[tonumber(args[3]) or error("Please specify the numeric index of the desired item.")]
check_empty(4, 5)
local retval = list[tonumber(args[3]) or error("Please specify the numeric index of the desired item in 3=.")]
if retval then
if retval then
if type(retval) == "string" then
if type(retval) == "string" then
Line 46: Line 64:
end
end
if itemname == "transliterate" then
if itemname == "transliterate" then
return (args[1]:transliterate(args[3], args[4], args[5])) or ""
local sc = get_script_by_code(args[4])
return (args[1]:transliterate(args[3], sc, args[5])) or ""
elseif itemname == "makeDisplayText" then
elseif itemname == "makeDisplayText" then
return (args[1]:makeDisplayText(args[3], args[4])) or ""
local sc = get_script_by_code(args[4])
check_empty(5)
return (args[1]:makeDisplayText(args[3], sc)) or ""
elseif itemname == "makeEntryName" then
elseif itemname == "makeEntryName" then
return (args[1]:makeEntryName(args[3], args[4])) or ""
-- FIXME, find places that use makeEntryName and convert to stripDiacritics
local sc = get_script_by_code(args[4])
check_empty(5)
return args[1]:makeEntryName(args[3], sc) or ""
elseif itemname == "stripDiacritics" then
local sc = get_script_by_code(args[4])
check_empty(5)
return args[1]:stripDiacritics(args[3], sc) or ""
elseif itemname == "makeSortKey" then
elseif itemname == "makeSortKey" then
return (args[1]:makeSortKey(args[3], args[4])) or ""
local sc = get_script_by_code(args[4])
check_empty(5)
return (args[1]:makeSortKey(args[3], sc)) or ""
elseif itemname == "logicalToPhysical" then
check_empty(4, 5)
return args[1]:logicalToPhysical(args[3]) or ""
elseif itemname == "countCharacters" then
elseif itemname == "countCharacters" then
return args[4]:countCharacters(args[3] or "")
local sc = get_script_by_code(args[4])
check_empty(5)
return sc:countCharacters(args[3] or "")
elseif itemname == "findBestScript" then
elseif itemname == "findBestScript" then
return args[1]:findBestScript(args[3] or ""):getCode()
check_empty(5)
return args[1]:findBestScript(args[3] or "", args[4]):getCode()
end
end
end
end