From ce469b1e2d66a5f598d316e0836ea361a4ccc61a Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Thu, 13 Aug 2020 15:08:27 +0200 Subject: Fixed setting moment.js locale to user-defined language Signed-off-by: Erik Michelson --- public/js/extra.js | 7 ++++++- public/js/locale.js | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/public/js/extra.js b/public/js/extra.js index 6cda6171..10c3735b 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -1,6 +1,6 @@ /* eslint-env browser, jquery */ /* eslint no-console: ["error", { allow: ["warn", "error"] }] */ -/* global moment, serverurl */ +/* global moment, serverurl, Cookies */ import Prism from 'prismjs' import hljs from 'highlight.js' @@ -31,6 +31,11 @@ require('../vendor/md-toc') var Viz = require('viz.js') const ui = getUIElements() +if (Cookies.get('locale')) { + const lang = Cookies.get('locale') + moment.locale(lang) +} + // auto update last change window.createtime = null window.lastchangetime = null diff --git a/public/js/locale.js b/public/js/locale.js index 670370d4..05c7b3e3 100644 --- a/public/js/locale.js +++ b/public/js/locale.js @@ -21,6 +21,7 @@ if (Cookies.get('locale')) { } locale.val(lang) +window.moment.locale(lang) $('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected') locale.change(function () { -- cgit v1.2.3 From da35e73346eba908071a7a5c5831a3d280769b9c Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Thu, 13 Aug 2020 23:41:44 +0200 Subject: Restructured locale.js to be included into the editor's js bundle Until now client-side translations were only possible in the context of the intro/history page, because the locale-detection logic relied on the language selector as a source of available languages. The editor of course has no such selector. With this commit, I copied the list of available languages from the i18n-initialization (server-side) to support language detection in the editor too. Signed-off-by: Erik Michelson --- public/js/extra.js | 8 ++------ public/js/locale.js | 57 ++++++++++++++++++++++++++++++----------------------- 2 files changed, 34 insertions(+), 31 deletions(-) diff --git a/public/js/extra.js b/public/js/extra.js index 10c3735b..0ba09388 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -1,6 +1,6 @@ /* eslint-env browser, jquery */ /* eslint no-console: ["error", { allow: ["warn", "error"] }] */ -/* global moment, serverurl, Cookies */ +/* global moment, serverurl */ import Prism from 'prismjs' import hljs from 'highlight.js' @@ -27,15 +27,11 @@ require('prismjs/components/prism-makefile') require('prismjs/components/prism-gherkin') require('./lib/common/login') +require('./locale') require('../vendor/md-toc') var Viz = require('viz.js') const ui = getUIElements() -if (Cookies.get('locale')) { - const lang = Cookies.get('locale') - moment.locale(lang) -} - // auto update last change window.createtime = null window.lastchangetime = null diff --git a/public/js/locale.js b/public/js/locale.js index 05c7b3e3..aca35b98 100644 --- a/public/js/locale.js +++ b/public/js/locale.js @@ -1,33 +1,40 @@ /* eslint-env browser, jquery */ /* global Cookies */ -var lang = 'en' -var userLang = navigator.language || navigator.userLanguage -var userLangCode = userLang.split('-')[0] -var locale = $('.ui-locale') -var supportLangs = [] -$('.ui-locale option').each(function () { - supportLangs.push($(this).val()) -}) -if (Cookies.get('locale')) { - lang = Cookies.get('locale') - if (lang === 'zh') { - lang = 'zh-TW' +const supported = ['en', 'zh-CN', 'zh-TW', 'fr', 'de', 'ja', 'es', 'ca', 'el', 'pt', 'it', 'tr', 'ru', 'nl', 'hr', 'pl', 'uk', 'hi', 'sv', 'eo', 'da', 'ko', 'id', 'sr', 'vi', 'ar', 'cs', 'sk'] + +function detectLang () { + if (Cookies.get('locale')) { + let lang = Cookies.get('locale') + if (lang === 'zh') { + lang = 'zh-TW' + } + return lang + } + const userLang = navigator.language || navigator.userLanguage + const userLangCode = userLang.split('-')[0] + if (supported.includes(userLangCode)) { + return userLangCode + } else if (supported.includes(userLang)) { + return userLang } -} else if (supportLangs.indexOf(userLang) !== -1) { - lang = supportLangs[supportLangs.indexOf(userLang)] -} else if (supportLangs.indexOf(userLangCode) !== -1) { - lang = supportLangs[supportLangs.indexOf(userLangCode)] + return 'en' } -locale.val(lang) -window.moment.locale(lang) -$('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected') +const lang = detectLang() +const localeSelector = $('.ui-locale') -locale.change(function () { - Cookies.set('locale', $(this).val(), { - expires: 365, - sameSite: 'strict' +// the following condition is needed as the selector is only available in the intro/history page +if (localeSelector.length > 0) { + localeSelector.val(lang) + $('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected') + localeSelector.change(function () { + Cookies.set('locale', $(this).val(), { + expires: 365, + sameSite: 'strict' + }) + window.location.reload() }) - window.location.reload() -}) +} + +window.moment.locale(lang) -- cgit v1.2.3 From c9442c3859810d1ef4a6a23a0027629e5545de7d Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Thu, 13 Aug 2020 23:55:28 +0200 Subject: Made changed/created status translatable The current version of CodiMD/HedgeDoc does only support translations to be filled on server-side rendering. To allow the translation of the changed/created texts, I duplicated the container that holds the text, and pre-filed these containers with the translation server-side. The client just needs to hide the unneeded container and show the right one to show the translated status text. Signed-off-by: Erik Michelson --- public/js/extra.js | 9 ++++++--- public/views/codimd/body.ejs | 5 +++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/public/js/extra.js b/public/js/extra.js index 0ba09388..a6b01a91 100644 --- a/public/js/extra.js +++ b/public/js/extra.js @@ -36,7 +36,8 @@ const ui = getUIElements() window.createtime = null window.lastchangetime = null window.lastchangeui = { - status: $('.ui-status-lastchange'), + statusChanged: $('.ui-status-lastchange.changed'), + statusCreated: $('.ui-status-lastchange.created'), time: $('.ui-lastchange'), user: $('.ui-lastchangeuser'), nouser: $('.ui-no-lastchangeuser') @@ -48,9 +49,11 @@ export function updateLastChange () { if (!window.lastchangeui) return if (window.createtime) { if (window.createtime && !window.lastchangetime) { - window.lastchangeui.status.text('created') + window.lastchangeui.statusChanged.hide() + window.lastchangeui.statusCreated.show() } else { - window.lastchangeui.status.text('changed') + window.lastchangeui.statusChanged.show() + window.lastchangeui.statusCreated.hide() } const time = window.lastchangetime || window.createtime window.lastchangeui.time.html(moment(time).fromNow()) diff --git a/public/views/codimd/body.ejs b/public/views/codimd/body.ejs index 5fe3d6bb..96cb8f95 100644 --- a/public/views/codimd/body.ejs +++ b/public/views/codimd/body.ejs @@ -7,8 +7,9 @@ - -   +   + <%= __('changed') %> + <%= __('created') %> -- cgit v1.2.3 From 062facb23295ed706e7339157dc27dc1f4a6f971 Mon Sep 17 00:00:00 2001 From: Erik Michelson Date: Fri, 14 Aug 2020 00:05:28 +0200 Subject: Added translation keys for changed and created Signed-off-by: Erik Michelson --- locales/en.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/locales/en.json b/locales/en.json index d4fc24d1..3d30b66e 100644 --- a/locales/en.json +++ b/locales/en.json @@ -124,5 +124,7 @@ "Limited - Signed-in people can edit (forbid guests)": "Limited - Signed-in people can edit (forbid guests)", "Locked - Only owner can edit": "Locked - Only owner can edit", "Protected - Only owner can edit (forbid guests)": "Protected - Only owner can edit (forbid guests)", - "Private - Only owner can view & edit": "Private - Only owner can view & edit" + "Private - Only owner can view & edit": "Private - Only owner can view & edit", + "changed": "changed", + "created": "created" } \ No newline at end of file -- cgit v1.2.3