Build TYPO3 Language Menu without the need of optionSplit

TYPO3 CMS allows you to build a language menu to enable the frontend user to switch the current language. This menu is generated via TypoScript using optionSplit. Just start a query and take a look at the snippets. This way has one big drawback. In a multi domain setup you have to change the config

We have overcame this issue with one language menu working for all setup on all domains without the need to adjust anything. Read here how to achieve this.

The idea

The basic idea is to use the HMENU like all other solutions, but instead of using the optionSplit we are using data to inject the values from a language file.

The TypoScript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
tmp.language = HMENU
tmp.language {
    wrap = <ul>|</ul>

    special = language
    special {
        value = 0, 1, 2, 3
    }

    1 = TMENU
    1 {
        NO = 1
        NO {
            allWrap = <li>|</li>

            ATagTitle {
                data = LLL:EXT:example/Resources/Private/Language/Frontend.xlf:languageMenu.title.{field: _PAGES_OVERLAY_LANGUAGE}
                data {
                    insertData = 1
                }
            }

            stdWrap {
                cObject = COA
                cObject {
                    1 = TEXT
                    1 {
                        data < tmp.language.1.NO.ATagTitle.data
                    }
                }
            }
        }

        ACT < .NO
        ACT {
            allWrap = <li class="active">|</li>
        }

        USERDEF1 = 1
        USERDEF1 {
            doNotShowLink = 1
        }
    }
}

On Line 5 to 8 we define the menu as you normally would. With one exception, we add all sys_language_uid‘s, not only the one we want on the current site. Via NO all existing languages that are not currently active are rendered. With ACT the current active language is rendered and via USERDEF1 we define to not show links to languages which are not available for the current site, depending on your configuration.

By Using USERDEF1 we don’t have to adjust the set of languages for each page.

Inside of data, the field: _PAGES_OVERLAY_LANGUAGE contains the uid of the current sys_language to render.

Using the data we can render the content of a language file. This file can be different for each language. This way we can adjust the labels for each language depending on the language.

Note

The language configuration is needed in addition. But as this is documented anywhere else, it’s not part of this post. This post just covers the menu generation.

Take a look at Frontend Localization Guide.

The language file

The language file for above example might look like the following.

Note

You have to provide the id languageMenu.title. for our example, as default language 0 will not have a number in this menu. See Line 5.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?xml version="1.0"?>
<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file original="en" datatype="plaintext">
        <body>
            <trans-unit id="languageMenu.title.">
                <source>Default</source>
            </trans-unit>
            <trans-unit id="languageMenu.title.1">
                <source>Deutsch</source>
            </trans-unit>
            <trans-unit id="languageMenu.title.2">
                <source>Nederlands</source>
            </trans-unit>
            <trans-unit id="languageMenu.title.3">
                <source>English</source>
            </trans-unit>
        </body>
    </file>
</xliff>

The result

The output will look like the following:

../../../_images/output.png

Example output

Credits

This solution was “invented” by Justus Moroni and myself during one project, as we thought that option split and adjusting the settings for each site is not the best way. Also we were just to lazy, you know programmer?, to adjust the configuration for each multisite.

Further reading

Here is just one example how it’s done through option split: https://typo3.org/documentation/snippets/sd/64/

And further resources to TYPO3 documentation which are used in this example: