Skip to content

Commit

Permalink
fix: display win11 and mac^10; lang options are no longer case sensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
imaegoo committed Nov 15, 2023
1 parent 12f841f commit a9b95c4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
14 changes: 5 additions & 9 deletions src/client/utils/i18n/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,15 @@ import i18n from './i18n'
const langs = {
zh: 0,
'zh-cn': 0,
'zh-CN': 0,
'zh-HK': 1,
'zh-hk': 1,
'zh-tw': 2,
'zh-TW': 2,
'en-us': 3,
'en-US': 3,
'en-gb': 3,
'en-GB': 3,
en: 3,
uz: 4,
'uz-UZ': 4,
'uz-uz': 4,
ja: 5,
'ja-JP': 5
'ja-jp': 5
}

let userLanguage = ''
Expand All @@ -28,12 +24,12 @@ const setLanguage = (options = {}) => {
}

const translate = (key, language) => {
const lang = language || userLanguage || navigator.language
const lang = (language || userLanguage || navigator.language).toLowerCase()
let value
if (lang && langs[lang]) {
value = i18n[key][langs[lang]]
} else {
value = i18n[key][langs['zh-CN']]
value = i18n[key][langs['zh-cn']]
}
return value || ''
}
Expand Down
25 changes: 25 additions & 0 deletions src/client/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ const getRecentCommentsApi = async (tcb, options) => {
return result.result.data
}

/**
* 替换 UA 中的 Windows NT 版本号以兼容识别 Windows 11
* https://learn.microsoft.com/en-us/microsoft-edge/web-platform/how-to-detect-win11
* 替换 UA 中的 macOS 版本以兼容识别 Catalina 以上版本的 macOS
*/
const getUserAgent = async () => {
let ua = window.navigator.userAgent
try {
const { platform } = navigator.userAgentData
if (platform === 'Windows' || platform === 'macOS') {
const { platformVersion } = await navigator.userAgentData.getHighEntropyValues(['platformVersion'])
const majorPlatformVersion = parseInt(platformVersion.split('.')[0])
if (platform === 'Windows' && majorPlatformVersion >= 13) {
const correctVersion = '11.0'
ua = ua.replace(/Windows NT 10\.0/i, `Windows NT ${correctVersion}`)
} else if (platform === 'macOS' && majorPlatformVersion >= 11) {
const correctVersion = platformVersion.replace(/\./g, '_')
ua = ua.replace(/Mac OS X 10_[0-9]+_[0-9]+/i, `Mac OS X ${correctVersion}`)
}
}
} catch (e) {}
return ua
}

/**
* 由于 Twikoo 早期版本将 path 视为表达式处理,
* 而其他同类评论系统都是把 path 视为字符串常量,
Expand Down Expand Up @@ -177,6 +201,7 @@ export {
initMarkedOwo,
getCommentsCountApi,
getRecentCommentsApi,
getUserAgent,
getUrl,
getHref,
readAsText,
Expand Down
10 changes: 4 additions & 6 deletions src/client/view/components/TkSubmit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ import iconImage from '@fortawesome/fontawesome-free/svgs/regular/image.svg'
import Clickoutside from 'element-ui/src/utils/clickoutside'
import TkAvatar from './TkAvatar.vue'
import TkMetaInput from './TkMetaInput.vue'
import { marked, call, logger, renderLinks, renderMath, renderCode, initOwoEmotions, initMarkedOwo, t, getUrl, getHref, blobToDataURL } from '../../utils'
import { marked, call, logger, renderLinks, renderMath, renderCode, initOwoEmotions, initMarkedOwo, t, getUrl, getHref, blobToDataURL, getUserAgent } from '../../utils'
import OwO from '../../lib/owo'
const imageTypes = [
Expand Down Expand Up @@ -179,15 +179,13 @@ export default {
if (this.comment.match(new RegExp(`!\\[${t('IMAGE_UPLOAD_PLACEHOLDER')}.+\\]\\(\\)`))) {
throw new Error(t('IMAGE_UPLOAD_PLEASE_WAIT'))
}
const url = getUrl(this.$twikoo.path)
const href = getHref(this.$twikoo.href)
const comment = {
nick: this.nick,
mail: this.mail,
link: this.link,
ua: navigator.userAgent,
url,
href,
ua: await getUserAgent(),
url: getUrl(this.$twikoo.path),
href: getHref(this.$twikoo.href),
comment: marked(this.comment),
pid: this.pid ? this.pid : this.replyId,
rid: this.replyId
Expand Down
25 changes: 24 additions & 1 deletion src/server/function/twikoo/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const fn = {
if (config.SHOW_UA !== 'false') {
try {
const ua = bowser.getParser(comment.ua)
const os = ua.getOS()
const os = fn.fixOS(ua.getOS())
displayOs = [os.name, os.versionName ? os.versionName : os.version].join(' ')
displayBrowser = [ua.getBrowserName(), ua.getBrowserVersion()].join(' ')
} catch (e) {
Expand Down Expand Up @@ -76,6 +76,29 @@ const fn = {
updated: comment.updated
}
},
fixOS (os) {
// Win 11 fix & macOS ^10 fix
if (os.name === 'Windows' && os.version === 'NT 11.0') {
os.versionName = '11'
} else if (os.name === 'macOS') {
const majorPlatformVersion = os.version.split('.')[0]
switch (majorPlatformVersion) {
case 11:
os.versionName = 'Big Sur'
break
case 12:
os.versionName = 'Monterey'
break
case 13:
os.versionName = 'Ventura'
break
case 14:
os.versionName = 'Sonoma'
break
default:
}
}
},
// 获取回复人昵称 / Get replied user nick name
ruser (pid, comments = []) {
const comment = comments.find((item) => item._id === pid)
Expand Down

0 comments on commit a9b95c4

Please sign in to comment.