mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-09 12:58:01 +02:00
feat: allow setting calendar for i18n date formatting (#6430)
* feat: allow setting calendar for i18n date formatting * fix TS * add test
This commit is contained in:
parent
3e110054d7
commit
7941a46be3
9 changed files with 54 additions and 12 deletions
|
@ -19,7 +19,11 @@ const DefaultI18N: I18n = {
|
|||
currentLocale: 'en',
|
||||
locales: ['en'],
|
||||
defaultLocale: 'en',
|
||||
localeConfigs: {},
|
||||
localeConfigs: {
|
||||
en: {
|
||||
calendar: 'gregory',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
function getBlogContentPaths(siteDir: string): BlogContentPaths {
|
||||
|
|
|
@ -41,7 +41,7 @@ function getI18n(locale: string): I18n {
|
|||
currentLocale: locale,
|
||||
locales: [locale],
|
||||
defaultLocale: locale,
|
||||
localeConfigs: {},
|
||||
localeConfigs: {[locale]: {calendar: 'gregory'}},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -151,13 +151,18 @@ export function parseBlogFileName(
|
|||
return {date: undefined, text, slug};
|
||||
}
|
||||
|
||||
function formatBlogPostDate(locale: string, date: Date): string {
|
||||
function formatBlogPostDate(
|
||||
locale: string,
|
||||
date: Date,
|
||||
calendar: string,
|
||||
): string {
|
||||
try {
|
||||
return new Intl.DateTimeFormat(locale, {
|
||||
day: 'numeric',
|
||||
month: 'long',
|
||||
year: 'numeric',
|
||||
timeZone: 'UTC',
|
||||
calendar,
|
||||
}).format(date);
|
||||
} catch (err) {
|
||||
logger.error`Can't format blog post date "${String(date)}"`;
|
||||
|
@ -253,7 +258,11 @@ async function processBlogSourceFile(
|
|||
}
|
||||
|
||||
const date = await getDate();
|
||||
const formattedDate = formatBlogPostDate(i18n.currentLocale, date);
|
||||
const formattedDate = formatBlogPostDate(
|
||||
i18n.currentLocale,
|
||||
date,
|
||||
i18n.localeConfigs[i18n.currentLocale]!.calendar,
|
||||
);
|
||||
|
||||
const title = frontMatter.title ?? contentTitle ?? parsedBlogFileName.text;
|
||||
const description = frontMatter.description ?? excerpt ?? '';
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue