Jump to content

မေႃႇၵျူး:languages/templates

လုၵ်ႉတီႈ ဝိၵ်ႇသျိၼ်ႇၼရီႇ မႃး

This module provides access to Module:languages from templates, so that they can make use of the information stored there.

If you know a language's code (for example, "en") and you want to find out its canonical name, you can use this:

  • {{#invoke:languages/templates|getByCode|en|getCanonicalName}} (returns "English")

If you know a language's canonical name (for example, "English") and you want to find out its code, use this:

  • {{#invoke:languages/templates|getByCanonicalName|English|getCode}} (returns "en")

Both of these functions are subst:able (type {{subst:#invoke:...).

Exported functions

[မႄးထတ်း]

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Check whether a language code exists and is valid. It will return "1" if the language code exists, and the empty string "" if it does not.

This is rarely needed, because a script error will result when someone uses a code that is not valid, so you do not need this just to check for errors. However, in case you need to decide different actions based on whether a certain parameter is a language code or something else, this function can be useful.

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Queries information about a language code.

  • The language code should be one of the codes that is defined in Module:languages data. If it is missing or does not exist, the result will be a script error.
  • The item is the name of one of the functions of a language object, such as getCanonicalName or getScripts. If no item has been provided, the result will be a script error.
  • The index is optional, and is used for items that are lists, such as getOtherNames or getScripts. It selects which item in the list to return. On items that are single strings, like getFamily, it has no effect. If an index is given that is higher than the number of items in the list, the result will be an empty string.

For example, to request the canonical name of the language whose code is en:

{{#invoke:languages/templates|getByCode|en|getCanonicalName}}
  • Result: ဢိင်းၵလဵတ်ႈ

To request its second name, if any:

{{#invoke:languages/templates|getByCode|en|getOtherNames|1}}
  • Result: Modern English

To request its family:

{{#invoke:languages/templates|getByCode|en|getFamily}}
  • Result: gmw-ang

getByCanonicalName

[မႄးထတ်း]

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Gets the language code corresponding to a canonical name.

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Like the above, except it will also accept other names for the language that are listed in the language's otherNames field. For instance:

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).
Script error: The function "getByName" does not exist.

makeEntryName

[မႄးထတ်း]

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Gets the entry name corresponding to the display name of a link. The entry name is the display name with language-specific diacritics removed, and is the actual name of the page that is normally displayed in a link using the display name. For example, the display name of Script error: The function "language_name_link_t" does not exist. is amo, without the macron over the o, and the display name of Script error: The function "language_name_link_t" does not exist. is школа, without the acute accent marking the stress.

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).
Script error: The function "makeEntryName" does not exist.

getCanonicalName

[မႄးထတ်း]

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).

Gets the canonical name for a language code if the language code is valid, or else returns an empty string. It uses a table that converts language code to canonical name, generated by Module:languages/code to canonical name. Requires more Lua memory than getByCode (about 10 megabytes) for a single instance, but may require less memory on pages that call the function many times.

Unlike Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table)., this function does does not yield a script error for an invalid language code.

Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).
Lua error in မေႃႇၵျူး:template_parser at line 1031: bad argument #1 to 'find' (string expected, got table).
ဢိင်းၵလဵတ်ႈ

local concat = table.concat
local insert = table.insert
local sort = table.sort

local export = {}

function export.exists(frame)
	return require("Module:languages").getByCode(
		require("Module:parameters").process(frame.args, {
			[1] = {required = true}
		})[1]
	) and "1" or ""
end

do
	local function getByCode(frame, allow_etym)
		local plain = {}
		local args = require("Module:parameters").process(frame.args, {
			[1] = {required = true, type = "language", etym_lang = allow_etym},
			[2] = {required = true},
			[3] = plain,
			[4] = {type = "script"},
			[5] = plain,
		})
		return require("Module:language-like").templateGetByCode(args,
			function(itemname)
				local list
				if itemname == "getWikimediaLanguages" then
					list = args[1]:getWikimediaLanguages()
				elseif itemname == "getScripts" then
					list = args[1]:getScriptCodes()
				elseif itemname == "getAncestors" then
					list = args[1]:getAncestors()
				end
				if list then
					local retval = list[tonumber(args[3]) or error("Please specify the numeric index of the desired item.")]
					if retval then
						if type(retval) == "string" then
							return retval
						else
							return retval:getCode()
						end
					else
						return ""
					end
				end
				if itemname == "transliterate" then
					return (args[1]:transliterate(args[3], args[4], args[5])) or ""
				elseif itemname == "makeDisplayText" then
					return (args[1]:makeDisplayText(args[3], args[4])) or ""
				elseif itemname == "makeEntryName" then
					return (args[1]:makeEntryName(args[3], args[4])) or ""
				elseif itemname == "makeSortKey" then
					return (args[1]:makeSortKey(args[3], args[4])) or ""
				elseif itemname == "countCharacters" then
					return args[4]:countCharacters(args[3] or "")
				elseif itemname == "findBestScript" then
					return args[1]:findBestScript(args[3] or ""):getCode()
				end
			end
		)
	end
	
	-- Used by the following JS:
	-- * [[WT:ACCEL]]
	-- * [[WT:EDIT]]
	-- * [[WT:NEC]]
	function export.getByCode(frame)
		return getByCode(frame, false)
	end
	
	function export.getByCodeAllowEtym(frame)
		return getByCode(frame, true)
	end
end

function export.getByCanonicalName(frame)
	local lang = require("Module:languages").getByCanonicalName(
		require("Module:parameters").process(frame.args, {
			[1] = {required = true}
		})[1],
		nil,
		true
	)
	return lang and lang:getCode() or ""
end

function export.getCanonicalName(frame)
	local args = require("Module:parameters").process(
		require("Module:yesno")(frame.args.parent) and frame:getParent().args or frame.args,
		{
			[1] = {required = true},
			["return_if_invalid"] = {type = "boolean"},
		}
	)
	local lang = require("Module:languages").getByCode(args[1], nil, true)
	return lang and lang:getCanonicalName() or not args.return_if_invalid and "" or args[1]
end

function export.getFull(frame)
	local args = require("Module:parameters").process(
		require("Module:yesno")(frame.args.parent) and frame:getParent().args or frame.args,
		{
			[1] = {required = true, type = "language", etym_lang = true},
		}
	)
	return args[1]:getFullCode()
end

function export.getChildren(frame)
	local args = require("Module:parameters").process(
		require("Module:yesno")(frame.args.parent) and frame:getParent().args or frame.args,
		{
			[1] = {required = true, type = "language", etym_lang = true},
		}
	)
	local children = args[1]:getChildren()
	
	sort(children, function(a, b)
		return a:getCanonicalName() < b:getCanonicalName()
	end)
	
	local list = {}
	for _, child in ipairs(children) do
		insert(list, "* " .. child:makeWikipediaLink() .. ": " .. "<code>" .. child:getCode() .. "</code>")
	end
	
	return concat(list, "\n")
end

return export