From 7a0fcaccaa16f298e9b420bf1acbe466413bd9f9 Mon Sep 17 00:00:00 2001 From: "Amy J. Ko" Date: Sun, 17 Sep 2023 13:46:24 -0700 Subject: [PATCH] Always include symbolic name. --- src/locale/getNameLocales.ts | 39 +++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/locale/getNameLocales.ts b/src/locale/getNameLocales.ts index 02b8fc4f3..fe6e9ab05 100644 --- a/src/locale/getNameLocales.ts +++ b/src/locale/getNameLocales.ts @@ -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); }