Skip to content
Guide

Localization

Customize labels and formats for international audiences

Custom Labels

Timepicker-UI allows you to customize all text labels to support any language:

Default Labels

index.tstypescript
1// Default English labels
2{
3 labels: {
4 ok: 'Ok',
5 cancel: 'Cancel',
6 am: 'AM',
7 pm: 'PM'
8 }
9}

Spanish Localization

index.tstypescript
1const picker = new TimepickerUI(input, {
2 labels: {
3 ok: 'Aceptar',
4 cancel: 'Cancelar',
5 am: 'AM',
6 pm: 'PM'
7 }
8});
9
10picker.create();

French Localization

index.tstypescript
1const picker = new TimepickerUI(input, {
2 labels: {
3 ok: 'Valider',
4 cancel: 'Annuler',
5 am: 'AM',
6 pm: 'PM'
7 }
8});
9
10picker.create();

German Localization

index.tstypescript
1const picker = new TimepickerUI(input, {
2 labels: {
3 ok: 'OK',
4 cancel: 'Abbrechen',
5 am: 'AM',
6 pm: 'PM'
7 }
8});
9
10picker.create();

Language Configuration Object

Create reusable language configuration objects for consistency:

Language Config Files

index.tstypescript
1// locales/es.ts
2export const esLocale = {
3 labels: {
4 ok: 'Aceptar',
5 cancel: 'Cancelar',
6 am: 'AM',
7 pm: 'PM'
8 }
9};
10
11// locales/fr.ts
12export const frLocale = {
13 labels: {
14 ok: 'Valider',
15 cancel: 'Annuler',
16 am: 'AM',
17 pm: 'PM'
18 }
19};
20
21// locales/de.ts
22export const deLocale = {
23 labels: {
24 ok: 'OK',
25 cancel: 'Abbrechen',
26 am: 'AM',
27 pm: 'PM'
28 }
29};
30
31// locales/ja.ts
32export const jaLocale = {
33 okLabel: '確認',
34 cancelLabel: 'キャンセル',
35 amLabel: '午前',
36 pmLabel: '午後'
37};
38
39// locales/zh.ts
40export const zhLocale = {
41 okLabel: '确定',
42 cancelLabel: '取消',
43 amLabel: '上午',
44 pmLabel: '下午'
45};

Using Language Configs

index.tstypescript
1import TimepickerUI from 'timepicker-ui';
2import { esLocale } from './locales/es';
3
4const picker = new TimepickerUI(input, {
5 ...esLocale,
6 clock: { type: '24h' }
7});
8
9picker.create();

Dynamic Language Switching

index.tstypescript
1import { esLocale, frLocale, deLocale } from './locales';
2
3const locales = {
4 es: esLocale,
5 fr: frLocale,
6 de: deLocale
7};
8
9function createLocalizedPicker(language: string) {
10 const locale = locales[language] || esLocale;
11
12 const picker = new TimepickerUI(input, {
13 ...locale,
14 clock: { type: '24h' }
15 });
16
17 return picker;
18}
19
20// Usage
21let picker = createLocalizedPicker('es');
22picker.create();
23
24// Switch language
25picker.destroy();
26picker = createLocalizedPicker('fr');
27picker.create();

Time Format Localization

Different regions use different time formats. Configure the clock type based on locale:

12-hour Format (US, UK, Canada)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '12h' },
3 labels: {
4 ok: 'OK',
5 cancel: 'Cancel',
6 am: 'AM',
7 pm: 'PM'
8 }
9});

24-hour Format (Europe, Asia, Most of World)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '24h' },
3 labels: {
4 ok: 'OK',
5 cancel: 'Annuler'
6 }
7 // No am/pm labels needed for 24h format
8});

Auto-detect User Locale

index.tstypescript
1function getLocaleTimeFormat() {
2 // Get user's locale
3 const locale = navigator.language || 'en-US';
4
5 // Test if locale uses 12-hour format
6 const testDate = new Date(2000, 0, 1, 13, 0);
7 const formatted = testDate.toLocaleTimeString(locale);
8
9 return formatted.includes('PM') || formatted.includes('AM') ? '12h' : '24h';
10}
11
12// Usage
13const clockType = getLocaleTimeFormat();
14const picker = new TimepickerUI(input, {
15 clock: { type: clockType }
16});
17
18picker.create();

Complete Locale Examples

Ready-to-use configurations for common languages:

Japanese (日本語)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '24h' },
3 labels: {
4 ok: '確認',
5 cancel: 'キャンセル'
6 }
7});

Chinese Simplified (简体中文)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '24h' },
3 labels: {
4 ok: '确定',
5 cancel: '取消'
6 }
7});

Arabic (العربية)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '12h' },
3 labels: {
4 ok: 'موافق',
5 cancel: 'إلغاء',
6 am: 'ص',
7 pm: 'م'
8 }
9});

Russian (Русский)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '24h' },
3 labels: {
4 ok: 'ОК',
5 cancel: 'Отмена'
6 }
7});

Portuguese (Português)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '24h' },
3 labels: {
4 ok: 'Confirmar',
5 cancel: 'Cancelar'
6 }
7});

Italian (Italiano)

index.tstypescript
1const picker = new TimepickerUI(input, {
2 clock: { type: '24h' },
3 labels: {
4 ok: 'OK',
5 cancel: 'Annulla'
6 }
7});

React i18n Integration

Integrate with popular i18n libraries:

react-i18next

index.tstypescript
1import { useTranslation } from 'react-i18next';
2import { useEffect, useRef } from 'react';
3import TimepickerUI from 'timepicker-ui';
4
5function LocalizedTimePicker() {
6 const { t, i18n } = useTranslation();
7 const inputRef = useRef<HTMLInputElement>(null);
8 const pickerRef = useRef<TimepickerUI | null>(null);
9
10 useEffect(() => {
11 if (inputRef.current) {
12 pickerRef.current = new TimepickerUI(inputRef.current, {
13 clock: {
14 type: i18n.language.includes('en') ? '12h' : '24h'
15 },
16 labels: {
17 ok: t('timepicker.ok'),
18 cancel: t('timepicker.cancel'),
19 am: t('timepicker.am'),
20 pm: t('timepicker.pm')
21 }
22 });
23
24 pickerRef.current.create();
25 }
26
27 return () => {
28 pickerRef.current?.destroy();
29 };
30 }, [t, i18n.language]);
31
32 return <input ref={inputRef} type="time" />;
33}

i18n Translation File

index.jsonjson
1// locales/en/translation.json
2{
3 "timepicker": {
4 "ok": "OK",
5 "cancel": "Cancel",
6 "am": "AM",
7 "pm": "PM"
8 }
9}
10
11// locales/es/translation.json
12{
13 "timepicker": {
14 "ok": "Aceptar",
15 "cancel": "Cancelar",
16 "am": "AM",
17 "pm": "PM"
18 }
19}
20
21// locales/fr/translation.json
22{
23 "timepicker": {
24 "ok": "Valider",
25 "cancel": "Annuler",
26 "am": "AM",
27 "pm": "PM"
28 }
29}

Right-to-Left (RTL) Support

Support for RTL languages like Arabic and Hebrew:

index.csscss
1/* Add to your CSS */
2[dir="rtl"] .timepicker-ui-wrapper {
3 direction: rtl;
4}
5
6[dir="rtl"] .timepicker-ui-hour,
7[dir="rtl"] .timepicker-ui-minutes {
8 text-align: right;
9}
10
11// JavaScript
12const picker = new TimepickerUI(input, {
13 clock: { type: '12h' },
14 labels: {
15 ok: 'موافق',
16 cancel: 'إلغاء',
17 am: 'ص',
18 pm: 'م'
19 }
20});
21
22picker.create();
23
24// Set RTL on container
25document.querySelector('.timepicker-container').dir = 'rtl';

Contributing Translations

If you'd like to contribute translations for additional languages to the official documentation or create a locale pack, please open a pull request on GitHub. Community translations help make Timepicker-UI accessible to users worldwide.

View on GitHub