Module:languages

From Wiktionary, the free dictionary
Archived revision by Ruakh (talk | contribs) as of 23:35, 5 December 2013.
Jump to navigation Jump to search

This module is used to retrieve and manage the languages that can have Wiktionary entries, and the information associated with them. See Wiktionary:Languages for more information.

For the languages and language varieties that may be used in etymologies, see Module:etymology languages. For language families, which sometimes also appear in etymologies, see Module:families.

This module provides access to other modules. To access the information from within a template, see Module:languages/templates.

The information itself is stored in the various data modules that are subpages of this module. These modules should not be used directly by any other module, the data should only be accessed through the functions provided by this module.

Data submodules:

Extra data submodules (for less frequently used data):

Finding and retrieving languages

The module exports a number of functions that are used to find languages.

export.getLanguageForCode

function export.getLanguageForCode(code)

This function lacks documentation. Please add a description of its usages, inputs and outputs, or its difference from similar functions, or make it local to remove it from the function list.

Language objects

A Language object is returned from one of the functions above. It is a Lua representation of a language and the data associated with it. It has a number of methods that can be called on it, using the : syntax. For example:

local m_languages = require("Module:languages")
local lang = m_languages.getByCode("fr")
local name = lang:getCanonicalName()
-- "name" will now be "French"


Error function

See Module:languages/error.

Subpages

See also


-- Example usage:
--      local language = mLanguages.getLanguageForCode('fr')
--      language ~= nil                        -- true (because language-code was found)
--      language.canonicalName                 -- 'French'
--      language.removeDiacriticsEtc('moi ?')  -- 'moi '
--      language.rawData                       -- a table of raw data (e.g. for JSONification)
--      language.code                          -- 'fr'


local fields = {}

function fields.canonicalName(this)
    return this.rawData.names[1]
end

function fields.allNames(this)
    return this.rawData.names
end

function fields.removeDiacriticsEtc(this)
    return function (text)
        text = mw.ustring.gsub(text, "^[¿¡]", "")
        text = mw.ustring.gsub(text, "[؟?!;՛՜ ՞ ՟?!।॥။၊ः་།]$", "")
        if this.rawData.entry_name then
            for i, from in ipairs(this.rawData.entry_name.from) do
                local to = this.rawData.entry_name.to[i] or ""
                text = mw.ustring.gsub(text, from, to)
            end
        end
        return text
    end
end


local metatable = {}

function metatable.__index(datum, key)
    return fields[key] and fields[key](datum)
end


local function getRawLanguageDataForCode(code)
    local stable = mw.loadData("Module:languages/stable")[code]
    if stable then return stable end
    local len = string.len(code)
    if len <= 2 then
        return mw.loadData("Module:languages/data2")[code]
    elseif len == 3 then
        local pre = code:sub(1, 1)
        return mw.loadData("Module:languages/data3/" .. pre)[code]
    else
        return mw.loadData("Module:languages/datax")[code]
    end
end


local export = {}

function export.getLanguageForCode(code)
    local rawData = getRawLanguageDataForCode(code)
    if rawData then
        return setmetatable({ rawData = rawData, code = code }, metatable)
    else
        return nil
    end
end

return export