Modul:Localized

From Star Citizen Wiki
Dokumentations-Unterseite Diese Dokumentationsseite spiegelt den Inhalt der Seite Modul:Localized/Doku wieder.

Modulabhängigkeiten

Modulinfo

Dieses Modul gibt die lokalisierte Beschreibung einer Seite aus Semantic Media Wiki aus

Öffentliche Methoden

Das Modul stellt folgende öffentliche Methoden bereit:

  • getMainTitle( title )
    • Gibt den Haupttitel einer Seite ohne Sprachcode aus
  • getDescription()
    • Gibt die in SMW gespeicherte Beschreibung aus, basierend auf der Seitensprache
  • isLanguagePage()
    • Gibt 'true' zurück, wenn die aktuelle Seite eine Übersetzungsseite ist

Tests

✔ Alle Tests bestanden.

Name Expected Actual
testGetMainTitle

local Localized = {}

local metatable = {}
local methodtable = {}

metatable.__index = methodtable


--- Returns the main title without a language code
--- E.g. Title 'Xyz Boots Grey/Black/en' returns 'Xyz Boots Grey/Black'
---
--- @param title string
function methodtable.getMainTitle( self, title )
    local titleparts = mw.text.split( title, '/' )
    local subpage = titleparts[#titleparts]

    -- We are on a language subpage
    if self.translation.checkLanguage( subpage, false ) ~= false then
        table.remove( titleparts, #titleparts )
    end

    return table.concat( titleparts, '/' )
end


--- Return the localized description from SMW
---
--- @return string
function methodtable.getDescription( self )
	local title = mw.title.getCurrentTitle()
    local page = self:getMainTitle( self.args[1] or self.args[ 'Titel' ] or title.fullText )

    local data = mw.smw.getQueryResult('[[' .. page .. ']]|?Beschreibung=desc|+lang=' .. self.translation._getLanguageSubpage( title ) )

    if type( data ) == 'table' and type( data[ 'results' ] ) == 'table' then
    	if type( data['results'][1] ) == 'table' then
        	return data['results'][1]['printouts']['desc'][1] or nil
    	end

    	return nil
    end

    return nil
end


--- Returns true if the current page is a language sub-page
---
--- @param title string
function methodtable.isLanguagePage( self, title )
    local titleparts = mw.text.split( title, '/' )
    local subpage = titleparts[#titleparts]

    return self.translation.checkLanguage( subpage, false ) ~= false
end

--- New Instance
--- Library entrance
function Localized.new( self, frame )
    local args = {}
    if frame then
        args = frame:getParent().args
    end

    local instance = {
        args = args,
        translation = require( 'Module:Template translation' )
    }

    setmetatable( instance, metatable )

    return instance
end


--- Template entry
function Localized.getDescription( frame )
    local instance = Localized:new()

    return instance:getDescription()
end


--- Returns the main title for the current page
function Localized.getMainTitle( frame )
    local instance = Localized:new()

    return instance:getMainTitle( mw.title.getCurrentTitle().fullText )
end


--- Returns true if the current page is a language sub-page
function Localized.isLanguagePage( frame )
    local instance = Localized:new()

    return instance:isLanguagePage( mw.title.getCurrentTitle().fullText )
end


return Localized
Cookies help us deliver our services. By using our services, you agree to our use of cookies.