This download file includes two versions of a language switcher. Supported languages can be defined in the templates.
Importing the template
The import file is included in the archive file HW-LanguageSwitcher.zip. Unpack the zip file first.
On the page Special:Import follow these steps:
- Select file and choose HW-LanguageSwitcher.xml.
- Enter an Interwiki prefix. Since this field is required, simply add hw (for hallowelt).
- Keep the selection Import to default locations.
- Click Upload file... .
The templates are now available in your wiki.
Pages included in the import
The xml import creates the following files in your wiki:
- Template:Languages
- Template:InterLanguages
- Template:Languages/styles.css
How it works
Template:Languages
This template is used to switch between a page and its translated versions when the translations are maintained as subpages.
The template defines some variables to determine which languages you want to support. It automatically determines the content language of your wiki installation. This variable "cl" is required and must not be deleted.
- You need to define at least one language for the switcher to make sense. Each language variable is named as lowercase l (for language) "l1", "l2",.... The value needs to be provided as the ISO 639-1 language code for the language. For Spanish, you would define:
{{#vardefine:l4|es}}
- After defining the variables, you need to add them to the actual switcher:
{{#switch: {{SUBPAGENAME}} |{{#var:l1}}={{#var:switcher}} |{{#var:l2}}={{#var:switcher}} |{{#var:l3}}={{#var:switcher}} |{{#var:l4}}={{#var:switcher}} |{{#var:clswitcher}}}}
- A tracking property enssures that the different languages are available for further processing through Semantic MediaWiki. This allows to create page lists of the different languages.:
<!-- set tracking property //--> [[Languages/tracklang::{{#switch: {{SUBPAGENAME}}|{{#var:l1}}={{#var:l1}}|{{#var:l2}}={{#var:l2}}|{{#var:l3}}={{#var:l3}}|{{#var:l4}}={{#var:l4}}|{{#var:cl}}}}]]
- Alternatively, especially in BluesSpice free (where no SMW functions and therefore no attributes are available by default), tracking categories can be used. These are commented out in the template by default. The comment must therefore be removed and all languages tracked as a category:
{{#switch: {{SUBPAGENAME}}|{{#var:l1}}=[[Category:{{#var:l1}}]]|{{#var:l2}}=[[Category:{{#var:l2}}]]|{{#var:l3}}=[[Category:{{#var:l3}}]]|{{#var:l4}}=[[Category:{{#var:l4}}]]|[[Category:{{#var:cl}}]]}}
- In addition, you should add the language to the template description for the extension TemplateData:
"description": { "en": "Language switcher", "de": "Sprachwechsler", "fr": "Sélecteur de langue", "nl": "Taal verandering", "es": "Cambiador de idioma" }
Template:InterLanguages
This template is used to switch between a page and its translated versions when the translations are not maintained as subpages, but are technically not associated at all.
The template defines some variables to determine which languages you want to support. The variable "self" defines the current page. It is required and must not be deleted.
The language variables are defined just like in the template Languages. If you define additional language variables, the newly defined languages has to be added to the language menu. The links are created in the following container:
<div id="langbar-hz">
...
</div>
Simply add each language with the l-number matching your language variable by copying this line and replacing the number:
{{#if: {{{pgnamel4|}}}|{{#switch:{{{pgnamel4|}}}|{{#var:self}}=[[{{FULLPAGENAME}}{{!}}{{#var:l4}}]]|[[{{{pgnamel4}}}{{!}}{{#var:l4}}]]}}|}}
In addition, you need to add the language to the template description and the available fields in the TemplateData editor.
In source editing mode:
- Click Manage TemplateData
- Click Add parameter
- Select any new language you have added and click Apply.
- Add the language abbreviation for the new language as a label and activate the Suggested checkbox and click Done.
- Click Add language to add a new template description for this language.
- Select the user language that corresponds to the current language parameter.
- Enter a template description and click Apply. This description is shown when the user selects a template in the visual editor.
Displaying the switcher on a page
For the language switcher to work, you need to create the language versions of a page as subpages of the actual content language, e.g. in an English wiki:
- Vacation policy
- Vacation policy/de
- Vacation policy/fr
The template is then added to each language version:
- Add the template Languages from the VisualEditor insert-dialog.
- Provide a display title if the page is a subpage. For example for the Vacation policy/de, enter "Urlaubsregelung":
Alignment of the language switcher
By default, the language switcher appears on the right side of the page. To display it on the left side, change the following line in the page Template:Languages/styles.css:
#langbar-hz {text-align: left;}