Przeglądaj źródła

add type Locale

maeshinshin 1 rok temu
rodzic
commit
08361668f8

+ 3 - 5
apps/app/config/next-i18next.config.js

@@ -2,11 +2,9 @@ const isDev = process.env.NODE_ENV === 'development';
 
 
 const path = require('path');
 const path = require('path');
 
 
-const { AllLang } = require('@growi/core');
+const { Locale, AllLocale } = require('@growi/core');
 const { isServer } = require('@growi/core/dist/utils');
 const { isServer } = require('@growi/core/dist/utils');
 
 
-const { defaultLang } = require('./i18next.config');
-
 const HMRPlugin = isDev ? require('i18next-hmr/plugin').HMRPlugin : undefined;
 const HMRPlugin = isDev ? require('i18next-hmr/plugin').HMRPlugin : undefined;
 
 
 /** @type {import('next-i18next').UserConfig} */
 /** @type {import('next-i18next').UserConfig} */
@@ -14,8 +12,8 @@ module.exports = {
   ...require('./i18next.config').initOptions,
   ...require('./i18next.config').initOptions,
 
 
   i18n: {
   i18n: {
-    defaultLocale: defaultLang.toString(),
-    locales: AllLang,
+    defaultLocale: Locale['en-US'],
+    locales: AllLocale,
   },
   },
 
 
   localePath: path.resolve('./public/static/locales'),
   localePath: path.resolve('./public/static/locales'),

+ 13 - 9
apps/app/src/pages/utils/commons.ts

@@ -1,5 +1,5 @@
 import type { ColorScheme, IUserHasId } from '@growi/core';
 import type { ColorScheme, IUserHasId } from '@growi/core';
-import { Lang, AllLang } from '@growi/core';
+import { Lang, AllLang, Locale } from '@growi/core';
 import { DevidedPagePath } from '@growi/core/dist/models';
 import { DevidedPagePath } from '@growi/core/dist/models';
 import { isServer } from '@growi/core/dist/utils';
 import { isServer } from '@growi/core/dist/utils';
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
@@ -105,17 +105,21 @@ export const getServerSideCommonProps: GetServerSideProps<CommonProps> = async(c
   return { props };
   return { props };
 };
 };
 
 
-export const getLocaleAtServerSide = (req: CrowiRequest): 'ja-jp' | 'en-us' | 'zh-cn' | 'fr-fr' => {
+export type LangMap = {
+  readonly [key in Lang]: Locale;
+};
+
+export const langMap: LangMap = {
+  [Lang.ja_JP]: Locale['ja-JP'],
+  [Lang.en_US]: Locale['en-US'],
+  [Lang.zh_CN]: Locale['zh-CN'],
+  [Lang.fr_FR]: Locale['fr-FR'],
+};
+
+export const getLocaleAtServerSide = (req: CrowiRequest): Locale => {
   const { user, headers } = req;
   const { user, headers } = req;
   const { configManager } = req.crowi;
   const { configManager } = req.crowi;
 
 
-  const langMap = {
-    [Lang.ja_JP]: 'ja-jp',
-    [Lang.en_US]: 'en-us',
-    [Lang.zh_CN]: 'zh-cn',
-    [Lang.fr_FR]: 'fr-fr',
-  } as const;
-
   return langMap[user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
   return langMap[user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
     : (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US) ?? Lang.en_US];
     : (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US) ?? Lang.en_US];
 };
 };

+ 1 - 0
packages/core/src/interfaces/index.ts

@@ -7,6 +7,7 @@ export * from './growi-facade';
 export * from './growi-theme-metadata';
 export * from './growi-theme-metadata';
 export * from './has-object-id';
 export * from './has-object-id';
 export * from './lang';
 export * from './lang';
+export * from './locale';
 export * from './page';
 export * from './page';
 export * from './revision';
 export * from './revision';
 export * from './subscription';
 export * from './subscription';

+ 8 - 0
packages/core/src/interfaces/locale.ts

@@ -0,0 +1,8 @@
+export const Locale = {
+  'en-US': 'en-US',
+  'ja-JP': 'ja-JP',
+  'zh-CN': 'zh-CN',
+  'fr-FR': 'fr-FR',
+} as const;
+export const AllLocale = Object.values(Locale);
+export type Locale = typeof Locale[keyof typeof Locale];