Template:Langcode/doc

From OpenStreetMap Wiki
Jump to: navigation, search
Dialog-warning-orange.svg 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.

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 begining 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.

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:

|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|cprt|cyrl|cyrs|deva|dsrt|dupl|egyd|egyh
|egyp|elba|ethi|geok|geor|glag|goth|gran|grek|gujr|guru|hanb|hang|hani|hano|hans|hant|hatr
|hebr|hira|hluw|hmng|hrkt|hung|inds|ital|jamo|java|jpan|jurc|kali|kana|khar|khoj|khmr|kitl
|kits|knda|kore|kpel|kthi|lana|laoo|latf|latg|latn|leke|lepc|limb|lina|linb|lisu|loma|lyci
|lydi|mand|mani|marc|maya|mend|merc|mero|mlym|modi|mong|moon|mroo|mtei|mult|mymr|narb|nbat
|newa|nkgk|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|sidd|sinh|sora|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|wole|xpeo|xsux|yiii

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 page in main namespace
{{langcode
| namespace = Category
| pagename  = Lt:Main Page
}}
lt

lt

Category name with Lt: prefix in its name
{{langcode
| namespace = 
| pagename  = Pt-br:Main Page
}}
pt-br

pt-br

Two-dash-two-letter-language page in main namespace
{{langcode
| namespace = Category
| pagename  = Pt-br:Main Page
}}
pt-br

pt-br

Two-dash-two-letter-language page in Category namespace
{{langcode
| namespace = 
| pagename  = Zh-hans:Main Page
}}
zh-hans

zh-hans

Two-dash-four-letter-language page in main namespace
{{langcode
| namespace = 
| pagename  = Jbo:Main Page
}}
jbo

jbo

Three-letter-language page in main namespace
{{langcode
| namespace = 
| pagename  = Key:highway
}}
en

en

Three-letter page in main namespace, not a language page
{{langcode
| namespace = 
| pagename  = Tag:aerialway=cable_car
}}
en

en

Three-letter page in main namespace, not a language page

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

  • Template:Languages, for displaying an interlanguage navigation bar, in pages that are translatable.
  • Template: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).
  • Template: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).
  • Template:LangNS, returning the appropriate namespace number (for use in links to Special:PrefixIndex).