Compare commits

...
3 changed files with 61 additions and 1 deletions

No files matched your search

+5
View File
@@ -0,0 +1,5 @@
---
"@jackyzha0/quartz": patch
---
A `locale` that Quartz has no translation for (such as `sv-SE`, `da-DK` or `es-MX`) no longer crashes the build. Quartz uses a translation of the same language (`es-MX` uses `es-ES`), else English, and prints one warning; dates still use the configured locale.
+28
View File
@@ -0,0 +1,28 @@
import test, { describe } from "node:test"
import assert from "node:assert"
import { i18n, resolveTranslationLocale } from "./index"
describe("i18n locale fallback", () => {
test("a locale with a translation uses it, whatever its case", (t) => {
t.mock.method(console, "warn", () => {})
assert.strictEqual(resolveTranslationLocale("de-DE"), "de-DE")
assert.strictEqual(resolveTranslationLocale("de-de"), "de-DE")
assert.strictEqual(resolveTranslationLocale(undefined), "en-US")
})
test("a locale without one uses the same language, else English, and warns once", (t) => {
const warn = t.mock.method(console, "warn", () => {})
assert.strictEqual(resolveTranslationLocale("es-MX"), "es-ES")
assert.strictEqual(resolveTranslationLocale("es-MX"), "es-ES")
assert.strictEqual(resolveTranslationLocale("sv-SE"), "en-US")
assert.strictEqual(resolveTranslationLocale("nb"), "nb-NO")
assert.strictEqual(warn.mock.callCount(), 3)
assert.match(String(warn.mock.calls[0]!.arguments[0]), /no translation for locale "es-MX"/)
})
// used to throw: TRANSLATIONS["sv-SE"] was undefined
test("i18n() of a locale without a translation returns one", (t) => {
t.mock.method(console, "warn", () => {})
assert.strictEqual(i18n("da-DK").pages.error.title, i18n("en-US").pages.error.title)
})
})
+28 -1
View File
@@ -85,6 +85,33 @@ export const TRANSLATIONS = {
} as const
export const defaultTranslation = "en-US"
export const i18n = (locale: ValidLocale): Translation => TRANSLATIONS[locale ?? defaultTranslation]
const warnedLocales = new Set<string>()
/**
* The translation to use for `locale`: its own, else one of the same language (`es-MX` uses
* `es-ES`), else English. A locale without a translation used to crash the build
* (`TRANSLATIONS[locale]` is undefined); dates and numbers still use the configured locale.
*/
export function resolveTranslationLocale(locale: string | undefined): ValidLocale {
if (!locale) return defaultTranslation
const keys = Object.keys(TRANSLATIONS) as ValidLocale[]
const exact = keys.find((key) => key.toLowerCase() === locale.toLowerCase())
if (exact) return exact
const language = locale.split("-")[0]!.toLowerCase()
const chosen =
keys.find((key) => key.split("-")[0]!.toLowerCase() === language) ?? defaultTranslation
if (!warnedLocales.has(locale)) {
warnedLocales.add(locale)
console.warn(
`Quartz has no translation for locale "${locale}"; its text uses ${chosen}. ` +
`Dates still use ${locale}. Translations: https://quartz.jzhao.xyz/features/i18n`,
)
}
return chosen
}
export const i18n = (locale: ValidLocale | string | undefined): Translation =>
TRANSLATIONS[resolveTranslationLocale(locale)]
export type ValidLocale = keyof typeof TRANSLATIONS
export type ValidCallout = keyof CalloutTranslation