Wikiup:Lua/Modul/Multilingual/en

aus Wikipedia, der freien Enzyklopädie
< Wikiup:Lua‎ | Modul‎ | Multilingual
Dies ist die aktuelle Version dieser Seite, zuletzt bearbeitet am 9. Januar 2021 um 17:52 Uhr durch imported>PerfektesChaos(310926) (fix).
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
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

For language code 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.
  • 1 – language code
  • 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
  • Result: string with replacement code, or empty
findCode
Retrieve code of language name in local (current project) language.
A code itself will be identified, too.
  • 1 – language name in local (current project) language
  • Result: string with code, or empty
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
  • Result: string with formatted language names, or empty
getBase
Retrieve base language from possibly combined ISO language code.
  • 1 – language code
  • Result: code of base language, or empty if invalid query
getName
Which name is assigned to this language code?
  • 1 – language code
  • 2 – language of the answer
    • * – in that language itself (default)
    • ! – in project language
    • Any ISO code.
  • Result: name of language, or empty if unknown
int
Translated system message
  • 1 – message ID
  • lang – language code
  • $1, $2, … $9 – parameters
isLang
Could this be an ISO language code?
  • 1 – language code
  • nothing – if not
isLangWiki
Could this be a Wiki language version?
  • 1 – language code
  • 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
  • Result: internal link (within WMF), or empty if no sitelinks at all
tabData
Try to retrieve translated keyword from commons:Data:****.tab tables and present in best current language
  • 1 – page identification on Commons; no need to prefix with Data: or to append .tab
  • 2 – keyword of text element in table
  • alt – (optional) fallback text if failure cannot be remedied (no table, no entry)
Tables for internationalisation are supposed to match following condition:
  1. field: string, with keyword for text element
  2. field: localized, with multilingual text fragments
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 management

{{Wikipedia:Lua/Modul-Failsafe/en|Multilingual}}

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.tabData( access, at, alt, frame )
  • accessstring, with page identification on Commons
  • atstring, with keyword
  • altstring, optional, with fallback text
  • frame – if available
  • Returns:
    1. string|nil|false, with selected message
    2. string, with language code, or "error"
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 )
Versioning interface
  1. atleast
    optional
    nil or minimum version or wikidata or ~ for synchronisation
returns: Version ID as string or false

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

Project configuration

The local project may be configured optionally, see /config.

Global data

Following pages may be evaluated:

commons:Data:ISO639.tab
List of individual code tables on commons:Data:
commons:Data:ISO639/correction.tab
Has this code a well known misinterpretation?
commons:Data:ISO639/fallback.tab
Could this language be replaced by a widely understood one?
mw.language.getFallbacksFor() will be evaluated first.

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

The following modules may be meaningful for full functionality: