|
|
@@ -1,5 +1,5 @@
|
|
|
-import type { ColorScheme, IUserHasId } from '@growi/core';
|
|
|
-import { Lang, AllLang, Locale } from '@growi/core';
|
|
|
+import type { ColorScheme, IUserHasId, Locale } from '@growi/core';
|
|
|
+import { Lang, AllLang } from '@growi/core';
|
|
|
import { DevidedPagePath } from '@growi/core/dist/models';
|
|
|
import { isServer } from '@growi/core/dist/utils';
|
|
|
import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
|
|
|
@@ -110,18 +110,24 @@ export type LangMap = {
|
|
|
};
|
|
|
|
|
|
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 => {
|
|
|
+ [Lang.ja_JP]: 'ja-JP',
|
|
|
+ [Lang.en_US]: 'en-US',
|
|
|
+ [Lang.zh_CN]: 'zh-CN',
|
|
|
+ [Lang.fr_FR]: 'fr-FR',
|
|
|
+} as const;
|
|
|
+
|
|
|
+// use this function to translate content
|
|
|
+export const getLangAtServerSide = (req: CrowiRequest): Lang => {
|
|
|
const { user, headers } = req;
|
|
|
const { configManager } = req.crowi;
|
|
|
|
|
|
- return langMap[user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
|
|
|
- : (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US) ?? Lang.en_US];
|
|
|
+ return user == null ? detectLocaleFromBrowserAcceptLanguage(headers)
|
|
|
+ : (user.lang ?? configManager.getConfig('crowi', 'app:globalLang') as Lang ?? Lang.en_US) ?? Lang.en_US;
|
|
|
+};
|
|
|
+
|
|
|
+// use this function to get locale for html lang attribute
|
|
|
+export const getLocaleAtServerSide = (req: CrowiRequest): Locale => {
|
|
|
+ return langMap[getLangAtServerSide(req)];
|
|
|
};
|
|
|
|
|
|
export const getNextI18NextConfig = async(
|
|
|
@@ -135,7 +141,7 @@ export const getNextI18NextConfig = async(
|
|
|
|
|
|
// determine language
|
|
|
const req: CrowiRequest = context.req as CrowiRequest;
|
|
|
- const locale = getLocaleAtServerSide(req);
|
|
|
+ const lang = getLangAtServerSide(req);
|
|
|
|
|
|
const namespaces = ['commons'];
|
|
|
if (namespacesRequired != null) {
|
|
|
@@ -146,7 +152,8 @@ export const getNextI18NextConfig = async(
|
|
|
namespaces.push('translation');
|
|
|
}
|
|
|
|
|
|
- return serverSideTranslations(locale, namespaces, nextI18NextConfig, preloadAllLang ? AllLang : false);
|
|
|
+ // The first argument must be a language code with an underscore, such as en_US
|
|
|
+ return serverSideTranslations(lang, namespaces, nextI18NextConfig, preloadAllLang ? AllLang : false);
|
|
|
};
|
|
|
|
|
|
/**
|