Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
feat: add stripLocaleFromPathName helper
Browse files Browse the repository at this point in the history
  • Loading branch information
matejm committed Nov 2, 2023
1 parent ef52747 commit 27ca02b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/i18n/client.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// locales/client.ts
'use client';

import { ReactNode } from 'react';
Expand Down
16 changes: 16 additions & 0 deletions src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ export type UnsafeT = (
params?: Record<string, string | ReactNode | number>,
) => string | ReactNode;

/**
* Strip locale from pathname.
* @example
* stripLocaleFromPathName('/sl/test') // => '/test'
* stripLocaleFromPathName('/en/test') // => '/en/test'
* stripLocaleFromPathName('/test') // => '/test'
*/
export const stripLocaleFromPathName = (pathname: string) => {
for (const locale of LOCALES) {
if (pathname.startsWith(`/${locale}/`)) {
return pathname.slice(locale.length + 1);
}
}
return pathname;
};

// Assert that defaultLocale is in LOCALES
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _checkDefaultLocale: Locale = defaultLocale;

0 comments on commit 27ca02b

Please sign in to comment.