Skip to content

Commit

Permalink
Always include symbolic name.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyjko committed Sep 17, 2023
1 parent 1b8d505 commit 7a0fcac
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/locale/getNameLocales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,35 @@ import Names from '@nodes/Names';
import type { NameText } from './Locale';
import type Locale from './Locale';
import { localeToLanguage } from './localeToLanguage';
import en from '../locale/en-US.json';

export function getNameLocales(
locales: Locale[],
nameText: NameText | ((translation: Locale) => NameText)
nameText: NameText | ((locale: Locale) => NameText)
): Names {
return new Names(
locales.reduce((names: Name[], locale) => {
const name =
nameText instanceof Function ? nameText(locale) : nameText;
return names.concat(
(Array.isArray(name) ? name : [name]).map((n) =>
Name.make(n, localeToLanguage(locale))
)
);
}, [])
);
// Construct names from the given locales.
let names = locales.reduce((names: Name[], locale) => {
const name = nameText instanceof Function ? nameText(locale) : nameText;
return names.concat(
(Array.isArray(name) ? name : [name]).map((n) =>
Name.make(n, localeToLanguage(locale))
)
);
}, []);
// If the given locales don't include, the default locale, include the symbolic name from the default locale first.
if (
nameText instanceof Function &&
locales.find((locale) => locale === en) === undefined
) {
const defaultNameTexts = nameText(en as Locale);
const symbolic = (
Array.isArray(defaultNameTexts)
? defaultNameTexts
: [defaultNameTexts]
)
.map((n) => Name.make(n, localeToLanguage(en as Locale)))
.find((name) => name.isSymbolic());
if (symbolic) names = [symbolic, ...names];
}
return new Names(names);
}

0 comments on commit 7a0fcac

Please sign in to comment.