Difference between revisions of "Template:Langcode/doc"

From Second Life Wiki
Jump to navigation Jump to search
(Shamelessly copied from wiki.openstreetmap.org)
 
(Place warning about some things not corresponding directly to what wiki.openstreetmap.org has or had)
 
Line 2: Line 2:
{{Heavily used template}}
{{Heavily used template}}
<!-- PLEASE ADD CATEGORIES AND INTERWIKIS AT THE BOTTOM OF THIS PAGE -->
<!-- PLEASE ADD CATEGORIES AND INTERWIKIS AT THE BOTTOM OF THIS PAGE -->
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 ===
=== Usage ===
This template extracts the ISO 639 language identifier from the page name. Call simply as <tt><nowiki>{{langcode}}</nowiki></tt>. To keep compatibility with templates that use the <tt><nowiki>{{langcode|{{{lang|}}}}}</nowiki></tt> syntax this template returns its first argument if given. Note that this stops Mediawiki from caching the page, but this template is more optimised than older versions. Non-language prefixes such as Key:, Tag: and Relation: are handled correctly. Pages without a colon in the name are reported as the page content language set in the wiki (“{{int:pageinfo-language}}<!--use text in user’s interface language-->” under “{{int:pageinfo-toolboxlink}}”). This defaults to English for new pages. The page content language is always returned if it isn’t English.
* <code><nowiki>{{Langcode}}</nowiki></code>
* There's no need for any parameter.
 
=== Caveats ===
{{Wfmessage|type=advice|text=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 ===
{|class="wikitable"
|-
!scope="col"| Test case
!scope="col"| Expected result
!scope="col"| Actual result
!scope="col"| Comment
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Main Page
}}</pre>
| en
| {{Langcode
  | namespace =
  | pagename  = Main Page
}}
| Standard page in main namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace = Template
  | pagename  = Langcode
}}</pre>
| en
| {{Langcode
  | namespace = Template
  | pagename  = Langcode
}}
| Standard page in template namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace = DE
  | pagename  = Hauptseite
}}</pre>
| de
| {{Langcode
  | namespace = DE
  | pagename  = Hauptseite
  }}
| Standard page in DE namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace = Category
  | pagename  = DE:Hauptseite
}}</pre>
| de
| {{Langcode
  | namespace = Category
  | pagename  = DE:Hauptseite
}}
| Category name with DE: prefix in its name
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Lt:Main Page
}}</pre>
| lt
| {{Langcode
  | namespace =
  | pagename  = Lt:Main Page
}}
| Two-letter language prefix in main namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace = Category
  | pagename  = Lt:Main Page
}}</pre>
| lt
| {{Langcode
  | namespace = Category
  | pagename  = Lt:Main Page
}}
| Two-letter language prefix in Category namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = PL:Main Page
}}</pre>
| pl
| {{Langcode
  | namespace =
  | pagename  = PL:Main Page
}}
| Two-letter language prefix in main namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace = Category
  | pagename  = PL:Main Page
}}</pre>
| pl
| {{Langcode
  | namespace = Category
  | pagename  = Pl:Main Page
}}
| Two-letter language prefix in Category namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Pt-br:Main Page
}}</pre>
| pt-br
| {{Langcode
  | namespace =
  | pagename  = Pt-br:Main Page
}}
| Two-letter-dash-two-letter language prefix in main namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace = Category
  | pagename  = Pt-br:Main Page
}}</pre>
| pt-br
| {{Langcode
  | namespace = Category
  | pagename  = Pt-br:Main Page
}}
| Two-letter-dash-two-letter language prefix in Category namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Zh-hans:Main Page
}}</pre>
| zh-hans
| {{Langcode
  | namespace =
  | pagename  = Zh-hans:Main Page
}}
| Two-letter-dash-four-letter language prefix in main namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Jbo:Main Page
}}</pre>
| jbo
| {{Langcode
  | namespace =
  | pagename  = Jbo:Main Page
}}
| Three-letter language prefix in main namespace
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Key:highway
}}</pre>
| en
| {{Langcode
  | namespace =
  | pagename  = Key:highway
}}
| Three-letter prefix in main namespace, not a language code
|-
| <pre style="margin:0">{{Langcode
  | namespace =
  | pagename  = Tag:aerialway=cable_car
}}</pre>
| en
| {{Langcode
  | namespace =
  | pagename  = Tag:aerialway=cable_car
}}
| Three-letter prefix in main namespace, not a language code
|}


=== Implementation ===
=== Notes ===
The arguments to <tt><nowiki>#invoke</nowiki></tt> are pseudonamespace/language pairs, for instance <tt>Key=en</tt> means that pages with names beginning with Key: are in English. Prefixes such as Pt: directly corresponding to languages do not have to be added here because the Scribunto module returns the beginning of the page name in lower case if it doesn’t match any of the arguments.
* 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 <code><nowiki>{{</nowiki>int:''resource name''<nowiki>}}</nowiki></code> to look for translations of a named ressource in the (privileged) "Mediawiki:" namespace ; for example <code><nowiki>{{int:lang}}</nowiki></code> which currently returns "{{int:lang}}" : if you see "&lt;lang&gt;" 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 ===
=== See also ===
* {{T|LangSwitch}}
* {{T|Languages}}, for displaying an interlanguage navigation bar, in pages that are translatable.
* {{T|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).
* {{T|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).
* {{T|LangNS}}, returning the appropriate namespace number (for use in links to [[Special:PrefixIndex]]).
* {{T|LangPrefix}}, returning the appropriate namespace or prefix to use for linking to pages in a specific language (or in its associated fallback language).
* {{T|LangSwitch}}, returning one of several translations available for a specific language (or its associated fallback language).


{{DEFAULTSORT:{{PAGENAME}}}}
<includeonly>
<includeonly>
<!-- CATEGORIES AND INTERWIKIS HERE, THANKS -->
<!-- CATEGORIES AND INTERWIKIS HERE, THANKS -->
[[Category:Templates for translating|Langcode]]
[[Category:Template:Internationalisation]]
</includeonly>
</includeonly>

Latest revision as of 12:18, 13 September 2022

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