Wikipedia:Lua/Modul/Multilingual/en
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
[Quelltext bearbeiten]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 supported2
– 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 itemslang
– language of the answer, if not native*
– native (default)!
– current project- any valid code
shift
– capitalizationc
– capitalize alld
– downcase everythingf
– capitalize first item onlym
– downcase every first word in item only
link=1
– link itemsscream
– category title in case of errorsplit
– split pattern, if list expected; e.g.split=,
– otherwise1
is regarded as single itemseparator
– list separator, elsesplit
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 code2
– 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 IDlang
– 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 versionde
– 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 withData:
or to append.tab
2
– keyword of text element in tablealt
– (optional) fallback text if failure cannot be remedied (no table, no entry)
- Tables for internationalisation are supposed to match following condition:
- field: string, with keyword for text element
- 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
oren-GB
and that is not mentioned explicitly in list, base language (hereen
) will be tried. - If nothing matches and no list is provided, the project language (here
de
), at leasten
for English will be returned.
- wikibase
- Optimal translation of wikibase component
1
– entity ID2
– (optional)1
ordescriptions
; (default)0
orlabels
- failsafe
- Version management
The Failsafe interface is heading for version management of globally distributed Lua modules. It enables modules equipped with this interface to
- ensure, that a library module required by a template or another module available as local copy does support certain functionality, or complain if not.
- administrate global updating and linking of module codes via Wikidata.
The Failsafe interface is present both at template level and for direct Lua access.
The functions in detail are (not all supported yet completely by every library):
Value | Result | current |
---|---|---|
nothingfalse
|
local version ID | »2020-12-10« |
Minimal version | version ID required at least date in ISO format It will be compared whether the current local implementation matches this version or later.
|
|
wikidata
|
version ID of global upstream
|
»2020-12-10« |
item
|
ID of the Wikidata item
|
Q47541920
|
~
|
Corresponding version ID locally and registered at Wikidata
|
»« |
@
|
Is the current (module) page linked correctly with Wikidata item?
|
»Q47541920« |
The return value is in template programming empty or under Lua false , otherwis a non-empty string as described.
|
Examples (test page)
[Quelltext bearbeiten]A test page illustrates practical use.
Functions for Lua modules (API)
[Quelltext bearbeiten]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.legal
– true 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.
- available – table with mapping: language code → message text
- alt – string, 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 )
-
- access – string, with page identification on Commons
- at – string, with keyword
- alt – string, optional, with fallback text
- frame – if available
- Returns:
- string|nil|false, with selected message
- 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
- atleast
optional
nil or minimum version orwikidata
or~
for synchronisation
- atleast
- returns: Version ID as string or
false
If succeeding, the Multilingual.get*() return a string, the Multilingual.is*() true
; on failure false
.
Project configuration
[Quelltext bearbeiten]The local project may be configured optionally, see /config.
Global data
[Quelltext bearbeiten]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
[Quelltext bearbeiten]Follow the steps:
- 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.
- If possible keep the name
- Register this module at d:Q47541920.
- 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.
- You might want to create your own version (keeping root name) of:
- The following modules are required for some functionality:
- Ready.
- Consider translation of doc page.
Usage
[Quelltext bearbeiten]General library; no limitations.
Dependencies
[Quelltext bearbeiten]The following modules may be meaningful for full functionality: