Module:User:Mr. Stradivarius/Latvian declension
This module is being discussed in accordance with Wikipedia's deletion policy. Help reach a consensus at its entry. |
The data page
[edit]To use this module, you need to make a data sub-module for each template. This data module can have any name, but you should choose something meaningful. For example, to make a template for month declensions, you could make a new module at the page "Module:User:Mr. Stradivarius/Latvian declension/months".
Data modules should have the following format:
return {
["foo"] = {
n = "Foo1",
g = "Foo2",
d = "Foo3",
a = "Foo4",
i = "Foo5",
l = "Foo6",
},
["bar"] = {
n = "Bar1",
g = "Bar2",
d = "Bar3",
a = "Bar4",
i = "Bar5",
l = "Bar6",
},
-- Add more data here
}
Keys like "foo" and "bar" should be lower case. The "n", "g", "d", "a", "i" and "l" keys should also be lower case. Also, if you want to use numbers instead of the "foo" or "bar" keys, then they should be written without speech marks (like [7]
, not ["7"]
).
For our months data page, we might write the following:
return {
[1] = {
n = "Janvāris",
g = "Janvāra",
d = "Janvārim",
a = "Janvāri",
i = "ar Janvāri",
l = "Janvārī",
},
[2] = {
n = "Februāris",
g = "Februāra",
d = "Februārim",
a = "Februāri",
i = "ar Februāri",
l = "Februārī",
},
-- ...
}
The template
[edit]You can make any page into a template for use with this module. But it's probably best to choose a page in the template namespace that has a meaningful name. You should create your template with the following code:
{{#invoke:User:Mr. Stradivarius/Latvian declension|data module subpage name}}
For our months example, let's say we chose the template name "Template:Latvian month". We would create the template page with the following code:
{{#invoke:User:Mr. Stradivarius/Latvian declension|months}}
Note: you can't use the module directly from #invoke; it must be used from a template, otherwise the parameters will not be detected.
Using the template
[edit]The templates are used like this:
{{template name|word code|declension code}}
The word codes are the ones you defined in the data module, like "foo" and "bar". When using the template itself, these can be either upper case, lower case, or a mixture. The declension code can be any of "n", "g", "d", "a", "i" and "l" (and "ģ" is an alias for "g"). These can also be upper case. The module will look in the data submodule for a match for these two keys, and if it finds one, it will return it. If not, it will return the text "Invalid".
So for our months example, we would use the following syntax:
{{Latvian month|word code|declension code}}
Here are some examples of how it would work:
{{Latvian month|1|n}}
→ Janvāris{{Latvian month|1|Ģ}}
→ Janvāra{{Latvian month|2|I}}
→ ar Februāri
-- This module generates templates for dealing with Latvian declensions.
local libraryUtil = require('libraryUtil')
local DATA_PAGE_PREFIX = mw.getCurrentFrame():getTitle() .. '/'
local NO_RESULT = 'Invalid'
local function main(dataPage, code, declen)
-- Get the data page first, as we want a big red error message if the data
-- page is bad.
libraryUtil.checkType('main', 1, dataPage, 'string')
local data = mw.loadData(DATA_PAGE_PREFIX .. dataPage)
-- Check argument types and do preprocessing.
if type(code) == 'string' then
if tonumber(code) then
code = tonumber(code)
else
code = mw.ustring.lower(code)
end
elseif type(code) ~= 'number' then
return NO_RESULT
end
if type(declen) ~= 'string' then
return NO_RESULT
end
-- Get the data.
local termData = data[code]
if termData == nil then
return NO_RESULT
elseif type(termData) ~= 'table' then
error(string.format(
"the data page '%s' contains malformatted data for code '%s'",
dataPage,
tostring(code)
), 2)
end
if declen == 'Ģ' or declen == 'ģ' then
-- Normalize declension string without using mw.ustring, which can be
-- slow.
declen = 'g'
else
declen = string.lower(declen)
end
return termData[declen] or NO_RESULT
end
return setmetatable(
{main = main},
{
__index = function (p, key)
return function (frame)
local args = require('Module:Arguments').getArgs(frame, {
parentOnly = true
})
return main(key, args[1], args[2])
end
end
}
)