Skip to content

Instantly share code, notes, and snippets.

@treckstar
Created May 11, 2023 18:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save treckstar/c7be61248fd5b01a075f4e24c82fbc51 to your computer and use it in GitHub Desktop.
Save treckstar/c7be61248fd5b01a075f4e24c82fbc51 to your computer and use it in GitHub Desktop.
Next.js TypeScript Localization Proof of Concept for `output: 'export'` builds
import { GetStaticPaths, GetStaticProps } from 'next';
type Locale = 'en-US' | 'es' | 'hm';
type PageData = {
// Define the properties you need for your page data
title: string;
content: string;
};
type PageParams = {
slug: string[];
locale: Locale;
};
const slugs: string[] = ['/about', '/products', '/contact'];
const locales: Locale[] = ['en-US', 'es', 'hm'];
export const getStaticPaths: GetStaticPaths<PageParams> = async () => {
const paths = [];
for (const slug of slugs) {
for (const locale of locales) {
paths.push({ params: { slug: [slug], locale } });
}
}
return {
paths,
fallback: false, // or 'blocking' if you want to use fallback behavior
};
};
export const getStaticProps: GetStaticProps<PageData, PageParams> = async ({ params }) => {
const { slug, locale } = params;
// Fetch data based on the slug and locale
// Example implementation:
const title = `Page Title: ${slug}`;
const content = `Page Content for ${slug} in ${locale}`;
const pageData: PageData = {
title,
content,
};
return {
props: pageData,
};
};
type Locale = 'en-US' | 'es' | 'hm';
function generateLocalizedUrls(slugs: string[], locales: Locale[]): string[] {
const defaultLocale = 'en';
const localizedSlugs: string[] = [];
for (const slug of slugs) {
const trimmedSlug = slug.slice(1); // Remove leading slash
for (const locale of locales) {
const localeCode = locale === 'en-US' ? defaultLocale : locale;
const localizedSlug = `/${localeCode}/${trimmedSlug}`;
localizedSlugs.push(localizedSlug);
}
localizedSlugs.push(slug); // Add original relative slug
}
return localizedSlugs;
}
// Example usage
const slugs = ['/about', '/products', '/contact'];
const locales: Locale[] = ['en-US', 'es', 'hm'];
const localizedUrls = generateLocalizedUrls(slugs, locales);
console.log(localizedUrls);
type Locale = 'en' | 'es' | 'hm';
function generateLocalizedUrls(slugs: string[], locales: Locale[]): string[] {
const defaultLocale = 'en';
const localizedSlugs = [];
for (const slug of slugs) {
for (const locale of locales) {
const localeCode = locale === defaultLocale ? '' : `/${locale}`;
const localizedSlug = slug.startsWith('/') ? `${localeCode}${slug}` : `${localeCode}/${slug}`;
localizedSlugs.push(localizedSlug);
}
}
return localizedSlugs;
}
// Example usage
const slugs = ['about', 'products', 'contact'];
const locales: Locale[] = ['en', 'es', 'hm'];
const localizedUrls = generateLocalizedUrls(slugs, locales);
console.log(localizedUrls);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment