Skip to content

Commit

Permalink
refactor: set lang attribute for html tag (#6536)
Browse files Browse the repository at this point in the history
* refactor: set `lang` attribute for html tag

* refactor: use shared i18next instance

* refactor: align html attr usage
  • Loading branch information
gao-sun authored Sep 9, 2024
1 parent b837efe commit 3b9714b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
7 changes: 7 additions & 0 deletions .changeset/quiet-penguins-join.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@logto/experience": patch
"@logto/demo-app": patch
"@logto/console": patch
---

set `lang` attribute for `<html>`
12 changes: 11 additions & 1 deletion packages/console/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
TenantScope,
} from '@logto/schemas';
import { conditionalArray } from '@silverhand/essentials';
import i18next from 'i18next';
import { useContext, useMemo } from 'react';
import { Helmet } from 'react-helmet';
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
Expand Down Expand Up @@ -114,7 +115,16 @@ function Providers() {
}}
>
<AppThemeProvider>
<Helmet titleTemplate={`%s - ${mainTitle}`} defaultTitle={mainTitle} />
<Helmet
titleTemplate={`%s - ${mainTitle}`}
defaultTitle={mainTitle}
htmlAttributes={{
// We intentionally use the imported i18next instance instead of the hook, since the
// hook will cause a re-render following some bugs here. This still works for the
// initial render, so we're good for now. Consider refactoring this in the future.
lang: i18next.language,
}}
/>
<Toast />
<AppConfirmModalProvider>
<ErrorBoundary>
Expand Down
2 changes: 2 additions & 0 deletions packages/demo-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@silverhand/ts-config-react": "6.0.0",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@types/react-helmet": "^6.1.6",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^8.56.0",
"i18next": "^22.4.15",
Expand All @@ -40,6 +41,7 @@
"prettier": "^3.0.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-helmet": "^6.1.0",
"react-i18next": "^12.3.1",
"stylelint": "^15.0.0",
"typescript": "^5.5.3",
Expand Down
10 changes: 10 additions & 0 deletions packages/demo-app/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { type IdTokenClaims, LogtoProvider, useLogto, type Prompt } from '@logto/react';
import { demoAppApplicationId } from '@logto/schemas';
import i18next from 'i18next';
import { useCallback, useEffect, useState } from 'react';
import { Helmet } from 'react-helmet';
import { useTranslation } from 'react-i18next';

import '@/scss/normalized.scss';
Expand Down Expand Up @@ -121,6 +123,14 @@ const Main = () => {

return (
<div className={styles.app}>
<Helmet
htmlAttributes={{
// We intentionally use the imported i18next instance instead of the hook, since the hook
// will cause a re-render following some bugs here. This still works for the initial
// render, so we're good for now. Consider refactoring this in the future.
lang: i18next.language,
}}
/>
{showDevPanel && <DevPanel />}
<div className={[styles.card, styles.congrats].join(' ')}>
{congratsIcon && <img src={congratsIcon} alt="Congrats" />}
Expand Down
11 changes: 10 additions & 1 deletion packages/experience/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { MfaFactor, experience } from '@logto/schemas';
import i18next from 'i18next';
import { Helmet } from 'react-helmet';
import { Route, Routes, BrowserRouter } from 'react-router-dom';

import AppLayout from './Layout/AppLayout';
Expand Down Expand Up @@ -39,14 +41,21 @@ import Springboard from './pages/Springboard';
import VerificationCode from './pages/VerificationCode';
import { UserMfaFlow } from './types';
import { handleSearchParametersData } from './utils/search-parameters';

import './scss/normalized.scss';

handleSearchParametersData();

const App = () => {
return (
<BrowserRouter>
<Helmet
htmlAttributes={{
// We intentionally use the imported i18next instance instead of the hook, since the hook
// will cause a re-render following some bugs here. This still works for the initial
// render, so we're good for now. Consider refactoring this in the future.
lang: i18next.language,
}}
/>
<PageContextProvider>
<SettingsProvider>
<UserInteractionContextProvider>
Expand Down
2 changes: 0 additions & 2 deletions packages/experience/src/Providers/AppBoundary/AppMeta.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Theme } from '@logto/schemas';
import { conditionalString } from '@silverhand/essentials';
import classNames from 'classnames';
import i18next from 'i18next';
import { useContext } from 'react';
import { Helmet } from 'react-helmet';

Expand Down Expand Up @@ -38,7 +37,6 @@ const AppMeta = () => {

return (
<Helmet>
<html lang={i18next.language} data-theme={theme} />
<link rel="shortcut icon" href={favicon ?? defaultFavicon} />
<link rel="apple-touch-icon" href={favicon ?? defaultAppleTouchLogo} sizes="180x180" />
{experienceSettings?.customCss && <style>{experienceSettings.customCss}</style>}
Expand Down
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3b9714b

Please sign in to comment.