Merge pull request #468 from codimd/fix/moment-js-locale

[1.x] Fix: Set moment.js locale to users' locale (#275)
This commit is contained in:
David Mehren 2020-08-19 19:36:04 +02:00 committed by GitHub
commit 8cf41ee669
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 30 deletions

View file

@ -124,5 +124,7 @@
"Limited - Signed-in people can edit (forbid guests)": "Limited - Signed-in people can edit (forbid guests)", "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", "Locked - Only owner can edit": "Locked - Only owner can edit",
"Protected - Only owner can edit (forbid guests)": "Protected - Only owner can edit (forbid guests)", "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"
} }

View file

@ -27,6 +27,7 @@ require('prismjs/components/prism-makefile')
require('prismjs/components/prism-gherkin') require('prismjs/components/prism-gherkin')
require('./lib/common/login') require('./lib/common/login')
require('./locale')
require('../vendor/md-toc') require('../vendor/md-toc')
var Viz = require('viz.js') var Viz = require('viz.js')
const ui = getUIElements() const ui = getUIElements()
@ -35,7 +36,8 @@ const ui = getUIElements()
window.createtime = null window.createtime = null
window.lastchangetime = null window.lastchangetime = null
window.lastchangeui = { window.lastchangeui = {
status: $('.ui-status-lastchange'), statusChanged: $('.ui-status-lastchange.changed'),
statusCreated: $('.ui-status-lastchange.created'),
time: $('.ui-lastchange'), time: $('.ui-lastchange'),
user: $('.ui-lastchangeuser'), user: $('.ui-lastchangeuser'),
nouser: $('.ui-no-lastchangeuser') nouser: $('.ui-no-lastchangeuser')
@ -47,9 +49,11 @@ export function updateLastChange () {
if (!window.lastchangeui) return if (!window.lastchangeui) return
if (window.createtime) { if (window.createtime) {
if (window.createtime && !window.lastchangetime) { if (window.createtime && !window.lastchangetime) {
window.lastchangeui.status.text('created') window.lastchangeui.statusChanged.hide()
window.lastchangeui.statusCreated.show()
} else { } else {
window.lastchangeui.status.text('changed') window.lastchangeui.statusChanged.show()
window.lastchangeui.statusCreated.hide()
} }
const time = window.lastchangetime || window.createtime const time = window.lastchangetime || window.createtime
window.lastchangeui.time.html(moment(time).fromNow()) window.lastchangeui.time.html(moment(time).fromNow())

View file

@ -1,32 +1,40 @@
/* eslint-env browser, jquery */ /* eslint-env browser, jquery */
/* global Cookies */ /* global Cookies */
var lang = 'en' 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']
var userLang = navigator.language || navigator.userLanguage
var userLangCode = userLang.split('-')[0] function detectLang () {
var locale = $('.ui-locale') if (Cookies.get('locale')) {
var supportLangs = [] let lang = Cookies.get('locale')
$('.ui-locale option').each(function () { if (lang === 'zh') {
supportLangs.push($(this).val()) lang = 'zh-TW'
}) }
if (Cookies.get('locale')) { return lang
lang = Cookies.get('locale')
if (lang === 'zh') {
lang = 'zh-TW'
} }
} else if (supportLangs.indexOf(userLang) !== -1) { const userLang = navigator.language || navigator.userLanguage
lang = supportLangs[supportLangs.indexOf(userLang)] const userLangCode = userLang.split('-')[0]
} else if (supportLangs.indexOf(userLangCode) !== -1) { if (supported.includes(userLangCode)) {
lang = supportLangs[supportLangs.indexOf(userLangCode)] return userLangCode
} else if (supported.includes(userLang)) {
return userLang
}
return 'en'
} }
locale.val(lang) const lang = detectLang()
$('select.ui-locale option[value="' + lang + '"]').attr('selected', 'selected') const localeSelector = $('.ui-locale')
locale.change(function () { // the following condition is needed as the selector is only available in the intro/history page
Cookies.set('locale', $(this).val(), { if (localeSelector.length > 0) {
expires: 365, localeSelector.val(lang)
sameSite: 'strict' $('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)

View file

@ -7,8 +7,9 @@
<small> <small>
<span> <span>
<span class="ui-lastchangeuser" style="display: none;">&thinsp;<i class="ui-user-icon small" data-toggle="tooltip" data-placement="right"></i></span> <span class="ui-lastchangeuser" style="display: none;">&thinsp;<i class="ui-user-icon small" data-toggle="tooltip" data-placement="right"></i></span>
<span class="ui-no-lastchangeuser">&thinsp;<i class="fa fa-clock-o fa-fw" style="width: 18px;"></i></span> <span class="ui-no-lastchangeuser">&thinsp;<i class="fa fa-clock-o fa-fw" style="width: 18px;"></i></span>&nbsp;
&nbsp;<span class="text-uppercase ui-status-lastchange"></span> <span class="text-uppercase ui-status-lastchange changed"><%= __('changed') %></span>
<span class="text-uppercase ui-status-lastchange created"><%= __('created') %></span>
<span class="ui-lastchange text-uppercase"></span> <span class="ui-lastchange text-uppercase"></span>
</span> </span>
<span class="ui-permission dropdown pull-right"> <span class="ui-permission dropdown pull-right">