From 2760d638b6e90509de6575530f9d7623daaf4988 Mon Sep 17 00:00:00 2001 From: tt <1109394634@qq.com> Date: Thu, 22 Feb 2024 11:36:40 +0800 Subject: [PATCH] refactor code --- src/components/HeadInfo.tsx | 6 +++--- src/config.ts | 12 +++++++++++- src/configs/supportLanguage.ts | 26 -------------------------- src/middleware.ts | 2 +- 4 files changed, 15 insertions(+), 31 deletions(-) delete mode 100644 src/configs/supportLanguage.ts diff --git a/src/components/HeadInfo.tsx b/src/components/HeadInfo.tsx index c1c5c18..6d140c8 100644 --- a/src/components/HeadInfo.tsx +++ b/src/components/HeadInfo.tsx @@ -1,4 +1,4 @@ -import {languageList} from "~/configs/supportLanguage"; +import { languages } from "~/config"; const HeadInfo = ({ title = "", @@ -11,7 +11,7 @@ const HeadInfo = ({ {title} { - languageList.map((item) => { + languages.map((item) => { const currentPage = page; let hrefLang = item.code; if (item.lang == 'en') { @@ -25,7 +25,7 @@ const HeadInfo = ({ }) } { - languageList.map((item) => { + languages.map((item) => { const currentPage = page; let hrefLang = item.code; let href = `${process.env.NEXT_PUBLIC_SITE_URL}/${item.lang}${currentPage}`; diff --git a/src/config.ts b/src/config.ts index 5f8bb7f..34e2205 100755 --- a/src/config.ts +++ b/src/config.ts @@ -4,17 +4,20 @@ export const locales = ['en', 'zh', 'ja'] as const; export const languages = [ { + code: "en-US", lang: "en", language: "English", }, { + code: "zh-CN", lang: "zh", language: "简体中文", }, { + code: "ja-JP", lang: "ja", language: "日本語", - }, + } ] export const pathnames = { @@ -26,3 +29,10 @@ export const localePrefix = 'as-needed'; export type AppPathnames = keyof typeof pathnames; +export const getLanguageByLang = (lang) => { + for (let i = 0; i < languages.length; i++) { + if (lang == languages[i].lang) { + return languages[i]; + } + } +} diff --git a/src/configs/supportLanguage.ts b/src/configs/supportLanguage.ts deleted file mode 100644 index 4059af8..0000000 --- a/src/configs/supportLanguage.ts +++ /dev/null @@ -1,26 +0,0 @@ - -export const languageList = [ - { - code: "en-US", - lang: "en", - language: "English", - }, - { - code: "zh-CN", - lang: "zh", - language: "简体中文", - }, - { - code: "ja-JP", - lang: "ja", - language: "日本語", - } -] - -export const getLanguageByLang = (lang) => { - for (let i = 0; i < languageList.length; i++) { - if (lang == languageList[i].lang) { - return languageList[i]; - } - } -} diff --git a/src/middleware.ts b/src/middleware.ts index 76d9caa..6527fd3 100755 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -16,7 +16,7 @@ export const config = { // Set a cookie to remember the previous locale for // all requests that have a locale prefix - '/(en|zh)/:path*', + '/(en|zh|ja)/:path*', // Enable redirects that add missing locales // (e.g. `/pathnames` -> `/en/pathnames`)