Fix formating authors
This commit is contained in:
@@ -136,7 +136,7 @@ export abstract class BaseAppService implements AppService {
|
||||
hash,
|
||||
format,
|
||||
title: formatTitle(loadedBook.metadata.title),
|
||||
author: formatAuthors(loadedBook.metadata.author),
|
||||
author: formatAuthors(loadedBook.metadata.language, loadedBook.metadata.author),
|
||||
lastUpdated: Date.now(),
|
||||
};
|
||||
if (!(await this.fs.exists(getDir(book), 'Books'))) {
|
||||
|
||||
@@ -46,11 +46,29 @@ const formatLanguageMap = (x: string | LanguageMap): string => {
|
||||
return x[userLang] || x[keys[0]!]!;
|
||||
};
|
||||
|
||||
const listFormat = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' });
|
||||
const listFormat = (lang: string) => {
|
||||
if (lang === 'zh') {
|
||||
return new Intl.ListFormat('en', { style: 'narrow', type: 'unit' });
|
||||
} else {
|
||||
return new Intl.ListFormat(lang, { style: 'long', type: 'conjunction' });
|
||||
}
|
||||
};
|
||||
|
||||
export const formatAuthors = (contributors: string | Contributor | [string | Contributor]) =>
|
||||
const getBookLangCode = (lang: string | string[] | undefined) => {
|
||||
try {
|
||||
const bookLang = typeof lang === 'string' ? lang : lang?.[0];
|
||||
return bookLang ? bookLang.split('-')[0]! : 'en';
|
||||
} catch {
|
||||
return 'en';
|
||||
}
|
||||
};
|
||||
|
||||
export const formatAuthors = (
|
||||
bookLang: string | string[] | undefined,
|
||||
contributors: string | Contributor | [string | Contributor],
|
||||
) =>
|
||||
Array.isArray(contributors)
|
||||
? listFormat.format(
|
||||
? listFormat(getBookLangCode(bookLang)).format(
|
||||
contributors.map((contributor) =>
|
||||
typeof contributor === 'string' ? contributor : formatLanguageMap(contributor?.name),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user