-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlanguages.ts
32 lines (23 loc) · 1.01 KB
/
languages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import engineConfig from "./engine.config";
export type LanguageType = "en";
export type LanguagesListItemType = {
id: LanguageType;
title: string;
};
export type LanguagesListType = LanguagesListItemType[];
export const languages = (engineConfig.languages || [
{ id: "en", title: "English" },
]) as LanguagesListType;
export const baseLanguage: LanguageType = languages[0].id;
export const getLanguageTitle = (language: LanguageType): string | null =>
languages.find(({ id }) => id === language)?.title ?? null;
export const getLanguagePath = (language?: LanguageType): string =>
language === baseLanguage || !language ? "/" : `/${language}`;
export const isLanguage = (str: string): boolean =>
languages.some(({ id }) => id === str);
export const getLanguageFromPath = (path: string | null): LanguageType => {
if (!path || !path.length) return baseLanguage;
const pathParts = path.split("/");
const language = pathParts[1];
return isLanguage(language) ? (language as LanguageType) : baseLanguage;
};