Simple Languagemenu with iso-code

Tips, Tricks & Workarounds [en] / [de]

Moderator: KB Moderators

Locked
olape
Posts: 3173
Joined: Fri Mar 13, 2015 8:47 am
Contact:

Simple Languagemenu with iso-code

Post by olape » Thu Oct 17, 2024 1:07 pm

A simple language menu. Output as iso code instead of flags.
A slimmed-down version of the original language menu.

This code insert to /cmsimple/userfuncs.php
These with a simple title: iso code,

Code: Select all

<?php

/**
 * Returns the language menu.
 *
 * A slimmed-down version of the original language menu.
 *
 * @return string HTML
 */
function simple_languagemenu() {

    global $pth, $cf, $sl;

    $r = XH_secondLanguages();
    array_unshift($r, $cf['language']['default']);

    $t = '<div class="simple_languagemenu">' . "\n";
    foreach ($r as $lang) {
        if($sl == $lang) {
            $t .= '<span>' . $sl . '</span>';
        } else {
            $url = $pth['folder']['base']
                 . ($lang == $cf['language']['default'] ? '' : $lang . '/');
            $t .= '<a title="' . $lang . '" href="' . $url . '">' . $lang . '</a>';
        }
    }
    $t .= '</div>' . "\n";

    return $t;
}
or this one with title: Language name from the configuration of XH.

Code: Select all

<?php

/**
 * Returns the language menu.
 *
 * A slimmed-down version of the original language menu.
 * With title from the config.php ($cf['language']['2nd_lang_names']).
 *
 * @return string HTML
 */
function simple_languagemenu() {

    global $pth, $cf, $sl;

    $r = XH_secondLanguages();
    array_unshift($r, $cf['language']['default']);

    $langName = [];
    $langNames = explode(';', $cf['language']['2nd_lang_names']);
    foreach ($langNames as $value) {
        $langName[substr($value, 0, 2)] = substr($value, 3);
    }

    $t = '<div class="simple_languagemenu">' . "\n";
    foreach ($r as $lang) {
        if($sl == $lang) {
            $t .= '<span>' . $sl . '</span>';
        } else {
            $url = $pth['folder']['base']
                 . ($lang == $cf['language']['default'] ? '' : $lang . '/');
            $title = isset($langName[$lang])
                ? $langName[$lang]
                : $lang;
            $t .= '<a title="' . $title . '" href="' . $url . '">' . $lang . '</a>';
        }
    }
    $t .= '</div>' . "\n";

    return $t;
}
Up to XH 1.7.6 you have to replace the call of the language menu in the template.

Code: Select all

<?=languagemenu();?>
to

Code: Select all

<?=simple_languagemenu();?>
As of XH 1.8, you only need to enter 'simple' under 'Languagemenu' in the XH configuration.
Changes to the template will no longer be necessary.

This menu can be visually customized with the .simple_languagemenu class.
i.e.

Code: Select all

.simple_languagemenu {
    display: flex;
}
.simple_languagemenu * {
    margin: 0;
    padding: 0;
}
.simple_languagemenu a {
    text-decoration: none !important;
}
.simple_languagemenu a:hover {
    background: rgba(0,0,0,.1);
}
.simple_languagemenu span {
    opacity: 0.33;
}
.simple_languagemenu span,
.simple_languagemenu a {
    font-size: 1.2em;
    line-height: normal;
    margin: 3px;
    padding: 0;
    border: 1px solid;
    border-radius: 2px;
    width: 1.75em;
    display: inline-block;
    text-align: center;
    text-transform: uppercase;
    color: inherit !important;
}
Gruß Olaf, Plugins for CMSimple_XH

Ich habe schon lange den Verdacht, dass so viele so eifrig auf Gender, Trans und Queer machen:
Weil sie für das Fachliche ganz einfach zu doof sind.

Locked