59 lines
2.3 KiB
TypeScript
59 lines
2.3 KiB
TypeScript
/**
|
|
* The languages an article can be written in: an ISO 639-1 code with the
|
|
* name of the language in its own script, as the editor's picker lists them.
|
|
*/
|
|
export const LANGUAGES: ReadonlyArray<{ code: string; name: string }> = [
|
|
{ code: 'en', name: 'English' },
|
|
{ code: 'fr', name: 'Français' },
|
|
{ code: 'de', name: 'Deutsch' },
|
|
{ code: 'es', name: 'Español' },
|
|
{ code: 'ca', name: 'Català' },
|
|
{ code: 'pt', name: 'Português' },
|
|
{ code: 'it', name: 'Italiano' },
|
|
{ code: 'nl', name: 'Nederlands' },
|
|
{ code: 'pl', name: 'Polski' },
|
|
{ code: 'ru', name: 'Русский' },
|
|
{ code: 'uk', name: 'Українська' },
|
|
{ code: 'tr', name: 'Türkçe' },
|
|
{ code: 'ar', name: 'العربية' },
|
|
{ code: 'fa', name: 'فارسی' },
|
|
{ code: 'hi', name: 'हिन्दी' },
|
|
{ code: 'zh', name: '中文' },
|
|
{ code: 'ja', name: '日本語' },
|
|
{ code: 'ko', name: '한국어' },
|
|
]
|
|
|
|
export const DEFAULT_LANGUAGE = 'en'
|
|
|
|
/** The language an article is written in; articles predating the field are English. */
|
|
export function articleLanguage(article: { language?: string }): string {
|
|
return article.language?.trim().toLowerCase() || DEFAULT_LANGUAGE
|
|
}
|
|
|
|
/** Display name for a code — the code itself in upper case if it is unfamiliar. */
|
|
export function languageName(code: string): string {
|
|
return LANGUAGES.find((l) => l.code === code)?.name ?? code.toUpperCase()
|
|
}
|
|
|
|
/**
|
|
* Which language version to open an article in when the address asks for
|
|
* none: the reader's own language when the article speaks it, else English
|
|
* as the wiki's common tongue, else null — nothing to pick, so whatever
|
|
* version already stands for the article answers.
|
|
*/
|
|
export function preferredLanguage(
|
|
available: readonly (string | undefined)[],
|
|
reader: string
|
|
): string | null {
|
|
const codes = new Set(available.map((c) => (c ?? '').trim().toLowerCase()))
|
|
const wanted = reader.trim().toLowerCase()
|
|
if (codes.has(wanted)) return wanted
|
|
return codes.has(DEFAULT_LANGUAGE) ? DEFAULT_LANGUAGE : null
|
|
}
|
|
|
|
/** The first language no version of an article has been written in yet. */
|
|
export function nextFreeLanguage(taken: readonly string[]): string {
|
|
const takenCodes = taken.map((c) => c.trim().toLowerCase())
|
|
return LANGUAGES.find((l) => !takenCodes.includes(l.code))?.code ?? DEFAULT_LANGUAGE
|
|
}
|