Zum Inhalt springen

Wikipedia:Lua/Modul/Multilingual/en

aus Wikipedia, der freien Enzyklopädie
Dies ist eine alte Version dieser Seite, zuletzt bearbeitet am 13. November 2019 um 17:24 Uhr durch PerfektesChaos (Diskussion | Beiträge) (+). Sie kann sich erheblich von der aktuellen Version unterscheiden.
Vorlagenprogrammierung Diskussionen Lua Test Unterseiten
Modul Deutsch English

Modul: Dokumentation

Multilingual – Module with functions in context of languages, language codes, language names.

Functions for templates

All functions expect one unnamed parameter 1 with the key information, and sometimes more optionals. Whitespace ahead and after any content is ignored. Upcasing of language code segments like in en-US does not matter; results are downcased.

The return value is an empty string (“nothing”), if the parameter value does not fulfil the expectations. If there is a result or the query condition is true, at least one visible character will be returned. The result does not begin or end with a space.

fair
Format language code according to RFC 5646 and check validity
  • Result: empty, if invalid
fallback
Is another language suitable as replacement?
  • 1 – language version specifier to be supported
  • 2 – language specifier of a possible replacement
findCode
Retrieve code of language name in local (current project) language.
A code itself will be identified, too.
fix
Fix frequently mistaken language code
  • 1 – presumable language code
  • Result: string with correction, or empty
format
Format one or more languages.
  • 1 – language list or single item
  • slang – language of the answer, if not native
    • * – native (default)
    • ! – current project
    • any valid code
  • shift – capitalization
    • c – capitalize all
    • d – downcase everything
    • f – capitalize first item only
    • m – downcase every first word in item only
  • link=1 – link items
  • scream – category title in case of error
  • split – split pattern, if list expected; e.g. split=, – otherwise 1 is regarded as single item
  • separator – list separator, else split
  • start – prepend first list element, if any
getBase
Retrieve base language from possibly combined ISO language code.
getName
Which name is assigned to this language code?
  • 2 – language of the answer
    • * – in that language itself (default)
    • ! – in project language
    • Any ISO code.
int
Translated system message
  • 1 – message ID
  • lang – language code
  • $1, $2, … $9 – parameters
isLang
Could this be an ISO language code?
  • nothing – if not
isLangWiki
Could this be a Wiki language version?
  • nothing – if not
message
Show text in best match of user language like system message
  • en – English text version
  • de – German text version
  • – any further text version
  • $1 – parameter value for replacement
  • $2 – parameter value for replacement
  • – …
  • $9 – parameter value for replacement
sitelink
Make link at local or other site with optimal linktext translation using Wikidata
  • 1 – item ID
userLang
Try to support user language by application.
  • 1 – space separated list of available ISO 639 codes
  • Result:
    • If the current user language is not a list element, the first element is used.
    • If the current user language is a variant like en-US or en-GB and that is not mentioned explicitly in list, base language (here en) will be tried.
    • If nothing matches and no list is provided, the project language (here de), at least en for English will be returned.
wikibase
Optimal translation of wikibase component
  • 1 – entity ID
  • 2(optional) 1 or descriptions; (default) 0 or labels
failsafe
Version ID: 2025-03-05
optional parameter 1 – required version
result: empty, if requirement not met

Examples (test page)

A test page illustrates practical use.

Functions for Lua modules (API)

All functions described above can be used by other modules:

local lucky, Multilingual = pcall( require, "Module:Multilingual" )
if type( Multilingual ) == "table" then
    Multilingual = Multilingual.Multilingual()
else
    -- failure; Multilingual is the error message
    return "<span class='error'>" .. Multilingual .. "</span>"
end

Subsequently there are available:

Multilingual.fair( ask )
  • ask – string, or table according to getLang()
Multilingual.fallback( able, another )
Multilingual.findCode( ask )
Multilingual.fix( ask )
Multilingual.format( apply, alien, alter, active, alert, frame, assembly, adjacent )
  • apply – string with language list or single item
  • alien – language of the answer
    • nil false "*" – native
    • "!" – current project
    • any valid code
  • alter – capitalization
    • "c" – capitalize
    • "d" – downcase everything
    • "f" – capitalize first item only, downcase anything else
  • active – link items, if true
  • alert – string with category title in case of error
  • frame – if available
  • assembly – string with split pattern, if list expected
  • adjacent – string with list separator, else assembly
  • ahead – string for optional prepending first element, if any
Multilingual.getBase( ask )
Multilingual.getLang( ask )
Split language code into components
Returns: table
.base – Basic language (2–3 lowercase letters)
.region – Country (2 uppercase letters)
.script – Scripting (4 letters, capitalized)
.year – year (4 digits)
.extension – Extension (1 lowercase letter)
.other – More
.legaltrue if valid
.n – Number of components
Multilingual.getName( ask, alien )
  • ask – language code
  • alien – language of the answer
    • nil false "*" – native
    • "!" – current project
    • Any ISO code.
Multilingual.i18n( available, alt, frame )
Internationalised text (I18N) in best language.
  • availabletable with mapping: language code → message text
  • altstring, optional, with message text if table fails
  • frame – if available
Multilingual.int( access, alien, apply )
Multilingual.isLang( ask )
Multilingual.isLangWiki( ask )
Multilingual.message( arglist, frame )
  • arglist – table like template parameters
  • frame – if available
Multilingual.sitelink( all, frame )
  • all – string or table or number, item ID or entity
  • frame – if available
Multilingual.userLang( accept, frame )
  • accept – string with space separated list of available ISO 639 codes
  • frame – if available
Multilingual.wikibase( all, about, attempt, frame )
  • all – string or table or number, item ID or entity
  • about – boolean, true "descriptions" or false "labels"
  • attempt – string or not, code of preferred language
  • frame – if available
Multilingual.failsafe( atleast )
  • atleast
    optional
    nil or required version
  • Returns: string or false

If succeeding, the Multilingual.get*() return a string, the Multilingual.is*() true; on failure false.

Installation on other WMF projects

Follow the steps:

  1. Copy main module Module:Multilingual into your project.
    • If possible keep the name Multilingual.
    • If another name is required due to conflict or naming convention or non-latin script then choose a different one.
  2. Register this module at d:Q47541920.
  3. To adapt central translations, you may copy the following sub module, and keep the chosen root name:
    • /names
    • Adaption to local language and project articles may be needed.
    • By Wikidata one day more sitelinks and translated names might become available.
  4. You might want to create your own version (keeping root name) of:
  5. The following modules are required for some functionality:
  6. Ready.
  7. Consider translation of doc page.

Usage

General library; no limitations.

Dependencies