Skip to content

AlexXanderGrib/i18n-typescript-how-to

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

TypeScript + i18n done properly

All source in i18n/

  • Absolute type safety, no more "translation.common.app.name"
  • Works with TypeScript 4.9+
  • Requires no interaction with fs or fetch. All done by your bundler/runtime
  • Caches loaded locales.
  • No codegen

Example (Next.JS)

// @/i18n/locales/en/index.ts

export const en = {
  hello: "Hello world!"
}
// pages/index.tsx
import { loadLocale, checkLocale } from "@/i18n";
import type { GetStaticProps, InferGetStaticPropsType } from "next"

export function getStaticProps({ locale }) {
  return {
    props: {
      translation: await loadLocale(checkLocale(locale))
    }
  }
} satisfies GetStaticProps;

export function Home({ translation }: InferGetStaticPropsType<typeof getStaticProps>) {
  return <h1>{translation.hello}</h1>;
  //                      ^? (property) hello: string 
}

About

TypeScript + i18n done properly

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published