|
|
@@ -1,6 +1,9 @@
|
|
|
import { Lang } from '@growi/core/dist/interfaces';
|
|
|
import type { IncomingHttpHeaders } from 'http';
|
|
|
-
|
|
|
+import { type Locale } from 'date-fns/locale';
|
|
|
+import {
|
|
|
+ enUS, ja, fr, ko, zhCN,
|
|
|
+} from 'date-fns/locale';
|
|
|
import * as i18nextConfig from '^/config/i18next.config';
|
|
|
|
|
|
const ACCEPT_LANG_MAP = {
|
|
|
@@ -11,6 +14,31 @@ const ACCEPT_LANG_MAP = {
|
|
|
ko: Lang.ko_KR,
|
|
|
};
|
|
|
|
|
|
+const DATE_FNS_LOCALE_MAP: Record<string, Locale | undefined> = {
|
|
|
+ 'en': enUS, 'en-US': enUS, 'en_US': enUS,
|
|
|
+ 'ja': ja, 'ja-JP': ja, 'ja_JP': ja,
|
|
|
+ 'fr': fr, 'fr-FR': fr, 'fr_FR': fr,
|
|
|
+ 'ko': ko, 'ko-KR': ko, 'ko_KR': ko,
|
|
|
+ 'zh': zhCN, 'zh-CN': zhCN, 'zh_CN': zhCN,
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * Gets the corresponding date-fns Locale object from an i18next language code.
|
|
|
+ * @param langCode The i18n language code (e.g., 'ja_JP').
|
|
|
+ * @returns The date-fns Locale object, defaulting to enUS if not found.
|
|
|
+ */
|
|
|
+export const getLocale = (langCode: string): Locale => {
|
|
|
+ let locale = DATE_FNS_LOCALE_MAP[langCode];
|
|
|
+
|
|
|
+ if (!locale) {
|
|
|
+ const baseCode = langCode.split(/[-_]/)[0];
|
|
|
+ locale = DATE_FNS_LOCALE_MAP[baseCode];
|
|
|
+ }
|
|
|
+
|
|
|
+ return locale ?? enUS;
|
|
|
+};
|
|
|
+
|
|
|
+
|
|
|
/**
|
|
|
* It return the first language that matches ACCEPT_LANG_MAP keys from sorted accept languages array
|
|
|
* @param sortedAcceptLanguagesArray
|