Skip to content

Commit

Permalink
feat: language automatic detection (#4114)
Browse files Browse the repository at this point in the history
Signed-off-by: jingyang <3161362058@qq.com>
  • Loading branch information
zjy365 committed Oct 19, 2023
1 parent ffba350 commit b6ef217
Show file tree
Hide file tree
Showing 26 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion frontend/desktop/next-i18next.config.js
Expand Up @@ -4,7 +4,7 @@

module.exports = {
i18n: {
defaultLocale: 'en',
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
Expand Down
2 changes: 2 additions & 0 deletions frontend/desktop/src/pages/index.tsx
Expand Up @@ -107,6 +107,8 @@ export default function Home({
export async function getServerSideProps({ req, res, locales }: any) {
const lang: string = req?.headers?.['accept-language'] || 'zh';
const local = lang.indexOf('zh') !== -1 ? 'zh' : 'en';
res.setHeader('Set-Cookie', `NEXT_LOCALE=${local}; Max-Age=2592000; Secure; SameSite=None`);

const sealos_cloud_domain = process.env.SEALOS_CLOUD_DOMAIN || 'cloud.sealos.io';

return {
Expand Down
2 changes: 1 addition & 1 deletion frontend/desktop/src/pages/license/index.tsx
Expand Up @@ -65,7 +65,7 @@ export default function LicensePage() {
}

export async function getServerSideProps({ req, res, locales }: any) {
const local = req?.cookies?.NEXT_LOCALE || 'en';
const local = req?.cookies?.NEXT_LOCALE || 'zh';

return {
props: {
Expand Down
1 change: 1 addition & 0 deletions frontend/desktop/src/pages/signin.tsx
Expand Up @@ -8,6 +8,7 @@ export default function SigninPage() {
export async function getServerSideProps({ req, res, locales }: any) {
const lang: string = req?.headers?.['accept-language'] || 'zh';
const local = lang.indexOf('zh') !== -1 ? 'zh' : 'en';
res.setHeader('Set-Cookie', `NEXT_LOCALE=${local}; Max-Age=2592000; Secure; SameSite=None`);

const props = {
...(await serverSideTranslations(local, undefined, null, locales || []))
Expand Down
2 changes: 1 addition & 1 deletion frontend/packages/client-sdk/src/master.ts
Expand Up @@ -194,7 +194,7 @@ class MasterSDK {
messageId: data.messageId,
success: true,
data: {
lng: getCookie('NEXT_LOCALE') || 'en'
lng: getCookie('NEXT_LOCALE') || 'zh'
}
});
} else {
Expand Down
4 changes: 2 additions & 2 deletions frontend/providers/applaunchpad/next-i18next.config.js
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'en',
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
};
}
4 changes: 2 additions & 2 deletions frontend/providers/applaunchpad/src/pages/_app.tsx
Expand Up @@ -99,7 +99,7 @@ const App = ({ Component, pageProps }: AppProps) => {
}, [setScreenWidth]);

useEffect(() => {
const changeI18n = async (data: any) => {
const changeI18n = async (data: { currentLanguage: string }) => {
const lastLang = getLangStore();
const newLang = data.currentLanguage;
if (lastLang !== newLang) {
Expand Down Expand Up @@ -132,7 +132,7 @@ const App = ({ Component, pageProps }: AppProps) => {
}, [router.pathname]);

useEffect(() => {
const lang = getLangStore() || 'en';
const lang = getLangStore() || 'zh';
i18n?.changeLanguage?.(lang);
}, [refresh, router.pathname]);

Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/applaunchpad/src/utils/cookieUtils.ts
@@ -1,6 +1,6 @@
import Cookies from 'js-cookie';

export const LANG_KEY = 'NEXT_LOCALE_LANG';
export const LANG_KEY = 'NEXT_LOCALE';

export const setLangStore = (value: string) => {
return Cookies.set(LANG_KEY, value, { expires: 30, sameSite: 'None', secure: true });
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/applaunchpad/src/utils/i18n.ts
Expand Up @@ -3,7 +3,7 @@ import { LANG_KEY } from './cookieUtils';

export const serviceSideProps = (content: any) => {
return serverSideTranslations(
content.req.cookies[LANG_KEY] || 'en',
content.req.cookies[LANG_KEY] || 'zh',
undefined,
null,
content.locales
Expand Down
6 changes: 3 additions & 3 deletions frontend/providers/costcenter/next-i18next.config.js
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'zh', 'zh-Hans'],
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
};
}
2 changes: 1 addition & 1 deletion frontend/providers/costcenter/src/stores/session.ts
Expand Up @@ -21,7 +21,7 @@ const useSessionStore = create<SessionState>()(
persist(
immer((set, get) => ({
session: {} as SessionV1,
locale: 'en',
locale: 'zh',
setSession: (ss: SessionV1) => set({ session: ss }),
setSessionProp: (key: keyof SessionV1, value: any) => {
set((state) => {
Expand Down
4 changes: 2 additions & 2 deletions frontend/providers/cronjob/next-i18next.config.js
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'en',
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
};
}
2 changes: 1 addition & 1 deletion frontend/providers/cronjob/src/pages/_app.tsx
Expand Up @@ -121,7 +121,7 @@ function App({ Component, pageProps }: AppProps) {
}, [router.pathname]);

useEffect(() => {
const lang = getLangStore() || 'en';
const lang = getLangStore() || 'zh';
i18n?.changeLanguage?.(lang);
}, [refresh, router.asPath]);

Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/cronjob/src/utils/cookieUtils.ts
@@ -1,6 +1,6 @@
import Cookies from 'js-cookie';

export const LANG_KEY = 'NEXT_LOCALE_LANG';
export const LANG_KEY = 'NEXT_LOCALE';

export const setLangStore = (value: string) => {
return Cookies.set(LANG_KEY, value, { expires: 30, sameSite: 'None', secure: true });
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/cronjob/src/utils/i18n.ts
Expand Up @@ -3,7 +3,7 @@ import { LANG_KEY } from './cookieUtils';

export const serviceSideProps = (content: any) => {
return serverSideTranslations(
content.req.cookies[LANG_KEY] || 'en',
content.req.cookies[LANG_KEY] || 'zh',
undefined,
null,
content.locales
Expand Down
6 changes: 3 additions & 3 deletions frontend/providers/dbprovider/next-i18next.config.js
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'zh', 'zh-Hans'],
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
};
}
2 changes: 1 addition & 1 deletion frontend/providers/dbprovider/src/pages/_app.tsx
Expand Up @@ -126,7 +126,7 @@ function App({ Component, pageProps }: AppProps) {
}, [router.pathname]);

useEffect(() => {
const lang = getLangStore() || 'en';
const lang = getLangStore() || 'zh';
i18n?.changeLanguage?.(lang);
}, [refresh, router.asPath]);

Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/dbprovider/src/utils/cookieUtils.ts
@@ -1,6 +1,6 @@
import Cookies from 'js-cookie';

export const LANG_KEY = 'NEXT_LOCALE_LANG';
export const LANG_KEY = 'NEXT_LOCALE';

export const setLangStore = (value: string) => {
return Cookies.set(LANG_KEY, value, { expires: 30, sameSite: 'None', secure: true });
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/dbprovider/src/utils/i18n.ts
Expand Up @@ -3,7 +3,7 @@ import { LANG_KEY } from './cookieUtils';

export const serviceSideProps = (content: any) => {
return serverSideTranslations(
content.req.cookies[LANG_KEY] || 'en',
content.req.cookies[LANG_KEY] || 'zh',
undefined,
null,
content.locales
Expand Down
4 changes: 2 additions & 2 deletions frontend/providers/license/next-i18next.config.js
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'en',
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
};
}
2 changes: 1 addition & 1 deletion frontend/providers/license/src/pages/_app.tsx
Expand Up @@ -121,7 +121,7 @@ function App({ Component, pageProps }: AppProps) {
}, [router.pathname]);

useEffect(() => {
const lang = getLangStore() || 'en';
const lang = getLangStore() || 'zh';
i18n?.changeLanguage?.(lang);
}, [refresh, router.asPath]);

Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/license/src/utils/cookieUtils.ts
@@ -1,6 +1,6 @@
import Cookies from 'js-cookie';

export const LANG_KEY = 'NEXT_LOCALE_LANG';
export const LANG_KEY = 'NEXT_LOCALE';

export const setLangStore = (value: string) => {
return Cookies.set(LANG_KEY, value, { expires: 30, sameSite: 'None', secure: true });
Expand Down
6 changes: 3 additions & 3 deletions frontend/providers/template/next-i18next.config.js
Expand Up @@ -5,8 +5,8 @@

module.exports = {
i18n: {
defaultLocale: 'en',
locales: ['en', 'zh', 'zh-Hans'],
defaultLocale: 'zh',
locales: ['en', 'zh'],
localeDetection: false
}
};
}
2 changes: 1 addition & 1 deletion frontend/providers/template/src/pages/_app.tsx
Expand Up @@ -113,7 +113,7 @@ const App = ({ Component, pageProps, domain }: AppProps & { domain: string }) =>
}, [router.pathname]);

useEffect(() => {
const lang = getLangStore() || 'en';
const lang = getLangStore() || 'zh';
i18n?.changeLanguage?.(lang);
}, [refresh, router.pathname]);

Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/template/src/utils/cookieUtils.ts
@@ -1,6 +1,6 @@
import Cookies from 'js-cookie';

export const LANG_KEY = 'NEXT_LOCALE_LANG';
export const LANG_KEY = 'NEXT_LOCALE';

export const setLangStore = (value: string) => {
return Cookies.set(LANG_KEY, value, { expires: 30, sameSite: 'None', secure: true });
Expand Down
2 changes: 1 addition & 1 deletion frontend/providers/template/src/utils/i18n.ts
Expand Up @@ -3,7 +3,7 @@ import { LANG_KEY } from './cookieUtils';

export const serviceSideProps = (content: any) => {
return serverSideTranslations(
content.req.cookies[LANG_KEY] || 'en',
content.req.cookies[LANG_KEY] || 'zh',
undefined,
null,
content.locales
Expand Down

0 comments on commit b6ef217

Please sign in to comment.