v0.0.2
Demos (demo panel, opens in a new tab)

Language switcher

The navbar's language switcher: the button shows the current language's flag and code, the panel lists the store's languages.

zb:shop.language-switcher zubazar/ui-pro

PRO

This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →

The store's language switcher

The store's language switcher: view
BLADE

The languages come from the store's languages() method; a store with one language draws no switcher.

blade
<zb:shop.language-switcher />

Languages and flags

PHP

languages() gives the languages as code => name, and language_flags maps the flags whose country code differs from the language code (gb for en).

php
// The store class (Storefront)
public function languages(): array
{
    return ['tr' => 'Türkçe', 'en' => 'English'];
}

// config/zubazar-ui-pro.php (commerce)
'language_flags' => ['en' => 'gb'],
PropValues
languages()Code => name; with fewer than two languages no switcher is drawn
language_flagsLanguage code => the flag's country code

General language switcher

General language switcher: view
BLADE

zb:locale-switcher is not tied to the store: it takes the languages, the address and the flags as props.

blade
<zb:locale-switcher
    :action="route('locale')"
    :locales="['tr' => 'Türkçe', 'en' => 'English', 'de' => 'Deutsch']"
    :flags="['en' => 'gb']"
    align="start"
/>

Without flags

Without flags: view
BLADE

:flags="false" removes the flags; the button keeps a language icon and the language code.

blade
<zb:locale-switcher :action="route('locale')" :locales="['tr' => 'Türkçe', 'en' => 'English']" :flags="false" align="start" />

Props

The values the component takes. What are props and slots?

PropTypeDefaultDescription
align—endThe panel's alignment: end · start (both components)
locales—[]zb:locale-switcher: the languages, code => name in that language
action—nullzb:locale-switcher: where the choice is posted (POST, field locale); without it nothing is drawn
flags—[]zb:locale-switcher: language code => country code, not needed when they match; false: no flags
current—nullzb:locale-switcher: the current language; null: app()->getLocale()

Developer notes

Events, hooks and details
  • zb:shop.language-switcher uses the store's languages; the general zb:locale-switcher under it takes the languages and the address as props.
  • When a language is picked, the form posts the locale field to the locale route; the application keeps the choice (a cookie, the account) and redirects back.
  • The application defines the locale route (Shop::ROUTES); the first preview shows a store with two languages.
  • The general switcher is not drawn either without action or with one language; align="start" aligns the panel with the button's left edge.
  • For a language with no flag (an unknown country code) the button shows the language icon.
Zubazar UI · ZubazarDesigned for people.