refactor code
This commit is contained in:
parent
740b3ea975
commit
af708b9043
@ -1,3 +1,8 @@
|
||||
{
|
||||
"extends": "next/core-web-vitals"
|
||||
"extends": "next/core-web-vitals",
|
||||
"rules": {
|
||||
"@next/next/no-img-element": "off",
|
||||
"react-hooks/exhaustive-deps": "off",
|
||||
"@next/next/no-html-link-for-pages": "off"
|
||||
}
|
||||
}
|
||||
|
6
.gitignore
vendored
6
.gitignore
vendored
@ -34,3 +34,9 @@ yarn-error.log*
|
||||
# typescript
|
||||
*.tsbuildinfo
|
||||
next-env.d.ts
|
||||
|
||||
.idea
|
||||
.vscode
|
||||
yarn.lock
|
||||
pnpm-lock.yaml
|
||||
package-lock.json
|
||||
|
@ -10,7 +10,7 @@
|
||||
"dependencies": {
|
||||
"@headlessui/react": "^1.7.18",
|
||||
"@heroicons/react": "^2.1.1",
|
||||
"@next/third-parties": "^14.1.3",
|
||||
"@next/third-parties": "^14.2.25",
|
||||
"@stripe/stripe-js": "^3.0.7",
|
||||
"@tailwindcss/typography": "^0.5.10",
|
||||
"ahooks": "^3.7.10",
|
||||
@ -18,9 +18,9 @@
|
||||
"clsx": "^2.1.0",
|
||||
"date-fns": "^3.3.1",
|
||||
"google-auth-library": "^9.6.3",
|
||||
"next": "14.1.3",
|
||||
"next": "14.2.25",
|
||||
"next-auth": "^4.24.6",
|
||||
"next-intl": "^3.9.2",
|
||||
"next-intl": "^3.26.0",
|
||||
"pg": "^8.11.3",
|
||||
"react": "^18",
|
||||
"react-dom": "^18",
|
||||
@ -36,7 +36,7 @@
|
||||
"@types/react-dom": "^18",
|
||||
"autoprefixer": "^10.0.1",
|
||||
"eslint": "^8",
|
||||
"eslint-config-next": "14.1.3",
|
||||
"eslint-config-next": "14.2.25",
|
||||
"postcss": "^8",
|
||||
"tailwindcss": "^3.3.0",
|
||||
"typescript": "^5"
|
||||
|
@ -1,25 +0,0 @@
|
||||
-- auto-generated definition
|
||||
create table works_translate_task
|
||||
(
|
||||
id bigint generated by default as identity
|
||||
primary key,
|
||||
created_at timestamp with time zone default now() not null,
|
||||
updated_at timestamp with time zone default now() not null,
|
||||
uid varchar,
|
||||
origin_language varchar,
|
||||
status varchar
|
||||
);
|
||||
|
||||
comment on table works_translate_task is 'works_translate_task';
|
||||
|
||||
comment on column works_translate_task.id is '自增id';
|
||||
|
||||
comment on column works_translate_task.created_at is '创建时间';
|
||||
|
||||
comment on column works_translate_task.updated_at is '更新时间';
|
||||
|
||||
comment on column works_translate_task.uid is 'uid';
|
||||
|
||||
comment on column works_translate_task.origin_language is 'origin_language';
|
||||
|
||||
comment on column works_translate_task.status is 'status: 0未翻译,1已翻译';
|
@ -1,78 +0,0 @@
|
||||
import {getDb} from "~/libs/db";
|
||||
import {locales} from "~/config";
|
||||
import {translateContent} from "~/servers/translate";
|
||||
|
||||
export const maxDuration = 300;
|
||||
|
||||
export async function GET() {
|
||||
const db = getDb();
|
||||
const timeFlag = 'workTranslate' + '-=->' + new Date().getTime();
|
||||
console.time(timeFlag);
|
||||
const results = await db.query('select * from works_translate_task where status=$1 order by created_at asc limit 3', [0]);
|
||||
const rows = results.rows;
|
||||
if (rows.length <= 0) {
|
||||
console.log('没有任务需要处理');
|
||||
console.timeEnd(timeFlag);
|
||||
return Response.json({message: '没有任务需要处理'});
|
||||
}
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const oneTask = rows[i];
|
||||
// 翻译为除了原始语言的其他语言,如果原始语言不在支持列表,就翻译为支持的十种语言
|
||||
const origin_language = oneTask.origin_language;
|
||||
const uid = oneTask.uid;
|
||||
|
||||
const needLanguage = getNeedTranslateLanguage(origin_language);
|
||||
|
||||
if (needLanguage.length <= 0) {
|
||||
// 更新状态为已翻译完成
|
||||
await db.query('update works_translate_task set status=$1 where uid=$2', [1, uid]);
|
||||
console.log('没有需要翻译的语言-=->', uid);
|
||||
console.timeEnd(timeFlag);
|
||||
return Response.json({message: '没有需要翻译的语言'});
|
||||
}
|
||||
|
||||
// 查出原始数据
|
||||
const resultsOrigin = await db.query('select * from works where uid=$1 and is_origin=$2 and is_delete=$3', [uid, true, false]);
|
||||
const rowsOrigin = resultsOrigin.rows;
|
||||
if (rowsOrigin.length <= 0) {
|
||||
console.log('没有原始数据-=->', uid);
|
||||
console.timeEnd(timeFlag);
|
||||
// 更新状态为已翻译完成
|
||||
await db.query('update works_translate_task set status=$1 where uid=$2', [1, uid]);
|
||||
return Response.json({message: '没有原始数据'});
|
||||
}
|
||||
const originData = rowsOrigin[0];
|
||||
if (originData.output_url == '') {
|
||||
// 如果原始数据的url还没生成,就不翻译了
|
||||
console.log('原始数据的url还没生成-=->', uid);
|
||||
console.timeEnd(timeFlag);
|
||||
return Response.json({message: '原始数据的url还没生成'});
|
||||
}
|
||||
const timeFlagCurrent = '翻译' + '-=->' + uid;
|
||||
console.time(timeFlagCurrent);
|
||||
for (let i = 0; i < needLanguage.length; i++) {
|
||||
const toLanguage = needLanguage[i];
|
||||
const translateText = await translateContent(originData.input_text, toLanguage);
|
||||
const sqlStr = 'insert into works(uid, input_text, output_url, is_public, status, user_id, revised_text, is_origin, origin_language, current_language) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)';
|
||||
const data = [uid, translateText, originData.output_url, originData.is_public, originData.status, originData.user_id, originData.revised_text, false, originData.origin_language, toLanguage];
|
||||
await db.query(sqlStr, data);
|
||||
}
|
||||
console.timeEnd(timeFlagCurrent);
|
||||
console.log('翻译完-=->', uid);
|
||||
// 更新状态为已翻译完成
|
||||
await db.query('update works_translate_task set status=$1 where uid=$2', [1, uid]);
|
||||
}
|
||||
console.timeEnd(timeFlag);
|
||||
return Response.json({message: '本次翻译任务翻译完'});
|
||||
}
|
||||
|
||||
function getNeedTranslateLanguage(origin_language: string) {
|
||||
const needTranslateLanguage = [];
|
||||
// 判断出需要翻译的语言,并调用翻译
|
||||
for (let i = 0; i < locales.length; i++) {
|
||||
if (origin_language != locales[i]) {
|
||||
needTranslateLanguage.push(locales[i]);
|
||||
}
|
||||
}
|
||||
return needTranslateLanguage;
|
||||
}
|
@ -1,13 +1,12 @@
|
||||
import {getUserById} from "~/servers/user";
|
||||
import {checkUserTimes, countDownUserTimes} from "~/servers/manageUserTimes";
|
||||
import {v4 as uuidv4} from 'uuid';
|
||||
import {getReplicateClient} from "~/libs/replicateClient";
|
||||
import {getInput} from "~/libs/replicate";
|
||||
import {getDb} from "~/libs/db";
|
||||
import {getLanguage} from "~/servers/language";
|
||||
import {checkSubscribe} from "~/servers/subscribe";
|
||||
import {checkSensitiveInputText} from "~/servers/checkInput";
|
||||
|
||||
import { getUserById } from "~/servers/user";
|
||||
import { checkUserTimes, countDownUserTimes } from "~/servers/manageUserTimes";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { getReplicateClient } from "~/libs/replicateClient";
|
||||
import { getInput } from "~/libs/replicate";
|
||||
import { getDb } from "~/libs/db";
|
||||
import { getLanguage } from "~/servers/language";
|
||||
import { checkSubscribe } from "~/servers/subscribe";
|
||||
import { checkSensitiveInputText } from "~/servers/checkInput";
|
||||
|
||||
export async function POST(req: Request, res: Response) {
|
||||
let json = await req.json();
|
||||
@ -15,14 +14,14 @@ export async function POST(req: Request, res: Response) {
|
||||
let user_id = json.user_id;
|
||||
let is_public = json.is_public;
|
||||
|
||||
if (!user_id && process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != '0') {
|
||||
return Response.json({msg: "Login to continue.", status: 601});
|
||||
if (!user_id && process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != "0") {
|
||||
return Response.json({ msg: "Login to continue.", status: 601 });
|
||||
}
|
||||
|
||||
// 检查用户在数据库是否存在,不存在则返回需登录
|
||||
const resultsUser = await getUserById(user_id);
|
||||
if (resultsUser.email == '' && process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != '0') {
|
||||
return Response.json({msg: "Login to continue.", status: 601});
|
||||
if (resultsUser.email == "" && process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != "0") {
|
||||
return Response.json({ msg: "Login to continue.", status: 601 });
|
||||
}
|
||||
|
||||
const checkSubscribeStatus = await checkSubscribe(user_id);
|
||||
@ -30,14 +29,14 @@ export async function POST(req: Request, res: Response) {
|
||||
if (!is_public) {
|
||||
// 判断用户是否订阅状态,否则返回错误
|
||||
if (!checkSubscribeStatus) {
|
||||
return Response.json({msg: "Pricing to continue.", status: 602});
|
||||
return Response.json({ msg: "Pricing to continue.", status: 602 });
|
||||
}
|
||||
}
|
||||
|
||||
if (!checkSubscribeStatus) {
|
||||
const check = await checkUserTimes(user_id);
|
||||
if (!check && process.env.NEXT_PUBLIC_CHECK_AVAILABLE_TIME != '0') {
|
||||
return Response.json({msg: "Pricing to continue.", status: 602});
|
||||
if (!check && process.env.NEXT_PUBLIC_CHECK_AVAILABLE_TIME != "0") {
|
||||
return Response.json({ msg: "Pricing to continue.", status: 602 });
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,7 +45,7 @@ export async function POST(req: Request, res: Response) {
|
||||
// 敏感词没通过,校验是否订阅
|
||||
if (!checkSubscribeStatus) {
|
||||
// 未订阅则返回付费再继续
|
||||
return Response.json({msg: "Pricing to continue.", status: 602});
|
||||
return Response.json({ msg: "Pricing to continue.", status: 602 });
|
||||
} else {
|
||||
// 订阅强制设置其为用户私有,不公开
|
||||
is_public = false;
|
||||
@ -63,23 +62,30 @@ export async function POST(req: Request, res: Response) {
|
||||
input: input,
|
||||
webhook: `${process.env.REPLICATE_WEBHOOK}/api/generate/callByReplicate?uid=${uid}`,
|
||||
webhook_events_filter: ["completed"],
|
||||
})
|
||||
});
|
||||
|
||||
const db = getDb();
|
||||
// 创建新的数据
|
||||
await db.query('insert into works(uid, input_text, output_url, is_public, status, user_id, revised_text, is_origin, origin_language, current_language) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)',
|
||||
[uid, textStr, '', is_public, 0, user_id, input.prompt, true, origin_language, origin_language]);
|
||||
// 创建一条翻译任务
|
||||
await db.query('insert into works_translate_task(uid,origin_language,status) values($1,$2,$3)', [uid, origin_language, 0]);
|
||||
|
||||
await db.query("insert into works(uid, input_text, output_url, is_public, status, user_id, revised_text, is_origin, origin_language, current_language) values ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)", [
|
||||
uid,
|
||||
textStr,
|
||||
"",
|
||||
is_public,
|
||||
0,
|
||||
user_id,
|
||||
input.prompt,
|
||||
true,
|
||||
origin_language,
|
||||
origin_language,
|
||||
]);
|
||||
// 需要登录,且需要支付时,才操作该项
|
||||
if (process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != '0' && process.env.NEXT_PUBLIC_CHECK_AVAILABLE_TIME != '0' && !checkSubscribeStatus) {
|
||||
if (process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != "0" && process.env.NEXT_PUBLIC_CHECK_AVAILABLE_TIME != "0" && !checkSubscribeStatus) {
|
||||
// 减少用户次数
|
||||
await countDownUserTimes(user_id);
|
||||
}
|
||||
|
||||
const resultInfo = {
|
||||
uid: uid
|
||||
}
|
||||
uid: uid,
|
||||
};
|
||||
return Response.json(resultInfo);
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
import clsx from 'clsx';
|
||||
import {Inter} from 'next/font/google';
|
||||
import {notFound} from 'next/navigation';
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import {ReactNode} from 'react';
|
||||
import {locales} from '~/config';
|
||||
import {CommonProvider} from '~/context/common-context';
|
||||
import {NextAuthProvider} from '~/context/next-auth-context';
|
||||
import {getAuthText, getCommonText, getMenuText, getPricingText} from "~/configs/languageText";
|
||||
import { Inter } from 'next/font/google';
|
||||
import { notFound } from 'next/navigation';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
import { ReactNode } from 'react';
|
||||
import { locales } from '~/i18n/config';
|
||||
import { CommonProvider } from '~/context/common-context';
|
||||
import { NextAuthProvider } from '~/context/next-auth-context';
|
||||
import { getAuthText, getCommonText, getMenuText, getPricingText } from "~/i18n/languageText";
|
||||
|
||||
const inter = Inter({subsets: ['latin']});
|
||||
const inter = Inter({ subsets: ['latin'] });
|
||||
|
||||
type Props = {
|
||||
children: ReactNode;
|
||||
@ -16,19 +16,19 @@ type Props = {
|
||||
};
|
||||
|
||||
export function generateStaticParams() {
|
||||
return locales.map((locale) => ({locale}));
|
||||
return locales.map((locale) => ({ locale }));
|
||||
}
|
||||
|
||||
export default async function LocaleLayout({
|
||||
children,
|
||||
params: {locale}
|
||||
}: Props) {
|
||||
children,
|
||||
params: { locale }
|
||||
}: Props) {
|
||||
|
||||
// Validate that the incoming `locale` parameter is valid
|
||||
if (!locales.includes(locale as any)) notFound();
|
||||
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const commonText = await getCommonText();
|
||||
const authText = await getAuthText();
|
||||
@ -37,21 +37,21 @@ export default async function LocaleLayout({
|
||||
|
||||
return (
|
||||
<html lang={locale}>
|
||||
<head>
|
||||
<script src="https://accounts.google.com/gsi/client" async defer></script>
|
||||
</head>
|
||||
<body suppressHydrationWarning={true} className={clsx(inter.className, 'flex flex-col background-div')}>
|
||||
<NextAuthProvider>
|
||||
<CommonProvider
|
||||
commonText={commonText}
|
||||
authText={authText}
|
||||
menuText={menuText}
|
||||
pricingText={pricingText}
|
||||
>
|
||||
{children}
|
||||
</CommonProvider>
|
||||
</NextAuthProvider>
|
||||
</body>
|
||||
<head>
|
||||
<script src="https://accounts.google.com/gsi/client" async defer></script>
|
||||
</head>
|
||||
<body suppressHydrationWarning={true} className={clsx(inter.className, 'flex flex-col background-div')}>
|
||||
<NextAuthProvider>
|
||||
<CommonProvider
|
||||
commonText={commonText}
|
||||
authText={authText}
|
||||
menuText={menuText}
|
||||
pricingText={pricingText}
|
||||
>
|
||||
{children}
|
||||
</CommonProvider>
|
||||
</NextAuthProvider>
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
|
@ -1,13 +1,13 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getWorksText
|
||||
} from "~/configs/languageText";
|
||||
} from "~/i18n/languageText";
|
||||
|
||||
export default async function IndexPage({params: {locale = ''}}) {
|
||||
export default async function IndexPage({ params: { locale = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const worksText = await getWorksText();
|
||||
|
||||
|
@ -1,16 +1,16 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getIndexPageText,
|
||||
getQuestionText
|
||||
} from "~/configs/languageText";
|
||||
import {getLatestPublicResultList} from "~/servers/works";
|
||||
} from "~/i18n/languageText";
|
||||
import { getLatestPublicResultList } from "~/servers/works";
|
||||
|
||||
export const revalidate = 120;
|
||||
export default async function IndexPage({params: {locale = ''}, searchParams: searchParams}) {
|
||||
export default async function IndexPage({ params: { locale = '' }, searchParams: searchParams }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const indexText = await getIndexPageText();
|
||||
const questionText = await getQuestionText();
|
||||
|
@ -1,9 +1,9 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
export default async function IndexPage({params: {locale = ''}}) {
|
||||
export default async function IndexPage({ params: { locale = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
return (
|
||||
<PageComponent
|
||||
|
@ -1,13 +1,13 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getPrivacyPolicyText
|
||||
} from "~/configs/languageText";
|
||||
} from "~/i18n/languageText";
|
||||
|
||||
export default async function IndexPage({params: {locale = ''}}) {
|
||||
export default async function IndexPage({ params: { locale = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const privacyPolicyText = await getPrivacyPolicyText();
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import {getSearchText} from "~/configs/languageText";
|
||||
import {getLatestPublicResultList} from "~/servers/works";
|
||||
import {getCountSticker} from "~/servers/keyValue";
|
||||
import {searchByWords, addSearchLog} from "~/servers/search";
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
import { getSearchText } from "~/i18n/languageText";
|
||||
import { getLatestPublicResultList } from "~/servers/works";
|
||||
import { getCountSticker } from "~/servers/keyValue";
|
||||
import { searchByWords, addSearchLog } from "~/servers/search";
|
||||
|
||||
export const revalidate = 0;
|
||||
|
||||
export default async function SearchPage({params: {locale = ''}, searchParams: {sticker = ''}}) {
|
||||
export default async function SearchPage({ params: { locale = '' }, searchParams: { sticker = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
|
||||
const countSticker = await getCountSticker();
|
||||
|
@ -1,19 +1,19 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getDetailText,
|
||||
} from "~/configs/languageText";
|
||||
import {getSimilarList, getWorkDetailByUid} from "~/servers/works";
|
||||
import {notFound} from "next/navigation";
|
||||
} from "~/i18n/languageText";
|
||||
import { getSimilarList, getWorkDetailByUid } from "~/servers/works";
|
||||
import { notFound } from "next/navigation";
|
||||
|
||||
// export const revalidate = 86400;
|
||||
export const dynamicParams = true
|
||||
export const dynamic = 'error';
|
||||
|
||||
export default async function IndexPage({params: {locale = '', uid = ''}}) {
|
||||
export default async function IndexPage({ params: { locale = '', uid = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const workDetail = await getWorkDetailByUid(locale, uid);
|
||||
if (workDetail.status == 404) {
|
||||
|
@ -1,19 +1,19 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getExploreText,
|
||||
} from "~/configs/languageText";
|
||||
import {getPagination, getPublicResultList} from "~/servers/works";
|
||||
import {notFound} from "next/navigation";
|
||||
import {getCountSticker} from "~/servers/keyValue";
|
||||
} from "~/i18n/languageText";
|
||||
import { getPagination, getPublicResultList } from "~/servers/works";
|
||||
import { notFound } from "next/navigation";
|
||||
import { getCountSticker } from "~/servers/keyValue";
|
||||
|
||||
export const revalidate = 300;
|
||||
export const dynamic = "force-static";
|
||||
|
||||
export default async function IndexPage({params: {locale = '', page = 2}}) {
|
||||
export default async function IndexPage({ params: { locale = '', page = 2 } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const countSticker = await getCountSticker();
|
||||
|
||||
|
@ -1,17 +1,17 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getExploreText,
|
||||
} from "~/configs/languageText";
|
||||
import {getPagination, getPublicResultList} from "~/servers/works";
|
||||
import {getCountSticker} from "~/servers/keyValue";
|
||||
} from "~/i18n/languageText";
|
||||
import { getPagination, getPublicResultList } from "~/servers/works";
|
||||
import { getCountSticker } from "~/servers/keyValue";
|
||||
|
||||
export const revalidate = 300;
|
||||
|
||||
export default async function IndexPage({params: {locale = ''}}) {
|
||||
export default async function IndexPage({ params: { locale = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const countSticker = await getCountSticker();
|
||||
|
||||
|
@ -1,13 +1,13 @@
|
||||
import PageComponent from "./PageComponent";
|
||||
import {unstable_setRequestLocale} from 'next-intl/server';
|
||||
import { setRequestLocale } from 'next-intl/server';
|
||||
|
||||
import {
|
||||
getTermsOfServiceText
|
||||
} from "~/configs/languageText";
|
||||
} from "~/i18n/languageText";
|
||||
|
||||
export default async function IndexPage({params: {locale = ''}}) {
|
||||
export default async function IndexPage({ params: { locale = '' } }) {
|
||||
// Enable static rendering
|
||||
unstable_setRequestLocale(locale);
|
||||
setRequestLocale(locale);
|
||||
|
||||
const termsOfServiceText = await getTermsOfServiceText();
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
import {languages} from "~/config";
|
||||
import { languages } from "~/i18n/config";
|
||||
|
||||
const HeadInfo = ({
|
||||
locale,
|
||||
page,
|
||||
title,
|
||||
description,
|
||||
}) => {
|
||||
locale,
|
||||
page,
|
||||
title,
|
||||
description,
|
||||
}) => {
|
||||
return (
|
||||
<>
|
||||
<title>{title}</title>
|
||||
<meta name="description" content={description}/>
|
||||
<meta name="description" content={description} />
|
||||
{
|
||||
languages.map((item) => {
|
||||
const currentPage = page;
|
||||
@ -29,7 +29,7 @@ const HeadInfo = ({
|
||||
href = `${process.env.NEXT_PUBLIC_SITE_URL}/`;
|
||||
}
|
||||
}
|
||||
return <link key={href} rel="alternate" hrefLang={hrefLang} href={href}/>
|
||||
return <link key={href} rel="alternate" hrefLang={hrefLang} href={href} />
|
||||
})
|
||||
}
|
||||
{
|
||||
@ -49,7 +49,7 @@ const HeadInfo = ({
|
||||
}
|
||||
}
|
||||
if (locale == item.lang) {
|
||||
return <link key={href + 'canonical'} rel="canonical" hrefLang={hrefLang} href={href}/>
|
||||
return <link key={href + 'canonical'} rel="canonical" hrefLang={hrefLang} href={href} />
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1,25 +1,25 @@
|
||||
'use client'
|
||||
import {useState} from 'react'
|
||||
import {Dialog} from '@headlessui/react'
|
||||
import {Bars3Icon, XMarkIcon} from '@heroicons/react/24/outline'
|
||||
import {GlobeAltIcon} from '@heroicons/react/24/outline'
|
||||
import {Fragment} from 'react'
|
||||
import {Menu, Transition} from '@headlessui/react'
|
||||
import {ChevronDownIcon} from '@heroicons/react/20/solid'
|
||||
import { useState } from 'react'
|
||||
import { Dialog } from '@headlessui/react'
|
||||
import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline'
|
||||
import { GlobeAltIcon } from '@heroicons/react/24/outline'
|
||||
import { Fragment } from 'react'
|
||||
import { Menu, Transition } from '@headlessui/react'
|
||||
import { ChevronDownIcon } from '@heroicons/react/20/solid'
|
||||
import Link from "next/link";
|
||||
import {languages} from "~/config";
|
||||
import {useCommonContext} from '~/context/common-context'
|
||||
import { languages } from "~/i18n/config";
|
||||
import { useCommonContext } from '~/context/common-context'
|
||||
import LoadingModal from "./LoadingModal";
|
||||
import GeneratingModal from "~/components/GeneratingModal";
|
||||
import LoginButton from './LoginButton';
|
||||
import LoginModal from './LoginModal';
|
||||
import LogoutModal from "./LogoutModal";
|
||||
import {getLinkHref} from "~/configs/buildLink";
|
||||
import { getLinkHref } from "~/configs/buildLink";
|
||||
|
||||
export default function Header({
|
||||
locale,
|
||||
page
|
||||
}) {
|
||||
locale,
|
||||
page
|
||||
}) {
|
||||
const [mobileMenuOpen, setMobileMenuOpen] = useState(false)
|
||||
const {
|
||||
setShowLoadingModal,
|
||||
@ -47,8 +47,8 @@ export default function Header({
|
||||
|
||||
return (
|
||||
<header className="top-0 z-20 w-full">
|
||||
<LoadingModal loadingText={commonText.loadingText}/>
|
||||
<GeneratingModal generatingText={commonText.generateText}/>
|
||||
<LoadingModal loadingText={commonText.loadingText} />
|
||||
<GeneratingModal generatingText={commonText.generateText} />
|
||||
<LoginModal
|
||||
loadingText={commonText.loadingText}
|
||||
redirectPath={pageResult}
|
||||
@ -83,7 +83,7 @@ export default function Header({
|
||||
onClick={() => setMobileMenuOpen(true)}
|
||||
>
|
||||
<span className="sr-only">Open main menu</span>
|
||||
<Bars3Icon className="h-6 w-6" aria-hidden="true"/>
|
||||
<Bars3Icon className="h-6 w-6" aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="hidden lg:ml-14 lg:flex lg:flex-1 lg:gap-x-6">
|
||||
@ -121,8 +121,8 @@ export default function Header({
|
||||
<div>
|
||||
<Menu.Button
|
||||
className="inline-flex w-full justify-center gap-x-1.5 border border-[rgba(255,255,255,0.5)] rounded-md px-3 py-2 text-sm font-semibold text-white hover:border-[rgba(255,255,255,0.9)]">
|
||||
<GlobeAltIcon className="w-5 h-5 text-white"/>{locale == 'default' ? 'EN' : locale.toUpperCase()}
|
||||
<ChevronDownIcon className="-mr-1 h-5 w-5 text-white" aria-hidden="true"/>
|
||||
<GlobeAltIcon className="w-5 h-5 text-white" />{locale == 'default' ? 'EN' : locale.toUpperCase()}
|
||||
<ChevronDownIcon className="-mr-1 h-5 w-5 text-white" aria-hidden="true" />
|
||||
</Menu.Button>
|
||||
</div>
|
||||
<Transition
|
||||
@ -146,11 +146,11 @@ export default function Header({
|
||||
return (
|
||||
<Menu.Item key={item.lang}>
|
||||
<Link href={hrefValue} onClick={() => checkLocalAndLoading(item.lang)} className={"z-30"}>
|
||||
<span
|
||||
className={'text-gray-700 block px-4 py-2 text-sm hover:text-[#2d6ae0] z-30'}
|
||||
>
|
||||
{item.language}
|
||||
</span>
|
||||
<span
|
||||
className={'text-gray-700 block px-4 py-2 text-sm hover:text-[#2d6ae0] z-30'}
|
||||
>
|
||||
{item.language}
|
||||
</span>
|
||||
</Link>
|
||||
</Menu.Item>
|
||||
)
|
||||
@ -163,20 +163,20 @@ export default function Header({
|
||||
{
|
||||
process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != '0' ?
|
||||
<div className="hidden lg:ml-2 lg:relative lg:inline-block lg:text-left lg:text-white">
|
||||
<LoginButton buttonType={userData.email ? 1 : 0} loginText={authText.loginText}/>
|
||||
<LoginButton buttonType={userData.email ? 1 : 0} loginText={authText.loginText} />
|
||||
</div>
|
||||
:
|
||||
null
|
||||
}
|
||||
</nav>
|
||||
<Dialog as="div" className="lg:hidden" open={mobileMenuOpen} onClose={setMobileMenuOpen}>
|
||||
<div className="fixed inset-0 z-30"/>
|
||||
<div className="fixed inset-0 z-30" />
|
||||
<Dialog.Panel
|
||||
className="fixed inset-y-0 right-0 z-30 w-full overflow-y-auto background-div px-6 py-6 sm:max-w-sm sm:ring-1 sm:ring-gray-900/10">
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="flex">
|
||||
<Link href={getLinkHref(locale, '')} className="-m-1.5 ml-0.5 p-1.5"
|
||||
onClick={() => checkLocalAndLoading(locale)}>
|
||||
onClick={() => checkLocalAndLoading(locale)}>
|
||||
<img
|
||||
className="h-8 w-auto"
|
||||
src="/website.svg"
|
||||
@ -192,7 +192,7 @@ export default function Header({
|
||||
onClick={() => setMobileMenuOpen(false)}
|
||||
>
|
||||
<span className="sr-only">Close menu</span>
|
||||
<XMarkIcon className="h-6 w-6" aria-hidden="true"/>
|
||||
<XMarkIcon className="h-6 w-6" aria-hidden="true" />
|
||||
</button>
|
||||
</div>
|
||||
<div className="mt-6 flow-root">
|
||||
@ -233,8 +233,8 @@ export default function Header({
|
||||
<div>
|
||||
<Menu.Button
|
||||
className="inline-flex w-full justify-center gap-x-1.5 border border-[rgba(255,255,255,0.5)] rounded-md px-3 py-2 text-sm font-semibold text-white hover:border-[rgba(255,255,255,0.9)]">
|
||||
<GlobeAltIcon className="w-5 h-5 text-white"/>{locale == 'default' ? 'EN' : locale.toUpperCase()}
|
||||
<ChevronDownIcon className="-mr-1 h-5 w-5 text-white" aria-hidden="true"/>
|
||||
<GlobeAltIcon className="w-5 h-5 text-white" />{locale == 'default' ? 'EN' : locale.toUpperCase()}
|
||||
<ChevronDownIcon className="-mr-1 h-5 w-5 text-white" aria-hidden="true" />
|
||||
</Menu.Button>
|
||||
</div>
|
||||
<Transition
|
||||
@ -258,11 +258,11 @@ export default function Header({
|
||||
return (
|
||||
<Menu.Item key={item.lang}>
|
||||
<Link href={hrefValue} onClick={() => checkLocalAndLoading(item.lang)}>
|
||||
<span
|
||||
className={'text-gray-700 block px-4 py-2 text-sm hover:text-[#2d6ae0]'}
|
||||
>
|
||||
{item.language}
|
||||
</span>
|
||||
<span
|
||||
className={'text-gray-700 block px-4 py-2 text-sm hover:text-[#2d6ae0]'}
|
||||
>
|
||||
{item.language}
|
||||
</span>
|
||||
</Link>
|
||||
</Menu.Item>
|
||||
)
|
||||
@ -277,7 +277,7 @@ export default function Header({
|
||||
process.env.NEXT_PUBLIC_CHECK_GOOGLE_LOGIN != '0' ?
|
||||
<div
|
||||
className="relative inline-block text-left text-base font-semibold text-white ml-2">
|
||||
<LoginButton buttonType={userData.email ? 1 : 0} loginText={authText.loginText}/>
|
||||
<LoginButton buttonType={userData.email ? 1 : 0} loginText={authText.loginText} />
|
||||
</div>
|
||||
:
|
||||
null
|
||||
|
@ -1,34 +0,0 @@
|
||||
import {Pathnames} from 'next-intl/navigation';
|
||||
|
||||
export const locales = ['en', 'zh'] as const;
|
||||
|
||||
export const languages = [
|
||||
{
|
||||
code: "en-US",
|
||||
lang: "en",
|
||||
language: "English",
|
||||
},
|
||||
{
|
||||
code: "zh-CN",
|
||||
lang: "zh",
|
||||
language: "简体中文",
|
||||
},
|
||||
]
|
||||
|
||||
export const pathnames = {
|
||||
'/': '/',
|
||||
} satisfies Pathnames<typeof locales>;
|
||||
|
||||
// Use the default: `always`,设置为 as-needed可不显示默认路由
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
10
src/i18n.ts
10
src/i18n.ts
@ -1,10 +0,0 @@
|
||||
import {getRequestConfig} from 'next-intl/server';
|
||||
|
||||
export default getRequestConfig(async ({locale}) => ({
|
||||
messages: (
|
||||
await (locale === 'en'
|
||||
? // When using Turbopack, this will enable HMR for `default`
|
||||
import('../messages/en.json')
|
||||
: import(`../messages/${locale}.json`))
|
||||
).default
|
||||
}));
|
22
src/i18n/config.ts
Normal file
22
src/i18n/config.ts
Normal file
@ -0,0 +1,22 @@
|
||||
export const locales = ["en", "zh"] as const;
|
||||
|
||||
export const languages = [
|
||||
{
|
||||
code: "en-US",
|
||||
lang: "en",
|
||||
language: "English",
|
||||
},
|
||||
{
|
||||
code: "zh-CN",
|
||||
lang: "zh",
|
||||
language: "简体中文",
|
||||
},
|
||||
];
|
||||
|
||||
export const getLanguageByLang = (lang) => {
|
||||
for (let i = 0; i < languages.length; i++) {
|
||||
if (lang == languages[i].lang) {
|
||||
return languages[i];
|
||||
}
|
||||
}
|
||||
};
|
4
src/i18n/navigation.ts
Normal file
4
src/i18n/navigation.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { createNavigation } from "next-intl/navigation";
|
||||
import { routing } from "./routing";
|
||||
|
||||
export const { Link, redirect, usePathname, useRouter, getPathname } = createNavigation(routing);
|
17
src/i18n/request.ts
Normal file
17
src/i18n/request.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { getRequestConfig } from "next-intl/server";
|
||||
import { routing } from "./routing";
|
||||
|
||||
export default getRequestConfig(async ({ requestLocale }) => {
|
||||
// This typically corresponds to the `[locale]` segment
|
||||
let locale = await requestLocale;
|
||||
|
||||
// Ensure that a valid locale is used
|
||||
if (!locale || !routing.locales.includes(locale as any)) {
|
||||
locale = routing.defaultLocale;
|
||||
}
|
||||
|
||||
return {
|
||||
locale,
|
||||
messages: (await import(`../../messages/${locale}.json`)).default,
|
||||
};
|
||||
});
|
13
src/i18n/routing.ts
Normal file
13
src/i18n/routing.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { defineRouting } from "next-intl/routing";
|
||||
import { locales } from "./config";
|
||||
|
||||
export const routing = defineRouting({
|
||||
// A list of all locales that are supported
|
||||
locales: locales,
|
||||
// Used when no locale matches
|
||||
defaultLocale: "en",
|
||||
// Use the default: `always`,设置为 as-needed可不显示默认路由
|
||||
localePrefix: "as-needed",
|
||||
localeDetection: false,
|
||||
alternateLinks: false,
|
||||
});
|
@ -1,26 +1,19 @@
|
||||
import createMiddleware from 'next-intl/middleware';
|
||||
import {pathnames, locales, localePrefix} from './config';
|
||||
import createMiddleware from "next-intl/middleware";
|
||||
import { routing } from "./i18n/routing";
|
||||
|
||||
export default createMiddleware({
|
||||
defaultLocale: 'en',
|
||||
locales,
|
||||
pathnames,
|
||||
localePrefix,
|
||||
localeDetection: false,
|
||||
alternateLinks: false
|
||||
});
|
||||
export default createMiddleware(routing);
|
||||
|
||||
export const config = {
|
||||
matcher: [
|
||||
// Enable a redirect to a matching locale at the root
|
||||
'/',
|
||||
"/",
|
||||
|
||||
// Set a cookie to remember the previous locale for
|
||||
// all requests that have a locale prefix
|
||||
'/(en|zh)/:path*',
|
||||
"/(en|zh)/:path*",
|
||||
|
||||
// Enable redirects that add missing locales
|
||||
// (e.g. `/pathnames` -> `/en/pathnames`)
|
||||
'/((?!_next|_vercel|.*\\..*).*)'
|
||||
]
|
||||
"/((?!_next|_vercel|.*\\..*).*)",
|
||||
],
|
||||
};
|
||||
|
@ -1,9 +0,0 @@
|
||||
import {createLocalizedPathnamesNavigation} from 'next-intl/navigation';
|
||||
import {locales, pathnames, localePrefix} from './config';
|
||||
|
||||
export const {Link, redirect, usePathname, useRouter} =
|
||||
createLocalizedPathnamesNavigation({
|
||||
locales,
|
||||
pathnames,
|
||||
localePrefix
|
||||
});
|
@ -1,8 +0,0 @@
|
||||
{
|
||||
"crons": [
|
||||
{
|
||||
"path": "/api/cron/workTranslate",
|
||||
"schedule": "*/1 * * * *"
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user