Skip to content

Commit

Permalink
fix: 修改Accept-Language 的值为从 path 中读取
Browse files Browse the repository at this point in the history
  • Loading branch information
Dearest committed Apr 28, 2023
1 parent cfbf823 commit 9d649e4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
3 changes: 2 additions & 1 deletion features/footer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getSupportLinks } from '@/services'
import { useTranslation } from 'react-i18next'
import styles from './index.module.scss'
import { useMount, useAsyncEffect } from 'ahooks'
import { getBasenameLocale } from '@/utils/common'

const Footer: FC = () => {
const [isCN, setISCN] = useState(false)
Expand Down Expand Up @@ -68,7 +69,7 @@ const Footer: FC = () => {

useAsyncEffect(async () => {
await fetchLegalTerms()
}, [])
}, [getBasenameLocale()])

useMount(() => {
const isCN = window.location.hostname.includes('.cn')
Expand Down
13 changes: 3 additions & 10 deletions services/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { getBasenameLocale } from '@/utils/common'

let host = 'https://m.longbridge.xyz/api/forward'

if (process.env.PROXY === 'prod') {
host = 'https://m.lbkrs.com/api/forward'
}

// 获取 html 上的语言
export const getHtmlLang = () => {
const html = document.querySelector('html')
if (html) {
return html.getAttribute('lang') || 'zh-CN'
}
return 'zh-CN'
}

async function transformRes(res: any) {
if (res.ok) {
const data = await res.json()
Expand Down Expand Up @@ -153,7 +146,7 @@ export const getSupportLinks = async () => {
'Accept': 'application/json, text/plain, */*',
'account-channel': 'lb',
'org-id': '1',
'Accept-Language': getHtmlLang(),
'Accept-Language': getBasenameLocale(),
},
})
return transformRes(resp)
Expand Down
5 changes: 4 additions & 1 deletion utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export function getRootDomain(hostname: string) {
}
// 在最外面获取当前 path 上的语言信息
export function getBasenameLocale(path?: string) {
if (isServer()) {
return ''
}
const invalidLocaleRegexResult = path || window.location.pathname.match(/^\/(zh-CN|en|zh-HK)\/?/)
return invalidLocaleRegexResult?.[1] || ''
}
Expand Down Expand Up @@ -51,4 +54,4 @@ export function fillZero(val: string | number) {
return val
}
return `0${val}`
}
}

0 comments on commit 9d649e4

Please sign in to comment.