Template:Langcode

From Second Life Wiki
Revision as of 12:02, 13 September 2022 by Gwyneth Llewelyn (talk | contribs) (Using a pre-Lua version from wiki.openstreetmap.org)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

en

[Edit] [🔄Purge] Template-info.png Template documentation

exclamation mark

Note: This template is used on a lot of pages. In order not to put too much load on the servers, edits should be kept to a bare minimum.
Please discuss proposed changes on the talk page first.
Editing a template causes all pages that use the template to be re-rendered. If the Template is used often, this can put a lot of load on the servers since it fills up the job queue. [statistics]

This template will autodetect the language code used in translated pages, by trying to match the supported language code prefixes (either in the dedicated namespaces used for a few languages, or at the beginning of the article name for most other languages).

It is intended to support the generation of templates whose language displayed will match the language defined by the translated page, and should not require administrator privileges to create/import new translations.

Usage

  • {{Langcode}}
  • There's no need for any parameter.

Caveats

exclamation mark

Note that most of the technical details explained below have a completely different Wiki in mind!

Technical details

This template detects (from the current page) the language code used before a colon (":") and the actual name of the page, in one of the two forms:

  • In a true namespace, only for 7 languages that have dedicated namespaces for articles on this wiki: "DE:", "ES:", "FR:", "IT:", "JA:", "NL:", "RU:" (and their associated talk namespaces).
  • As a prefix of the pagename in any other namespace, by looking if they match the following forms:
    • 2-letter prefixes before ":" are assumed to be language codes, e.g. "Cs:".
    • 3-letter prefixes before ":" are tested:
      • Known 3-letter English prefixes which are not language codes are in English: "Key:", "POI:", "Tag:", "Way:" (if needed this list is expansible for other known short English words used on this wiki).
      • Other 3-letter prefixes are assumed to be language codes, e.g. "Vec:".
    • 2-letter language code followed by "-" and 2-letter country/region code (deprecated), e.g. "En-gb:". Other 5-chars prefixes assumed to be part of English title.
    • 3-letter language code followed by "-" and 2-letter country/region code (deprecated), e.g. "Vec-it:". Other 6-chars prefixes assumed to be part of English title.
    • 2-letter language code followed by "-" and 4-letter lowercase script code (like arab,latn,cyrl,hans,hant), e.g. "Zh-hans:". Other 7-chars prefixes assumed to be part of English title.
    • 3-letter language code followed by "-" and 4-letter lowercase script code (like arab,latn,cyrl,hans,hant), e.g. "Tzm-tfng:". Other 8-chars prefixes assumed to be part of English title.

In all other cases, it returns "en", assuming English (the default language of this wiki).

This template returns the detected language as a standard language code always in lowercase, including for English ("en").

Ideally it should also test the capitalization of language code and country code prefixes, but it is not significant in the dedicated namespaces for 7 seven languages listed above, and there are still lot of articles making confusion about it (notably when initially creating articles), so this is not tested.

However standard ISO 15924 4-letter script codes (excluding private use codes starting by "q", and codes for special use starting by "z") are validated and accepted only in lowercase; this includes the following codes (last published on 2017-07-26):

|adlm|afak|aghb|ahom|arab|aran|armi|armn|avst|bali|bamu|bass|batk|beng|bhks|blis|bopo|brah
|brai|bugi|buhd|cakm|cans|cari|cham|cher|cirt|copt|cpmn|cprt|cyrl|cyrs|deva|dogr|dsrt|dupl
|egyd|egyh|egyp|elba|ethi|geok|geor|glag|gong|gonm|goth|gran|grek|gujr|guru|hanb|hang|hani
|hano|hans|hant|hatr|hebr|hira|hluw|hmng|hmnp|hrkt|hung|inds|ital|jamo|java|jpan|jurc|kali
|kana|khar|khmr|khoj|kitl|kits|knda|kore|kpel|kthi|lana|laoo|latf|latg|latn|leke|lepc|limb
|lina|linb|lisu|loma|lyci|lydi|mahj|maka|mand|mani|marc|maya|medf|mend|merc|mero|mlym|modi
|mong|moon|mroo|mtei|mult|mymr|narb|nbat|newa|nkdb|nkgb|nkoo|nshu|ogam|olck|orkh|orya|osge
|osma|palm|pauc|perm|phag|phli|phlp|phlv|phnx|piqd|plrd|prti|rjng|roro|runr|samr|sara|sarb
|saur|sgnw|shaw|shrd|shui|sidd|sind|sinh|sora|soyo|sund|sylo|syrc|syre|syrj|syrn|tagb|takr
|tale|talu|taml|tang|tavt|telu|teng|tfng|tglg|thaa|thai|tibt|tirh|ugar|vaii|visp|wara|wcho
|wole|xpeo|xsux|yiii|zanb

This template is used in lots of pages, and notably all those using the interlanguage navigation bar (most pages of this wiki should be translatable and should use it), or those that are using various other templates to localize them in the same language as used in the article.

Test cases

Test case Expected result Actual result Comment
{{Langcode
  | namespace =
  | pagename  = Main Page
}}
en en Standard page in main namespace
{{Langcode
  | namespace = Template
  | pagename  = Langcode
}}
en en Standard page in template namespace
{{Langcode
  | namespace = DE
  | pagename  = Hauptseite
}}
de de Standard page in DE namespace
{{Langcode
  | namespace = Category
  | pagename  = DE:Hauptseite
}}
de de Category name with DE: prefix in its name
{{Langcode
  | namespace =
  | pagename  = Lt:Main Page
}}
lt lt Two-letter language prefix in main namespace
{{Langcode
  | namespace = Category
  | pagename  = Lt:Main Page
}}
lt lt Two-letter language prefix in Category namespace
{{Langcode
  | namespace =
  | pagename  = PL:Main Page
}}
pl pl Two-letter language prefix in main namespace
{{Langcode
  | namespace = Category
  | pagename  = PL:Main Page
}}
pl pl Two-letter language prefix in Category namespace
{{Langcode
  | namespace =
  | pagename  = Pt-br:Main Page
}}
pt-br pt-br Two-letter-dash-two-letter language prefix in main namespace
{{Langcode
  | namespace = Category
  | pagename  = Pt-br:Main Page
}}
pt-br pt-br Two-letter-dash-two-letter language prefix in Category namespace
{{Langcode
  | namespace =
  | pagename  = Zh-hans:Main Page
}}
zh-hans zh-hans Two-letter-dash-four-letter language prefix in main namespace
{{Langcode
  | namespace =
  | pagename  = Jbo:Main Page
}}
jbo jbo Three-letter language prefix in main namespace
{{Langcode
  | namespace =
  | pagename  = Key:highway
}}
en en Three-letter prefix in main namespace, not a language code
{{Langcode
  | namespace = 
  | pagename  = Tag:aerialway=cable_car
}}
en en Three-letter prefix in main namespace, not a language code

Notes

  • The returned language code will not depend on the UI language selected by the user in his preferences, so the result will be a static code, which is not intended to translate the UI (such as "edit" or "purge", or "add a new section", or "talk"), but only the content in the same language as the translated page.
  • For translating the UI, you'll need to use {{int:resource name}} to look for translations of a named ressource in the (privileged) "Mediawiki:" namespace ; for example {{int:lang}} which currently returns "⧼lang⧽" : if you see "<lang>" instead of your UI language, there are missing resources in Mediawiki:Lang (this link will remain red, but should contain "en" for English, the default language of this wiki), and in its subpages (one per language supported by the Mediawiki UI).

See also

  • {{Languages}}, for displaying an interlanguage navigation bar, in pages that are translatable.
  • {{Uselang}}, for returning the language code of the UI (such as the "edit" text displayed in links for editing a section, or the language code used for adapting the lateral toolbars and menus).
  • {{Int}}, a variant of the MediaWiki parser function, used for returning some text of the translatable UI of Wikimedia but in another language than the default user language.
  • Mediawiki talk:Lang, for how and why to setup the Mediawiki:Lang resources and its subpages for adapting the UI (like on Wikimedia Commons).
  • {{LangNS}}, returning the appropriate namespace number (for use in links to Special:PrefixIndex).
  • {{LangPrefix}}, returning the appropriate namespace or prefix to use for linking to pages in a specific language (or in its associated fallback language).
  • {{LangSwitch}}, returning one of several translations available for a specific language (or its associated fallback language).