django-webpush icon indicating copy to clipboard operation
django-webpush copied to clipboard

Allow translations in subscribing UI

Open arogachev opened this issue 8 years ago • 2 comments

Currently all informing messages for user are hardcoded as js strings, for example:

messageBox.textContent = 'Service Worker is not supported in your Browser!';

Translation to different language requires copying whole js and translating these strings.

arogachev avatar Oct 10 '16 05:10 arogachev

We can use gettext

safwanrahman avatar Oct 10 '16 05:10 safwanrahman

The way translation works can be improved for example like that. We can define messages object storing all messages indexed with codes:

var messages = {
    serviceWorkerNotSupported: 'Подписка невозможна, так как технология Service Worker не поддерживается вашим браузером.',
    showNotificationNotSupported: 'Подписка невозможна, так как показ уведомлений не поддерживается вашим браузером.',
    pushNotificationsBlocked: 'Подписка невозможна, так как Push уведомления заблокированы в вашем браузере.',
    pushNotificationsNotSupported: 'Подписка невозможна, так как технология Push уведомлений не поддерживается вашим браузером.',
    subscriptionNotAvailable: 'Не удалось отписаться, т.к. подписка недоступна.',
    subscribeSuccess: 'Вы успешно подписались на уведомления.',
    unsubscribeSuccess: 'Вы успешно отписались от уведомлений.',
    unsubscribeError: 'Возникла ошибка в процессе попытки отписаться от уведомлений.'
};

This is example for russian language. These values will be fetched from the server depending on language setting (and we can translate it using Django).

Then we can define method for getting message:

function getMessage(code) {
    return messages[code];
}

and use it like that:

messageBox.textContent = getMessage('showNotificationNotSupported');

For showing messages in message box we can use this:

function showMessage(code) {
    messageBox.textContent = getMessage('showNotificationNotSupported');
}

and then:

showMessage('showNotificationNotSupported');

This also makes code shorter and easier to read in case of long text messages.

arogachev avatar Oct 10 '16 05:10 arogachev