ryoji-s 1 год назад
Родитель
Сommit
b35c40ed23

+ 7 - 4
apps/app/src/pages/utils/commons.ts

@@ -1,5 +1,5 @@
 import type { ColorScheme, IUserHasId } from '@growi/core';
-import { AllLang } from '@growi/core';
+import { AllLang, Lang } from '@growi/core';
 import { DevidedPagePath } from '@growi/core/dist/models';
 import { isServer } from '@growi/core/dist/utils';
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
@@ -8,6 +8,7 @@ import type { SSRConfig, UserConfig } from 'next-i18next';
 
 import * as nextI18NextConfig from '^/config/next-i18next.config';
 
+import { detectLocaleFromBrowserAcceptLanguage } from '~/client/util/locale-utils';
 import { type SupportedActionType } from '~/interfaces/activity';
 import type { CrowiRequest } from '~/interfaces/crowi-request';
 import type { ISidebarConfig } from '~/interfaces/sidebar-config';
@@ -17,7 +18,6 @@ import type { UserUISettingsDocument } from '~/server/models/user-ui-settings';
 import {
   useCurrentProductNavWidth, useCurrentSidebarContents, usePreferCollapsedMode,
 } from '~/stores/ui';
-import { determineLocale } from '~/utils/locale-utils';
 
 export type CommonProps = {
   namespacesRequired: string[], // i18next
@@ -120,9 +120,12 @@ export const getNextI18NextConfig = async(
 ): Promise<SSRConfig> => {
 
   const req: CrowiRequest = context.req as CrowiRequest;
-  const { user, headers } = req;
+  const { crowi, user, headers } = req;
+  const { configManager } = crowi;
 
-  const locale = determineLocale(headers, user);
+  // determine language
+  const locale = user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
+    : (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US);
 
   const namespaces = ['commons'];
   if (namespacesRequired != null) {

+ 8 - 9
apps/app/src/server/routes/apiv3/page/create-page.ts

@@ -1,6 +1,6 @@
 import { allOrigin } from '@growi/core';
 import type {
-  IPage, IUser, IUserHasId, Lang,
+  IPage, IUser, IUserHasId,
 } from '@growi/core';
 import { ErrorV3 } from '@growi/core/dist/models';
 import { isCreatablePage, isUserPage, isUsersHomepage } from '@growi/core/dist/utils/page-path-utils';
@@ -23,7 +23,6 @@ import {
 import type { PageDocument, PageModel } from '~/server/models/page';
 import PageTagRelation from '~/server/models/page-tag-relation';
 import { configManager } from '~/server/service/config-manager';
-import { determineLocale } from '~/utils/locale-utils';
 import loggerFactory from '~/utils/logger';
 
 import { apiV3FormValidator } from '../../../middlewares/apiv3-form-validator';
@@ -43,10 +42,8 @@ async function generateUntitledPath(parentPath: string, basePathname: string, in
   return path;
 }
 
-async function determinePath(locale: Lang, _parentPath?: string, _path?: string, optionalParentPath?: string): Promise<string> {
-  // const t = i18n?.getFixedT(locale);
-  // const basePathname = t?.('create_page.untitled') || 'Undefined';
-  const basePathname = 'Undefined';
+async function determinePath(t: any, _parentPath?: string, _path?: string, optionalParentPath?: string): Promise<string> {
+  const basePathname = t('create_page.untitled');
 
   if (_path != null) {
     const path = normalizePath(_path);
@@ -92,6 +89,10 @@ type ReqBody = IApiv3PageCreateParams
 
 interface CreatePageRequest extends Request<undefined, ApiV3Response, ReqBody> {
   user: IUserHasId,
+
+  // TODO: remove req.t
+  // https://redmine.weseek.co.jp/issues/125884
+  t: any
 }
 
 type CreatePageHandlersFactory = (crowi: Crowi) => RequestHandler[];
@@ -214,9 +215,7 @@ export const createPageHandlersFactory: CreatePageHandlersFactory = (crowi) => {
       try {
         const { path, parentPath, optionalParentPath } = req.body;
 
-        const locale = determineLocale(req.headers, req.user);
-
-        pathToCreate = await determinePath(locale, parentPath, path, optionalParentPath);
+        pathToCreate = await determinePath(req.t, parentPath, path, optionalParentPath);
       }
       catch (err) {
         return res.apiv3Err(new ErrorV3(err.toString(), 'could_not_create_page'));