Åtgärder

Mall

Pagelang/doc

Från DAU-handboken

< Mall:Pagelang

{{#ifeq:doc |doc

    | 

{{#ifeq:show |show

         | Skriptfel: Modulen "Message box" finns inte.
        }}{{#if: |
         |   {{#ifexist:Mall:Pagelang
                  | [[Category:{{#switch:Mall |Template=Template |Module=Module |User=User |#default=Wikipedia}} documentation pages]]
                  |
                 }}
        }}
    | 
   }}

Usage

<templatedata> { "description": "This stuff template for check language of translated subpage (for translatable pages). Used for automate substitution localized values that will match the language of the current page. Also used to help defining text direction (with using with {{#switch:{{#if:||{{#if:||⧼lang⧽}}}} |aeb-arab|ar|arc|arz|azb|bcc|bgn|bqi|ckb|dv|en-rtl|fa|fa-af|glk|he|khw|kk-arab|kk-cn|ks|ks-arab|ku-arab|lki|lrc|luz|mzn|pnb|prd|ps|sd|sdh|skr-arab|ug|ug-arab|ur|ydd|yi ={{#if:||{{#if:||rtl}}}} |#default ={{#if:||{{#if:||ltr}}}} }}). It is designed to be called from other templates.", "format": "inline", "params": { "1": { "label": "page title", "description": "Page title to get language for. By default the title of the current page.", "type": "wiki-page-name" } } } </templatedata>

Examples of common use in translated pages or templates:

<source lang="html5"> <span lang="Mall:BCP47" dir="{{#switch:{{#if:||{{#if:{{#ifeq:skriptfel: modulen "template translation" finns inte.|en |{{#ifeq:{{#titleparts:pagelang/doc||-1}}|en

 |skriptfel: modulen "template translation" finns inte.
 }}

|skriptfel: modulen "template translation" finns inte. }}|{{#ifeq:skriptfel: modulen "template translation" finns inte.|en |{{#ifeq:{{#titleparts:pagelang/doc||-1}}|en

 |skriptfel: modulen "template translation" finns inte.
 }}

|skriptfel: modulen "template translation" finns inte. }}|⧼lang⧽}}}} |aeb-arab|ar|arc|arz|azb|bcc|bgn|bqi|ckb|dv|en-rtl|fa|fa-af|glk|he|khw|kk-arab|kk-cn|ks|ks-arab|ku-arab|lki|lrc|luz|mzn|pnb|prd|ps|sd|sdh|skr-arab|ug|ug-arab|ur|ydd|yi ={{#if:||{{#if:||rtl}}}} |#default ={{#if:||{{#if:||ltr}}}} }}" class="autonym">{{#language:Mall:1|{{#ifeq:Skriptfel: Modulen "Template translation" finns inte.|en |{{#ifeq:{{#titleparts:Pagelang/doc||-1}}|en

 |Skriptfel: Modulen "Template translation" finns inte.
 }}

|Skriptfel: Modulen "Template translation" finns inte. }}

</source>

Technical note

When a subpage page contains any (single or double) quotes or ampersands, it is not a valid language code; PAGENAME and SUBPAGENAME normally HTML-encodes these characters in their return value, so they are safe to use as input of #language. But #titleparts restores these quotes or ampersands by HTML-decoding them.

But then #language will produce a fatal server error when it is used with "language codes" with single quotes

Proof

This still works: <source lang="html5">

 français

</source>

This does not work either: <source lang="html5">

 français

</source>

Nor does this: <source lang="html5">

 français

</source> This is a critical bug of the #language parser function, and apparently a severe security issues that could be caused by some clever insertion of code in PHP after the quote.

This bug only occurs in the second parameter of #language (the target language into which to give the name of the language specified by the code in the first parameter). Only the first parameter is checked for validity, but not the second one. Here, where #language cannot find the name of the language, in any target language, it should return the native language name, as in:

<source lang="html5">

 français

</source>

français
Word-around used in this template

To avoid this severe bug, we want to detect these damned quotes (or ampersands) in subpage names, as they are invalid anyway in any language code, in order to return an empty string and not the subpage name: such subpage name cannot be a language code anyway.

One way to detect subpages that cannot be safe language codes is to check if their value filtered by #titleparts is equal to their value filtered by PAGENAME: if they aren't equal that's because they contained ampersands, or quotes.

Why we use titleparts and not just SUBPAGENAME ?

Titleparts is used in this template instead of using SUBPAGENAME, but using SUBPAGENAME would not avoid the bug either, because it could as well return the full page name containing the damned quotes ! (SUBPAGENAME only works in namespaces where subpages have been activated (so it does not work in the main namespace to see if it has been translated using a language code suffix).

See also