From 3c1fb681903cb3ed5c1a02d8ce67ed1b376771ef Mon Sep 17 00:00:00 2001 From: KillyMXI Date: Sun, 16 Jul 2017 03:38:37 +0300 Subject: www: i18n in js --- juick-www/src/main/static/scripts.js | 113 ++++++++++++++++++++++++++--------- 1 file changed, 84 insertions(+), 29 deletions(-) (limited to 'juick-www/src/main/static/scripts.js') diff --git a/juick-www/src/main/static/scripts.js b/juick-www/src/main/static/scripts.js index fa6d1307..a5da0849 100644 --- a/juick-www/src/main/static/scripts.js +++ b/juick-www/src/main/static/scripts.js @@ -20,6 +20,7 @@ require('whatwg-fetch'); require('element-closest'); require('classlist.js'); var killy = require('killy'); + if (!('remove' in Element.prototype)) { // Firefox <23 Element.prototype.remove = function() { if (this.parentNode) { @@ -31,6 +32,54 @@ if (!('remove' in Element.prototype)) { // Firefox <23 NodeList.prototype.forEach = Array.prototype.forEach; HTMLCollection.prototype.forEach = Array.prototype.forEach; +const translations = { + 'en': { + 'message.inReplyTo': 'in reply to', + 'message.reply': 'Reply', + 'postForm.pleaseInputMessageText': 'Please input message text', + //'postForm.upload': 'upload', + 'comment.writeComment': 'Write comment...', + 'shareDialog.linkToMessage': 'Link to message', + 'shareDialog.messageNumber': 'Message number', + 'shareDialog.share': 'Share', + 'loginDialog.pleaseIntroduceYourself': 'Please introduce yourself', + 'loginDialog.registeredAlready': 'Registered already?', + 'loginDialog.username': 'Username', + 'loginDialog.password': 'Password', + 'loginDialog.vk': 'vk.com', + 'error.error': 'Error' + }, + 'ru': { + 'message.inReplyTo': 'в ответ на', + 'message.reply': 'Ответить', + 'postForm.pleaseInputMessageText': 'Пожалуйста, введите текст сообщения', + 'postForm.upload': 'загрузить', + 'comment.writeComment': 'Написать комментарий...', + 'shareDialog.linkToMessage': 'Ссылка на сообщение', + 'shareDialog.messageNumber': 'Номер сообщения', + 'shareDialog.share': 'Поделиться', + 'loginDialog.pleaseIntroduceYourself': 'Пожалуйста, представьтесь', + 'loginDialog.registeredAlready': 'Уже зарегистрированы?', + 'loginDialog.username': 'Имя пользователя', + 'loginDialog.password': 'Пароль', + 'loginDialog.vk': 'ВКонтакте', + 'error.error': 'Ошибка' + } +}; + +function getLang() { + return (window.navigator.languages && window.navigator.languages[0]) + || window.navigator.userLanguage + || window.navigator.language; +} +function i18n(key, lang=undefined) { + const fallbackLang = 'ru'; + lang = lang || getLang().split('-')[0]; + return (translations[lang] && translations[lang][key]) + || translations[fallbackLang][key] + || key; +} + var ws, pageTitle; @@ -105,7 +154,7 @@ function wsIncomingReply(msg) { const unsetProto = (url) => url.replace(/^(https?:)?(?=\/\/)/i, ''); let msgNum = '/' + msg.rid; if (msg.replyto > 0) { - msgNum += ` в ответ на /${msg.replyto}`; + msgNum += ` ${i18n('message.inReplyTo')} /${msg.replyto}`; } let photoDiv = (msg.photo === undefined) ? '' : `
@@ -124,7 +173,7 @@ function wsIncomingReply(msg) {
${killy.format(msg.body, msg.mid, false)}
${photoDiv} -
+ `; @@ -213,7 +262,7 @@ function unfoldPostForm() { function newMessage() { if (document.querySelector('#newmessage textarea').value.length == 0) { - openDialog('

Пожалуйста, введите текст сообщения

'); + openDialog('

' + i18n('postForm.pleaseInputMessageText') + '

'); return false; } return true; @@ -267,7 +316,7 @@ function taWrapper() { txtarea.setAttribute('name', 'body'); txtarea.setAttribute('rows', 1); txtarea.setAttribute('class', 'reply narrow'); - txtarea.setAttribute('placeholder', 'Написать комментарий...'); + txtarea.setAttribute('placeholder', i18n('comment.writeComment')); var txtKeypress = function (e) { postformListener(e.target, e); }; @@ -359,13 +408,13 @@ function attachMessagePhoto(div) { inp.style.width = 0; inp.style.height = 0; inp.addEventListener('change', function () { - div.textContent = 'загрузить (✓)'; + div.textContent = i18n('postForm.upload') + ' (✓)'; }); f.appendChild(inp); inp.click(); } else { finput.remove(); - div.textContent = 'загрузить'; + div.textContent = i18n('postForm.upload'); } } @@ -383,23 +432,25 @@ function unfoldReply() { } function showMessageLinksDialog(mid, rid) { - var hlink = window.location.protocol + '//juick.com/' + mid, - mlink = '#' + mid; + let hlink = window.location.protocol + '//juick.com/' + mid; + let mlink = '#' + mid; if (rid > 0) { hlink += '#' + rid; mlink += '/' + rid; } - var hlinkenc = encodeURIComponent(hlink), - html = '
Ссылка на сообщение:' - + '
' + hlink + '
' - + 'Номер сообщения:' - + '
' + mlink + '
' - + 'Поделиться:
'; + let hlinkenc = encodeURIComponent(hlink); + let html = ` +
+ ${i18n('shareDialog.linkToMessage')}:
${hlink}
+ ${i18n('shareDialog.messageNumber')}:
${mlink}
+ ${i18n('shareDialog.share')}: + +
`; openDialog(html); } @@ -459,15 +510,19 @@ function checkUsername() { /******************************************************************************/ function openDialogLogin() { - var html = '

Пожалуйста, представьтесь:' - + 'Facebook ' - + 'ВКонтакте

' - + '

Уже зарегистрированы?

' - + '
' - + '
' - + '
' - + '' - + '
'; + let html = ` +
+

${i18n('loginDialog.pleaseIntroduceYourself')}: + Facebook + ${i18n('loginDialog.vk')} +

+

${i18n('loginDialog.registeredAlready')}

+
+
+
+ +
+
`; openDialog(html); return false; } @@ -492,7 +547,7 @@ function likeMessage(e, mid) { } }) .catch(function () { - e.closest('article').appendChild(resultMessage('Ошибка')); + e.closest('article').appendChild(resultMessage(i18n('error.error'))); }); } return false; -- cgit v1.2.3