Skip to main content

DHTMLX Booking. Localization example

Live example​

Booking ships with three locales, en, de, and cn. Each one is an object of calendar names, button labels, date formats, and booking strings. setLocale applies any of them to a widget that is already on the page. The demo cycles through all three, using a button as the trigger.

const labels = ["en", "de", "cn"];

let current = 0;

const widget = new booking.Booking("#root", {
	start: buildToday(),
    data: dataset,
});

function confirmCallback(ev){
    const confirm = ev.confirm;
    setTimeout(() => {
        Math.random() < 0.5 ? confirm.error() : confirm.done();
    }, 1000);
}

widget.setConfirmHandler(confirmCallback);

function changeLocale() {
    current = ++current % labels.length;
    const lang = labels[current];
    const locale = booking.locales[lang];
    widget.setLocale(locale);
    widget.setConfirmHandler(confirmCallback);

    document.querySelector("#label").innerHTML = lang.toUpperCase();
}
<!-- custom styles -->
<link rel="stylesheet" href="https://snippet.dhtmlx.com/codebase/assets/css/auxiliary_controls.css">

<div class="g-wrap" style="height: 100%;">
    <!-- Locale selector -->

    <div class="dhx_sample-controls">
        <span class="dhx_sample-label" id="label">EN</span>
        <button class="dhx_sample-btn dhx_sample-btn--cta" id="change_locale" onclick="changeLocale()">Change locale</button>
    </div>

    <!-- component container -->
    <div id="root" style="height:calc(100% - 60px)"></div>
</div>
<!-- dataset -->

<script>
function buildToday(){const params = new URLSearchParams(typeof location !== 'undefined' ? location.search : '');const fixed = params.get('fixedDate') || (typeof window !== 'undefined' && window.FIXED_DATE);return fixed ? new Date(fixed) : new Date();}

	const dataset = [
      {
        id: "ee828b5d-a034-420c-889b-978840015d6a",
        title: "Natalie Tyson",
        category: "Therapist",
        subtitle: "2 years of experience",
        details: "Cleveland Clinic\n9500 Euclid Ave",
        preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/01.jpg",
        price: "$35",
        review: {
            stars: 4,
            count: 120,
        },
        slots: [
            {
                from: 9,
                to: 20,
                days: [1, 2, 3, 4, 5],
            },
            {
                from: 10,
                to: 18,
                days: [6, 0],
            },
        ],
    },
      {
        id: "5c9b64ad-1830-4e5b-a5f9-8acea10706df",
        title: "James Anderson",
        category: "Allergist",
        subtitle: "3 years of experience",
        details: "UCLA Medical Center\n57 Westwood Plaza",
        preview: "https://snippet.dhtmlx.com/codebase/data/booking/01/img/11.jpg",
        price: "$30",
        review: {
            stars: 4,
            count: 64,
        },
        slotSize: 45,
        slotGap: 10,
        slots: [
            {
                from: "9:15",
                to: 17,
                days: [1, 2, 3, 4, 5],
            },
        ],
    },
      // ... 44 more items (see Live Editor for full data)
    ];

function getDate(addDays, hoursValue = 0, minutesValue = 0) {
	const date = buildToday();
	const secondsValue = 0; // round to minutes
	const msValue = 0;

	date.setDate(date.getDate() + addDays);
	date.setHours(hoursValue, minutesValue, secondsValue, msValue);

	return date.getTime();
}
</script>

Use case​

A booking page carries more words than it looks like. The search bar, the day names, the slot messages, the dialog fields, and the validation text under them all come from the locale. An internal tool shared by offices in two countries shows all of that to both. A company that resells the screen to its own customers has to ship it in their language. Because a locale is one object, a translation is a file the customer can hand back to you. Language is also a runtime choice here, so a profile setting can apply another locale to the widget that is already on the page.

How to do this in your app​

  • Set the language at start: pass a built-in object to the constructor, as in locale: booking.locales.de.
  • Switch it later: call widget.setLocale(booking.locales["cn"]) with the object you want next.
  • Return to English: call setLocale() with no argument, or setLocale(null).
  • Add a language of your own: copy the English object, keep its keys, and translate the string values. booking.textSearchPlaceholder is a function the search field calls with the filter labels. Keep the function and translate the sentence inside it, or replace it with a string. Pass your copy through the same property or method.

Good to know​

  • The switch rebuilds the widget: setLocale applies the new locale by rebuilding, so the open day, the selected slot, and the search text are cleared. If the page has handlers, set them again after the call.
  • The filter date and the slot dates use different keys: formats.dateFormat prints the date in the filter. Chinese uses "%Y-%m-%d", and English and German use "%d.%m.%Y". Slot headers use booking.monthDayFormat, which is "%M %d" in all three locales, so those labels change through the month and day names in calendar.
  • The clock stays 24 hour until you change it: formats.timeFormat is "%H:%i" in all three locales. The time format example shows how to switch that string to a 12 hour clock.
  • The first day of the week is part of it: calendar.weekStart is 7 (Sunday) in the English and Chinese locales and 1 (Monday) in the German one, which is why the German day picker starts on a different column.
  • A custom locale needs every key: the object has calendar, core, formats, lang, and booking. Those groups cover the day names, the core buttons, the search bar, the slot messages, and the booking dialog, so start from the English one instead of writing it from scratch.

FAQ​

How do you change the language of a booking widget?​

Pass a locale object as locale at initialization, or call setLocale(locale) on a running widget. Both take the same object, and booking.locales holds the built-in ones.

Which languages does DHTMLX Booking include?​

English, German, and Chinese, available as booking.locales.en, booking.locales.de, and booking.locales.cn. English is applied when no locale is set.

How do you translate the widget into a language that is not included?​

Copy the English locale object, replace the values with your translations, and pass the result as locale or to setLocale(). Keep the keys as they are, since the widget looks its labels up by key.

API reference​

Additional resources​