Skip to content

Commit

Permalink
Merge pull request #8940 from weseek/imprv/147666-149169-html-lang-at…
Browse files Browse the repository at this point in the history
…tribute

imprv: lang attribute in Html element to correctly reflect locale
  • Loading branch information
yuki-takei authored Jul 5, 2024
2 parents 92ff2cc + 130bd54 commit a425e10
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
23 changes: 21 additions & 2 deletions apps/app/src/pages/_document.page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
/* eslint-disable @next/next/google-font-display */
import React from 'react';

import { Lang } from '@growi/core';
import type { DocumentContext, DocumentInitialProps } from 'next/document';
import Document, {
Html, Head, Main, NextScript,
} from 'next/document';

import type { GrowiPluginResourceEntries } from '~/features/growi-plugin/server/services';
import type { CrowiRequest } from '~/interfaces/crowi-request';
import { configManager } from '~/server/service/config-manager';
import { detectLocaleFromBrowserAcceptLanguage } from '~/server/util/locale-utils';
import loggerFactory from '~/utils/logger';

import { getLocateAtServerSide } from './utils/commons';

const logger = loggerFactory('growi:page:_document');

type HeadersForGrowiPluginProps = {
Expand Down Expand Up @@ -41,14 +46,24 @@ interface GrowiDocumentProps {
customCss: string | null,
customNoscript: string | null,
pluginResourceEntries: GrowiPluginResourceEntries;
locale: string;
}
declare type GrowiDocumentInitialProps = DocumentInitialProps & GrowiDocumentProps;

class GrowiDocument extends Document<GrowiDocumentInitialProps> {

static override async getInitialProps(ctx: DocumentContext): Promise<GrowiDocumentInitialProps> {

const langMap = {
[Lang.ja_JP]: 'ja-jp',
[Lang.en_US]: 'en-us',
[Lang.zh_CN]: 'zh-cn',
[Lang.fr_FR]: 'fr-fr',
} as const;

const initialProps: DocumentInitialProps = await Document.getInitialProps(ctx);
const { crowi } = ctx.req as CrowiRequest;
const req = ctx.req as CrowiRequest;
const { crowi } = req;
const { customizeService } = crowi;

const { themeHref } = customizeService;
Expand All @@ -60,13 +75,16 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
const growiPluginService = await import('~/features/growi-plugin/server/services').then(mod => mod.growiPluginService);
const pluginResourceEntries = await growiPluginService.retrieveAllPluginResourceEntries();

const locale = langMap[getLocateAtServerSide(req)];

return {
...initialProps,
themeHref,
customScript,
customCss,
customNoscript,
pluginResourceEntries,
locale,
};
}

Expand Down Expand Up @@ -95,10 +113,11 @@ class GrowiDocument extends Document<GrowiDocumentInitialProps> {
const {
customCss, customScript, customNoscript,
themeHref, pluginResourceEntries,
locale,
} = this.props;

return (
<Html>
<Html lang={locale}>
<Head>
{this.renderCustomScript(customScript)}
<link rel="stylesheet" key="link-theme" href={themeHref} />
Expand Down
17 changes: 11 additions & 6 deletions apps/app/src/pages/utils/commons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ export const getServerSideCommonProps: GetServerSideProps<CommonProps> = async(c
return { props };
};


export const getLocateAtServerSide = (req: CrowiRequest): Lang => {
const { user, headers } = req;
const { configManager } = req.crowi;

return user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
: (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US);
};

export const getNextI18NextConfig = async(
// 'serverSideTranslations' method should be given from Next.js Page
// because importing it in this file causes https://github.com/isaachinman/next-i18next/issues/1545
Expand All @@ -115,13 +124,9 @@ export const getNextI18NextConfig = async(
context: GetServerSidePropsContext, namespacesRequired?: string[] | undefined, preloadAllLang = false,
): Promise<SSRConfig> => {

const req: CrowiRequest = context.req as CrowiRequest;
const { crowi, user, headers } = req;
const { configManager } = crowi;

// determine language
const locale = user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
: (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US);
const req: CrowiRequest = context.req as CrowiRequest;
const locale = getLocateAtServerSide(req);

const namespaces = ['commons'];
if (namespacesRequired != null) {
Expand Down

0 comments on commit a425e10

Please sign in to comment.