|
1 | | -import { resolve } from '$app/paths' |
2 | 1 | import type { Component } from 'svelte' |
3 | 2 |
|
4 | 3 | const MODULES = import.meta.glob('/src/docs/**/*.md') |
5 | 4 |
|
6 | 5 | export const DEFAULT_LANGUAGE = 'en' |
7 | | -export const SUPPORTED_LANGUAGES = ['de', 'en', 'nl', 'ru', 'uk', 'zh-cn'] |
| 6 | +export const SUPPORTED_LANGUAGES = ['en', 'de', 'nl', 'ru', 'uk', 'zh-cn'] |
| 7 | +/** Languages served under a `/<lang>` prefix. The default language has no prefix. */ |
| 8 | +export const PREFIXED_LANGUAGES = SUPPORTED_LANGUAGES.filter(lang => lang !== DEFAULT_LANGUAGE) |
8 | 9 |
|
9 | | -export function getDocModule(lang = 'en', path: string) { |
10 | | - const normalizedLang = SUPPORTED_LANGUAGES.includes(lang) ? lang : DEFAULT_LANGUAGE |
11 | | - const key = `/src/docs/${path}/${normalizedLang}.md` |
| 10 | +type DocModule = () => Promise<{ default: Component; metadata?: Record<string, any> }> |
12 | 11 |
|
13 | | - const mod = MODULES[key] ?? MODULES[`/src/docs/${path}/${DEFAULT_LANGUAGE}.md`] |
| 12 | +/** Every doc page path relative to `/docs` (e.g. `nodes/cubes`) that has at least one language file. */ |
| 13 | +export const DOC_PATHS = [ |
| 14 | + ...new Set( |
| 15 | + Object.keys(MODULES).map(key => |
| 16 | + key.replace(/^\/src\/docs\//, '').replace(/\/[^/]+\.md$/, '') |
| 17 | + ) |
| 18 | + ), |
| 19 | +] |
14 | 20 |
|
15 | | - return mod as () => Promise<{ default: Component; metadata: Record<string, any> }> |
| 21 | +/** |
| 22 | + * Finds the doc module for `path` in `lang`, falling back to the default language. |
| 23 | + * `translated` is false when the fallback was used. |
| 24 | + */ |
| 25 | +export function getDocModule(path: string, lang: string) { |
| 26 | + const translatedModule = MODULES[`/src/docs/${path}/${lang}.md`] as DocModule | undefined |
| 27 | + if (translatedModule) return { load: translatedModule, translated: true } |
| 28 | + |
| 29 | + const fallback = MODULES[`/src/docs/${path}/${DEFAULT_LANGUAGE}.md`] as DocModule | undefined |
| 30 | + if (fallback) return { load: fallback, translated: false } |
| 31 | +} |
| 32 | + |
| 33 | +/** Prefixes a site-absolute `/docs` path with `lang`. Every other href is returned unchanged. */ |
| 34 | +export function localizeHref(href: string, lang: string | undefined): string { |
| 35 | + if (!lang || lang === DEFAULT_LANGUAGE || !/^\/docs(?=[/#?]|$)/.test(href)) return href |
| 36 | + return `/${lang}${href}` |
| 37 | +} |
| 38 | + |
| 39 | +/** Removes a leading `/<lang>` prefix and any trailing slash from a pathname. */ |
| 40 | +export function stripLanguage(pathname: string, lang: string | undefined): string { |
| 41 | + let path = pathname |
| 42 | + if (lang && lang !== DEFAULT_LANGUAGE && path.startsWith(`/${lang}/`)) { |
| 43 | + path = path.slice(lang.length + 1) |
| 44 | + } |
| 45 | + if (path.length > 1 && path.endsWith('/')) path = path.slice(0, -1) |
| 46 | + return path |
16 | 47 | } |
17 | 48 |
|
18 | | -export function docHref(path: string, lang: string): string { |
19 | | - const prefix = lang === DEFAULT_LANGUAGE ? '' : `/${lang}` |
20 | | - return resolve(`${prefix}/${path}`) |
| 49 | +/** The name of `lang` written in `inLang`, e.g. `Deutsch` for `de` by default, or `German` in `en`. */ |
| 50 | +export function getLanguageName(lang: string, inLang = lang): string { |
| 51 | + const name = new Intl.DisplayNames([inLang], { type: 'language' }).of(lang) ?? lang |
| 52 | + return name.charAt(0).toLocaleUpperCase(inLang) + name.slice(1) |
21 | 53 | } |
0 commit comments