Saltar para o conteúdo

Módulo:Mwlanguage

Origem: Wikipédia, a enciclopédia livre.
-- Wrapper for mw.language Lua functions
--   https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Language_library
--
-- Not implemented
--   mw.language.new( code )

local mwlanguage = {}

function table.ipairs(t)
  ret = {}
  for key, value in ipairs(t) do
    ret[key] = value
  end
  return ret
end

function boolean( str )
  -- Interpret string as boolean
  -- * Returns False if nil, "false" or "0"; returns True otherwise
  return not (str == nil or str == "false" or str == "0")
end

function mwlanguage.fetchLanguageName(frame)
  -- mw.language.fetchLanguageName( code, inLanguage )
  
  code = frame.args["code"] or frame.args[1]
  inLanguage = frame.args["inLanguage"] or frame.args[2]
  
  return mw.language.fetchLanguageName( code, inLanguage )  
end

function mwlanguage.fetchLanguageNames(frame)
  -- mw.language.fetchLanguageNames( inLanguage, include [, reverse, separator, sort] )
  
  inLanguage = frame.args["inLanguage"] or frame.args[1]
  include = frame.args["include"] or frame.args[2]
  reverse = boolean(frame.args["reverse"] or frame.args[3])
  separator = frame.args["separator"] or frame.args[4] or '|'
  sort = boolean(frame.args["sort"] or frame.args[5])
  
  ret = {}
  for key, value in pairs( mw.language.fetchLanguageNames( inLanguage, include ) ) do
    if reverse then
      str = value .. '=' .. key
    else
      str = key .. '=' .. value
    end
    table.insert( ret, str )
  end
  
  if sort then
  	table.sort( ret )
  end
  
  return table.concat( ret, separator )
end

function mwlanguage.getContentLanguage(frame)
  -- mw.language.getContentLanguage():getCode()
  
  return mwlanguage.getCode(frame)
end

function mwlanguage.getFallbacksFor(frame)
  -- mw.language.getFallbacksFor( code )
  
  code = frame.args["code"] or frame.args[1]
  
  return mw.language.getFallbacksFor( code )  
end

function mwlanguage.isKnownLanguageTag(frame)
  -- mw.language.isKnownLanguageTag( code )
  
  code = frame.args["code"] or frame.args[1]
  
  return tostring( mw.language.isKnownLanguageTag( code ) )
end

function mwlanguage.isSupportedLanguage(frame)
  -- mw.language.isSupportedLanguage( code )
  
  code = frame.args["code"] or frame.args[1]
  
  return tostring( mw.language.isSupportedLanguage( code ) )
end

function mwlanguage.isValidBuiltInCode(frame)
  -- mw.language.isValidBuiltInCode( code )
  
  code = frame.args["code"] or frame.args[1]
  
  return tostring( mw.language.isValidBuiltInCode( code ) )
end

function mwlanguage.isValidCode(frame)
  -- mw.language.isValidCode( code )
  
  code = frame.args["code"] or frame.args[1]
  
  return tostring( mw.language.isValidCode( code ) )
end

function mwlanguage.getCode(frame)
  -- mw.language:getCode
  lang = mw.language.getContentLanguage()
  return lang:getCode()
end

function mwlanguage.getFallbackLanguages(frame)
  -- mw.language:getFallbackLanguages
  lang = mw.language.getContentLanguage()
  return table.concat( lang:getFallbackLanguages(), '|' )
end

function mwlanguage.isRTL(frame)
  -- mw.language:isRTL
  lang = mw.language.getContentLanguage()
  return tostring( lang:isRTL() )
end

function mwlanguage.lc(frame)
  -- mw.language:lc( s )
  lang = mw.language.getContentLanguage()
  
  s = frame.args["s"] or frame.args[1]
  
  return lang:lc( s )
end

function mwlanguage.lcfirst(frame)
  -- mw.language:lcfirst( s )
  lang = mw.language.getContentLanguage()
  
  s = frame.args["s"] or frame.args[1]
  
  return lang:lcfirst( s )
end

function mwlanguage.uc(frame)
  -- mw.language:uc( s )
  lang = mw.language.getContentLanguage()
  
  s = frame.args["s"] or frame.args[1]
  
  return lang:uc( s )
end

function mwlanguage.ucfirst(frame)
  -- mw.language:ucfirst( s )
  lang = mw.language.getContentLanguage()
  
  s = frame.args["s"] or frame.args[1]
  
  return lang:ucfirst( s )
end

function mwlanguage.caseFold(frame)
  -- mw.language:caseFold( s )
  lang = mw.language.getContentLanguage()
  
  s = frame.args["s"] or frame.args[1]
  
  return lang:caseFold( s )  
end

function mwlanguage.formatNum(frame)
  -- mw.language:formatNum( n )
  lang = mw.language.getContentLanguage()
  
  n = frame.args["n"] or frame.args[1]
  
  return lang:formatNum( n )
end

function mwlanguage.formatDate(frame)
  -- mw.language:formatDate
  lang = mw.language.getContentLanguage()
  
  format_ = frame.args["format"] or frame.args[1]
  timestamp = frame.args["timestamp"] or frame.args[2]
  local_ = boolean(frame.args["local"] or frame.args[3])
  
  return lang:formatDate( format_, timestamp, local_ )
end

function mwlanguage.formatDuration(frame)
  -- mw.language:formatDuration( seconds, ... )
  lang = mw.language.getContentLanguage()
  
  seconds = frame.args["seconds"]
  allowedIntervals = table.ipairs( frame.args )
  
  return lang:formatDuration( seconds, allowedIntervals )
end

function mwlanguage.parseFormattedNumber(frame)
  -- mw.language:parseFormattedNumber( s )
  lang = mw.language.getContentLanguage()
  
  s = frame.args["s"] or frame.args[1]
  
  return lang:parseFormattedNumber( s )
end

function mwlanguage.convertPlural(frame)
  -- mw.language:convertPlural( n, ... )
  lang = mw.language.getContentLanguage()
  
  n = frame.args["n"]
  forms = table.ipairs( frame.args )
  
  return lang:convertPlural( n, forms )
end

function mwlanguage.plural(frame)
  -- mw.language:plural( n, ... )
  return mwlanguage.convertPlural( frame )
end

function mwlanguage.convertGrammar(frame)
  -- mw.language:convertGrammar( word, case )
  lang = mw.language.getContentLanguage()
  
  word = frame.args["word"] or frame.args[1]
  case = frame.args["case"] or frame.args[2]
  
  return  lang:convertGrammar( word, case )
end

function mwlanguage.grammar(frame)
  -- mw.language:grammar( case, word )
  lang = mw.language.getContentLanguage()
  
  case = frame.args["case"] or frame.args[1]
  word = frame.args["word"] or frame.args[2]
  
  return  lang:grammar( case, word )
end

function mwlanguage.gender(frame)
  -- mw.language:gender( what, masculine, feminine, neutral )
  lang = mw.language.getContentLanguage()
  
  what = frame.args["what"] or frame.args[1]
  masculine = frame.args["masculine"] or frame.args[2]
  feminine = frame.args["feminine"] or frame.args[3]
  neutral = frame.args["neutral"] or frame.args[4]
  
  return lang:gender( what, masculine, feminine, neutral )
end

function mwlanguage.getArrow(frame)
  -- mw.language:getArrow( direction )
  lang = mw.language.getContentLanguage()
  
  direction = frame.args["direction"] or frame.args[1]
  
  return lang:getArrow( direction )
end

function mwlanguage.getDir(frame)
  -- mw.language:getDir()
  lang = mw.language.getContentLanguage()  
  return lang:getDir()
end

function mwlanguage.getDirMark(frame)
  -- mw.language:getDirMark( opposite )
  lang = mw.language.getContentLanguage()
  
  opposite = frame.args["opposite"] or frame.args[1]
  
  return lang:getDirMark( opposite )
end

function mwlanguage.getDirMarkEntity(frame)
  -- mw.language:getDirMarkEntity( opposite )
  lang = mw.language.getContentLanguage()
  
  opposite = frame.args["opposite"] or frame.args[1]
  
  return lang:getDirMarkEntity( opposite )
end

function mwlanguage.getDurationIntervals(frame)
  -- mw.language:getDurationIntervals( seconds, allowedIntervals )
  lang = mw.language.getContentLanguage()
  
  seconds = frame.args["seconds"]
  allowedIntervals = table.ipairs( frame.args )
  
  return table.concat( lang:getDurationIntervals( seconds, allowedIntervals ), '|' )
end

return mwlanguage