Skip to content

Commit

Permalink
feat(helper): ensure fallback exists in getLocaleConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Nov 21, 2024
1 parent 4eb7811 commit 9648e50
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
26 changes: 15 additions & 11 deletions tools/helper/src/node/locales/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ export const getRootLang = (app: App): string => {
}

/**
* Infer language path from root directory language
* Infer locale path from root directory language
*
* @param app VuePress Node App
* @returns inferred locale path of root directory
*/
export const getRootLangPath = (app: App): string =>
export const inferRootLocalePath = (app: App): string =>
inferLocalePath(getRootLang(app), app.env.isDebug)

/**
Expand Down Expand Up @@ -89,32 +89,35 @@ export const getLocaleConfig = <T extends LocaleData>({
default: defaultLocalesConfig,
config: userLocalesConfig = {},
}: LocaleConfigOptions<T>): ExactLocaleConfig<T> => {
const rootPath = getRootLangPath(app)
const rootLocalePath = inferRootLocalePath(app)
const logger = new Logger(name)

const fallbackLocaleData =
// fallback to the root locale config
defaultLocalesConfig[rootLocalePath] ??
// fallback to the first locale config
Object.values(defaultLocalesConfig).shift()

return fromEntries([
...getLocalePaths(app)
.filter((localePath) => localePath !== '/')
.map<[string, T]>((localePath) => {
const defaultLocaleData =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
defaultLocalesConfig[localePath] ||
(defaultLocalesConfig[localePath] as T | undefined) ??
(inferLocalePath(app.options.locales[localePath].lang) === '/'
? null
: defaultLocalesConfig[
inferLocalePath(app.options.locales[localePath].lang)
])

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!defaultLocaleData)
logger.warn(`Locale ${localePath} is missing it's i18n config`)

return [
localePath,
deepAssign(
{},
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
defaultLocaleData ?? defaultLocalesConfig[rootPath] ?? {},
defaultLocaleData ?? fallbackLocaleData,
userLocalesConfig[localePath] ?? {},
),
]
Expand All @@ -123,9 +126,10 @@ export const getLocaleConfig = <T extends LocaleData>({
'/',
deepAssign(
{},
defaultLocalesConfig[rootPath],
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
userLocalesConfig['/'] ?? userLocalesConfig[rootPath] ?? {},
fallbackLocaleData,
(userLocalesConfig['/'] as T | undefined) ??
(userLocalesConfig[rootLocalePath] as T | undefined) ??
{},
),
],
])
Expand Down
40 changes: 40 additions & 0 deletions tools/helper/tests/node/locales.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,46 @@ describe('getLocaleConfig() should generate locale', () => {
})

describe('handle unknown locale', () => {
it('unknown root language should fallback to first language in default config', () => {
const app = createBaseApp({
locales: {
'/': { lang: 'unknown-Language' },
'/en/': { lang: 'en-US' },
'/ja/': { lang: 'ja-JP' },
'/id/': { lang: 'id-ID' },
'/nl/': { lang: 'nl-NL' },
},
source: path.resolve(__dirname, './__fixtures__/src'),
bundler: {} as Bundler,
theme: emptyTheme,
})

const locales = getLocaleConfig({ app, default: defaultLocaleConfig })

expect(locales).toEqual({
'/': {
text: 'English',
fallback: 'English',
},
'/en/': {
text: 'English',
fallback: 'English',
},
'/ja/': {
text: '日本',
fallback: '日本',
},
'/id/': {
text: 'Indonesia',
fallback: 'Indonesia',
},
'/nl/': {
text: 'Dutch',
fallback: 'Dutch',
},
})
})

it('fallback to root language if exists', () => {
const app = createBaseApp({
locales: {
Expand Down

0 comments on commit 9648e50

Please sign in to comment.