This commit is contained in:
+14
-12
@@ -1,21 +1,23 @@
|
||||
import { routing } from "../i18n/routing";
|
||||
import { appLocales } from "../i18n/routing";
|
||||
|
||||
export type AppLocale = (typeof routing.locales)[number];
|
||||
export type AppLocale = (typeof appLocales)[number];
|
||||
|
||||
export function resolveLocale(locale: string): AppLocale {
|
||||
if (locale === "en" || locale === "ar") {
|
||||
return locale;
|
||||
}
|
||||
export const FALLBACK_LOCALE: AppLocale = "de";
|
||||
|
||||
return routing.defaultLocale;
|
||||
export function isSupportedLocale(locale: string | undefined | null): locale is AppLocale {
|
||||
return locale === "de" || locale === "en" || locale === "ar";
|
||||
}
|
||||
|
||||
export function resolveLocale(locale: string | undefined | null, fallbackLocale: AppLocale): AppLocale {
|
||||
return isSupportedLocale(locale) ? locale : fallbackLocale;
|
||||
}
|
||||
|
||||
export function getDirection(locale: string): "ltr" | "rtl" {
|
||||
return resolveLocale(locale) === "ar" ? "rtl" : "ltr";
|
||||
return locale === "ar" ? "rtl" : "ltr";
|
||||
}
|
||||
|
||||
export function stripLocalePrefix(pathname: string): string {
|
||||
for (const locale of routing.locales) {
|
||||
for (const locale of appLocales) {
|
||||
if (pathname === `/${locale}`) {
|
||||
return "/";
|
||||
}
|
||||
@@ -31,7 +33,7 @@ export function stripLocalePrefix(pathname: string): string {
|
||||
export function getLocalizedPath(
|
||||
locale: string,
|
||||
pathname = "/",
|
||||
defaultLocale: AppLocale = routing.defaultLocale,
|
||||
defaultLocale: AppLocale,
|
||||
): string {
|
||||
return getLocalizedPathWithDefault(locale, pathname, defaultLocale);
|
||||
}
|
||||
@@ -39,9 +41,9 @@ export function getLocalizedPath(
|
||||
export function getLocalizedPathWithDefault(
|
||||
locale: string,
|
||||
pathname = "/",
|
||||
defaultLocale: AppLocale = routing.defaultLocale,
|
||||
defaultLocale: AppLocale,
|
||||
): string {
|
||||
const localeKey = resolveLocale(locale);
|
||||
const localeKey = resolveLocale(locale, defaultLocale);
|
||||
const normalizedPath = pathname === "" ? "/" : pathname;
|
||||
const strippedPath = stripLocalePrefix(normalizedPath);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user