Ce module permet de récupérer les informations des différentes langues définies dans Module:langues/data, notamment leur nom, à partir de leur code et vice-versa.
getName(code, allowSpecial)
Renvoie le nom d’une langue à partir de son code. Si aucune langue ne correspond, la fonction renvoie nil
.
code
(string
) : Le code de la langue.allowSpecial
(boolean
) : Si true, les codes de langue spéciaux seront pris en compte.string
ou nil
getSortKey(code, allowSpecial)
Renvoie la clé de tri d’une langue à partir de son code. Le nom est retourné si la clé n’est pas définie. Si aucune langue ne correspond, la fonction renvoie nil
.
code
(string
) : Le code de la langue.allowSpecial
(boolean
) : Si true, les codes de langue spéciaux seront pris en compte.string
ou nil
getLanguageCode(name, allowSpecial)
Renvoie le code d’une langue à partir de son nom. Si aucune langue ne correspond, la fonction renvoie nil
. Voir la doc dans le code pour plus de précisions sur l’algorithme de recherche.
name
(string
) : Le nom de la langue.allowSpecial
(boolean
) : Si true, les codes de langue spéciaux seront pris en compte.string
ou nil
getWikimediaCode(code)
Renvoie le code de langue Wikimedia correspondant au code de langue local. S’il n'y a pas de code spécial Wikimedia, la fonction renvoie nil
.
code
(string
) : Le code de la langue.string
ou nil
hasPortal(code)
Renvoie true ou false selon que le code langue est associé à une langue qui a un portail ou non.
1
(string
) : Le code de la langue.boolean
hasWiktionary(code)
Renvoie true ou false selon que le code langue est associé à une langue qui possède un Wiktionnaire.
1
(string
) : Le code de la langue.boolean
languageName
Retourne le nom de la langue ou, à défaut, une chaine vide. Veuillez utiliser le modèle {{nom langue}}
plutôt que d’appeler cette fonction directement.
1
(string
, optionnel) : Le code de la langue.string
languageSortKey
Retourne la clé de tri d’une langue, à défaut, une chaine vide. Veuillez utiliser le modèle {{clé langue}}
plutôt que d’appeler cette fonction directement.
1
(string
, optionnel) : Le code de la langue.string
languageCode
Retourne le code d’une langue à partir de son nom, à défaut, une chaine vide. Veuillez utiliser le modèle {{code langue}}
plutôt que d’appeler cette fonction directement.
1
(string
, optionnel) : Le nom de la langue.string
languageNameForList
Retourne le nom de la langue avec la première letter en majuscule, à défaut, une chaine vide. Cette fonction est utilisée par le modèle {{L}}
.
1
(string
) : Le code de la langue.string
wikimediaCode
Renvoie le code de langue Wikimedia correspondant au code de langue local. S’il n'y a pas de code spécial Wikimedia, la fonction renvoie une chaine vide.
code
(string
, optionnel) : Le code de la langue.string
La documentation de ce module est générée par le modèle {{Documentation module}}.
Elle est incluse depuis la page Module:langues/Documentation. Veuillez placer les catégories sur cette page-là.
Les éditeurs peuvent travailler dans le bac à sable (créer).
Voir les statistiques d'appel depuis le wikicode sur l'outil wstat et les appels depuis d'autres modules.
local m_bases = require("Module:bases")
local m_params = require("Module:paramètres")
local languagesData = mw.loadData("Module:langues/data")
local p = {}
p.specialCodes = {
= "zh",
= "zh",
= "yue",
= "wuu",
= "ko",
= "vi",
= "vi",
= "vi",
= "nan",
= "nan",
= "nan",
}
--- Return the name of the language matching the given language code.
--- @param code string A language code.
--- @param allowSpecial boolean If true, codes marked as group or special also will be considered.
--- @return string|nil The matching language name, nil otherwise.
function p.getName(code, allowSpecial)
if not code or not languagesData or not allowSpecial and (languagesData.isSpecial or languagesData.isGroup) then
return nil
end
return languagesData.name
end
--- Return the sort key for the given language code.
--- @param code string A language code.
--- @param allowSpecial boolean If true, codes marked as group or special will also be considered.
--- @return string|nil The sort key for the language, nil if the code is invalid.
function p.getSortKey(code, allowSpecial)
if not code or not languagesData or not allowSpecial and (languagesData.isSpecial or languagesData.isGroup) then
return nil
end
return languagesData.sortKey or languagesData.name
end
--- Return the name of the given language. Available to templates.
--- @param frame frame
--- Parameters:
--- parent.args (string): Language code.
--- @return string The name of the language or an empty string if the code is invalid.
function p.languageName(frame)
local args = m_params.process(frame:getParent().args, {
= {},
})
return p.getName(args) or ""
end
--- Return the sort key for the given language code. Available to templates.
--- @param frame frame
--- Parameters:
--- args (string, optional): Language code.
--- @return string|nil The sort key for the language, nil if the code is invalid.
function p.languageSortKey(frame)
local args = m_params.process(frame:getParent().args, {
= {},
})
return p.getSortKey(args) or ""
end
--- Return the name of the given language with its first letter capitalized.
--- This function is used by the template {{L}}.
--- @param frame frame
--- Parameters:
--- parent.args (string, optional): A language code.
--- @return string The capitalized name of the language, an error message if none matched.
function p.languageNameForList(frame)
local args = m_params.process(frame:getParent().args, {
= {}
})
local code = args
if not code then
return '<span style="color: red">Code de langue manquant</span>' ..
m_bases.fait_categorie_contenu("Wiktionnaire:Codes langue manquants")
end
local languageName = p.getName(code, true)
if not languageName then
return mw.ustring.format('<span style="color: red">Code de langue inconnu : %s*</span>', code) ..
m_bases.fait_categorie_contenu("Wiktionnaire:Codes langue non définis")
end
return m_bases.ucfirst(languageName)
end
--- Return the Wikimedia language code for the given internal language code if it exists.
--- @param code string A language code.
--- @return string The corresponding Wikimedia language code, or nil if none matched.
function p.getWikimediaCode(code)
if not code or not languagesData then
return nil
end
return languagesData.wikimediaCode
end
--- Return the Wikimedia language code for the given internal language code if it exists.
--- @param frame frame
--- Parameters:
--- parent.args (string, optional): A language code.
--- @return string The corresponding Wikimedia language code, or an empty string if none matched.
function p.wikimediaCode(frame)
local args = m_params.process(frame:getParent().args, {
= {},
})
local code = args
return p.getWikimediaCode(code) or code
end
--- Check whether a page in the “Portail” namespace exists for the given language code.
--- @param code string A language code.
--- @return boolean True if a “Portail” page exists, false otherwise.
function p.hasPortal(code)
return languagesData ~= nil and languagesData.hasPortal
end
--- Check whether a Wiktionary exists for the given language code.
--- @param code string Le code de langue.
--- @return boolean True if a Wiktionary exists, false otherwise.
function p.hasWiktionary(code)
return languagesData ~= nil and languagesData.wiktionaryExists
end
--- Return the code corresponding to the given language name.
--- If there are more than one, keep the shortest one.
--- The function also takes code aliases into account (e.g. "anglo-saxon" for "vieil anglais").
--- Special case: if there exists a code that strictly equals a language name,
--- it will be returned even if a shorter code exists (e.g. "vieil écossais" vs "vieux scots"),
--- unless there exists a code with 3 characters or less (e.g. "créole guadeloupéen" vs "gcf"),
--- except if the language name is "normand".
--- @param languageName string A language name.
--- @param allowSpecial boolean If true, codes marked as group or special also will be considered.
--- @return string|nil The language’s code, or nil if none matched.
function p.getLanguageCode(languageName, allowSpecial)
if languageName == "normand" then
-- Special case: we prefer to return "normand" instead of shorter code "nrf"
return languageName
end
local result
for code, languageData in pairs(languagesData) do
if languageName == languageData.name and
(allowSpecial or not languageData.isGroup and not languageData.isSpecial) then
local codeLength = mw.ustring.len(code)
if result == nil or code == languageName or
codeLength < mw.ustring.len(result) and (result ~= languageName or codeLength <= 3)
then
result = code
end
end
end
-- If no match yet, consider the name as an alias and try again
if result == nil and languagesData and languagesData.name then
return p.getLanguageCode(languagesData.name)
end
return result
end
--- Return the code corresponding to the given language name.
--- If there are more than one, keep the shortest one.
--- The function also takes code aliases into account (e.g. "anglo-saxon" for "vieil anglais").
--- Special case: if there exists a code that strictly equals a language name,
--- it will be returned even if a shorter code exists (e.g. "vieil écossais" vs "vieux scots"),
--- unless there exists a code with 3 characters or less (e.g. "créole guadeloupéen" vs "gcf"),
--- except if the language name is "normand".
--- @param frame frame
--- Parameters:
--- parent.args (string, optional): A language name. Defaults to the current page’s title.
--- parent.args (boolean, optional): If true, codes marked as group or special also will be considered.
--- @return string The language’s code, or an empty string if none matched.
function p.languageCode(frame)
local args = m_params.process(frame:getParent().args, {
= { default = mw.title.getCurrentTitle().text },
= { type = m_params.BOOLEAN, default = false },
})
return p.getLanguageCode(args, args) or ""
end
return p