Utilizaire:Guilhelma/Guilhelma Mòdul:essai

local p = {}

local function formWithSingleGrammaticalFeature( lexeme, item_id )

      for i, form in pairs( lexeme:getForms() ) do
              local grammaticalFeatures = form:getGrammaticalFeatures()
              if #grammaticalFeatures == 1 and grammaticalFeatures[1] == item_id then
                      return form
              end
      end
      return nil

end

local function representationInLanguage( form, language_code )

      local representation, language = form:getRepresentation( language_code )
      if representation then
              return { representation, language }
      else
              return form:getRepresentations()[1]
      end

end

local function termSpan( term )

      local text = term[1]
      local lang = term[2]
      local dir = mw.language.new( lang ):getDir()
      local span = mw.html.create( 'span' )
      span:attr( 'lang', lang ) -- TODO T310581
              :attr( 'dir', dir )
              :wikitext( text )
      return tostring( span )

end

local function singularPlural( lexeme_id, item_id )

      local lexeme = mw.wikibase.getEntity( lexeme_id )
      local form = formWithSingleGrammaticalFeature( lexeme, item_id )
      if form == nil then
              return "no such form"
      end
      local representation = representationInLanguage( form, 'en' )
      return termSpan( representation )

end

function p.singular( frame )

      return singularPlural( frame.args[1], 'Q598521' )

end

function p.plural( frame )

      return singularPlural( frame.args[1], 'Q598522' )

end

return p