From 965989688f954523633918b59b9546d706b4dfaf Mon Sep 17 00:00:00 2001 From: Ethan Zhang Date: Wed, 16 Oct 2024 17:58:05 -0700 Subject: [PATCH] Issues/214 (#338) * feat: set language based on browser setting * feat: update image --- demo/src/app/home/page.tsx | 6 +++++- demo/src/common/utils.ts | 32 +++++++++++++++++++++++++++++++- docker-compose.yml | 2 +- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/demo/src/app/home/page.tsx b/demo/src/app/home/page.tsx index a94b9ba9..e0549df0 100644 --- a/demo/src/app/home/page.tsx +++ b/demo/src/app/home/page.tsx @@ -1,9 +1,10 @@ "use client" import AuthInitializer from "@/components/authInitializer" -import { isMobile } from "@/common" +import { getBrowserLanguage, isMobile, useAppDispatch } from "@/common" import dynamic from 'next/dynamic' import { useEffect, useState } from "react" +import { setLanguage } from "@/store/reducers/global" const PCEntry = dynamic(() => import('@/platform/pc/entry'), { ssr: false, @@ -15,9 +16,12 @@ const MobileEntry = dynamic(() => import('@/platform/mobile/entry'), { export default function Home() { const [mobile, setMobile] = useState(null); + const dispatch = useAppDispatch() + useEffect(() => { setMobile(isMobile()) + dispatch(setLanguage(getBrowserLanguage().value)) }) return ( diff --git a/demo/src/common/utils.ts b/demo/src/common/utils.ts index 1d6f0d00..96bf8e4d 100644 --- a/demo/src/common/utils.ts +++ b/demo/src/common/utils.ts @@ -1,3 +1,5 @@ +import { LanguageOptionItem } from "@/types" + export const genRandomString = (length: number = 10) => { let result = ''; const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'; @@ -56,4 +58,32 @@ export const genUUID = () => { export const isMobile = () => { return /Mobile|iPhone|iPad|Android|Windows Phone/i.test(navigator.userAgent) -} \ No newline at end of file +} + + +export const getBrowserLanguage = (): LanguageOptionItem => { + const lang = navigator.language; + + switch (true) { + case lang.startsWith("zh"): + return { + label: "Chinese", + value: "zh-CN" + }; + case lang.startsWith("ko"): + return { + label: "Korean", + value: "ko-KR" + }; + case lang.startsWith("ja"): + return { + label: "Japanese", + value: "ja-JP" + }; + default: + return { + label: "English", + value: "en-US" + }; + } +}; \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index a6da3f87..bb5160d2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,7 +30,7 @@ services: - AGENT_SERVER_URL=http://ten_agent_dev:8080 - TEN_DEV_SERVER_URL=http://ten_agent_dev:49483 ten_agent_demo: - image: ghcr.io/ten-framework/agent_demo + image: ghcr.io/ten-framework/ten_agent_demo:0.5.0-43-ga6ea980 container_name: ten_agent_demo restart: always ports: