Difference between revisions of "Template:LangSwitch"
Template>Jarekt (Switch to Module:LangSwitch; allow multiple languages to map to a single value) |
m (1 revision imported) |
||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
| − | <includeonly>{{# | + | <!-- Documentation of code Snippet: |
| + | 1) "{{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}}" - is the chosen language, either set by parameter "lang=xx" | ||
| + | or by language of the user "{{int:Lang}}" | ||
| + | 2) "{{{ {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |}}}" - is the input parameter matching the language for example {{{xx|}}} | ||
| + | 3) Without the fallback templates the LangSwitch template would be | ||
| + | {{{ | ||
| + | {{#switch: {{{ {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |}}} // switch based on input parameter matching the language ie. xx | ||
| + | | ~ = empty // if |xx=~| than the template will return {{{empty}}} which is empty | ||
| + | | = {{#if: {{{default|}}} | default | en }} // if xx is undefined or empty |xx=| than return {{{default}}} or {{{en}}} | ||
| + | | #default = {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} // for all other cases return {{{xx|}}} | ||
| + | }} | ||
| + | |}}} | ||
| + | |||
| + | The actual template is bit more complicated calling templates {{GetFallback}} and {{GetFallback2}} to allow language fallback. For example if desired encoding is "sr-ec" (Serbian using Cyrillic alphabet) than the fall back would be "sr" (Serbian)) | ||
| + | -->{{{ | ||
| + | {{#switch: {{{ {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |}}} | ||
| + | | ~ = empty | ||
| + | | = {{#switch: {{{ {{GetFallback| {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |default=default}} |}}} | ||
| + | | ~ = empty | ||
| + | | = {{#switch: {{{ {{GetFallback2| {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |default=default}} |}}} | ||
| + | | ~ = empty | ||
| + | | = {{#if: {{{default|}}} | default | en }} | ||
| + | | #default = {{GetFallback2| {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |default=default}} | ||
| + | }} | ||
| + | | #default = {{GetFallback| {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} |default=default}} | ||
| + | }} | ||
| + | | #default = {{#if:{{{lang|}}}|{{{lang}}}|{{int:Lang}}}} | ||
| + | }} | ||
| + | |}}}<includeonly>{{#ifeq:{{NAMESPACE}}|File||{{#if:{{{en|}}}{{{default|}}}{{{nocat|<noinclude>1</noinclude>}}}||[[Category:LangSwitch template without English version]]}}}}</includeonly><noinclude> | ||
| − | |||
{{Documentation}} | {{Documentation}} | ||
</noinclude> | </noinclude> | ||
Latest revision as of 16:26, 13 September 2020
Provides a method to present a string of text selected from an array of many language translations in the language set in the user's preferences or the most appropiate fallback language (as determined by {{GetFallback}}).
Usage
{{LangSwitch
|en=
|xx=
|default=
}}
Parameters
| Parameter name | Description | Default value | required or optional?
|
|---|---|---|---|
| en | Text in English language. Unless default option is used, this text will also be used if the languages provided do not include the language set in the user's preferences. If you do not want to show any text when the user's language is English, then use "en=~". LangSwitch templates without an en option will be added to Category:LangSwitch template without English version. | empty | required |
| xx | Translated text in language xx (the parameter name xx can be any supported language code, and you can specify as many such parameters as needed for each translated language). Use "~" if you do not want any message (or default) in some specific language. | empty | optional |
| default | Default text in case one of the 'xx' languages is not the language of user’s preferences. By default, the English version will be shown, if provided. | {{{en|}}} (English version) | optional |
| lang | Language code (only needs to be provided if the language displayed is to be fixed and independent of the user’s preferences). This option is usually only used for template debugging. | Value of {{int:Lang}} (the preferred language of the visitor viewing the page)
|
optional |
| nocat | "nocat=1" will overwrite the default behavior of adding Category:LangSwitch template without English version to pages with no "en" and "default" options. | empty | optional |
Additional information
The template is intended to be used in the following namespaces: the Template namespace
The template is intended to be used by the following user groups: no user group specified
Relies on:
{{GetFallback}}, {{GetFallback2}}
See also
Examples
Here are a few examples showing template behavior for three languages: English, French and Corsu, which defaults to French if Corsu text is not available:
| Test | Code | lang=en | lang=fr | lang=co |
|---|---|---|---|---|
| No En; default | {{LangSwitch|fr=French|default=default}}
|
default | French | default |
| No En ; no default | {{LangSwitch|fr=French|co=Corsu}}
|
<ref name="c">adds [[Category:LangSwitch template without English version]]</ref> | French<ref name="c"/> | Corsu<ref name="c"/> |
| En; default | {{LangSwitch|en=English|co=Corsu|default=default}}
|
English | default | Corsu |
| En; no default | {{LangSwitch|en=English|co=Corsu}}
|
English | English | Corsu |
| Empty En; default | {{LangSwitch|en=~|fr=French|default=default}}
|
French | default | |
| Empty En; no default | {{LangSwitch|en=~|fr=French}}
|
French | ~ | |
| En; default; empty Fr | {{LangSwitch|en=English|fr=~|co=Corsu|default=default}}
|
English | Corsu | |
| En; default; empty Fr | {{LangSwitch|en=English|fr=|co=Corsu|default=default}}
|
English | default | Corsu |
| En; no default; empty Fr | {{LangSwitch|en=English|fr=~|co=Corsu}}
|
English | Corsu | |
| Simple case with "lang=" | {{LangSwitch|en=English|fr=French|co=Corsu|default=default|lang=}}
|
English | English | English |
<references/>
The default option is intended for providing more language independent option. For example:
{{LangSwitch
|de=[[:de:Warschau|Warschau]]
|en=[[:en:Warsaw|Warsaw]]
|fr=[[:fr:Varsovie|Varsovie]]
|ja=[[:ja:Warsaw|ワルシャワ]]
|pl=[[:pl:Warszawa|Warszawa]]
|default=[[Warsaw]]
}}
will provide link to Wikipedia articles in case one of the languages provided matches user's language but will link to Commons gallery page (Warsaw) if any other language is used.