Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dictionary #19

Merged
merged 6 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
# React.js Hello World <sup>(Enterprise Edition)</sup>

This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).

## Currently Supported Features <sup>to be continued...</sup>

- A/B testing
- Localization
- Server-side rendering

## Getting Started

First, run the development server:

```bash
```sh
bun run dev
# or
pnpm dev
Expand Down
16 changes: 13 additions & 3 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
import './globals.css';
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';

import Providers from './providers';
import type { Locales } from '@/types/locales';

import './globals.css';

export async function generateStaticParams() {
return [{ lang: 'en' }, { lang: 'es' }];
}

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'React Hello World Enterprise Edition',
title: 'React Hello World | Enterprise Edition',
description: 'Generated by create next app',
};

export default function RootLayout({
children,
params,
}: {
children: React.ReactNode;
params: {
lang: Locales;
};
}) {
return (
<html lang="en">
<html lang={params.lang}>
<body className={inter.className}>
<Providers>{children}</Providers>
</body>
Expand Down
11 changes: 9 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import Image from 'next/image';
import { HelloWorld } from '@/components/hello-world';
import { Locales } from '@/types/locales';
import { getDictionary } from '@/dictionaries/index';

export default function Home() {
export default async function Home({
params: { lang },
}: {
params: { lang: Locales };
}) {
const dict = await getDictionary(lang);
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
Expand Down Expand Up @@ -40,7 +47,7 @@ export default function Home() {
/>
</div>

<HelloWorld />
<HelloWorld name={dict.world} lang={lang} />

<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
<a
Expand Down
10 changes: 8 additions & 2 deletions components/hello-world/hello-world.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ const meta = {
export default meta;
type Story = StoryObj<typeof meta>;

export const LoggedIn: Story = {
export const LangdEn: Story = {
args: {
name: 'Jane Doe',
lang: 'en',
},
};

export const LoggedOut: Story = {};
export const LangEs: Story = {
args: {
name: 'Jane Doe',
lang: 'es',
},
};
14 changes: 11 additions & 3 deletions components/hello-world/hello-world.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import Image from 'next/image';

import Earth from '@/public/earth.svg';
import { getDictionary } from '@/dictionaries/index';
import type { Locales } from '@/types/locales';

import { Title } from './hello-world.css';
import { getDictionary } from '../../app/dictionaries';

export const HelloWorld = async ({ name = 'World' }) => {
const dict = await getDictionary('en');
export const HelloWorld = async ({
name,
lang = 'en',
}: {
name: string;
lang: Locales;
}) => {
const dict = await getDictionary(lang);
return (
<>
<h1 className={Title}>
Expand Down
6 changes: 4 additions & 2 deletions dictionaries/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"hello": "Hola"
}
"hello": "Hello",
"hey": "Hey",
"world": "World"
}
6 changes: 4 additions & 2 deletions dictionaries/es.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"hello": "Hello"
}
"hello": "Hola",
"hey": "Eh",
"world": "Mundo"
}
10 changes: 10 additions & 0 deletions dictionaries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'server-only';
import type { Locales } from '@/types/locales';

const dictionaries = {
en: () => import('./en.json').then((module) => module.default),
es: () => import('./es.json').then((module) => module.default),
};

export const getDictionary = async (locale: Locales) =>
dictionaries[locale]?.() ?? dictionaries.en();
6 changes: 6 additions & 0 deletions i18n-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const i18n = {
defaultLocale: 'en',
locales: ['en', 'es'],
} as const;

export type Locale = (typeof i18n)['locales'][number];
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
"paths": {
"@/components/*": ["./components/*"],
"@/public/*": ["./public/*"]
"@/public/*": ["./public/*"],
"@/dictionaries/*": ["./dictionaries/*"],
"@/types/*": ["./types/*"]
}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
Expand Down
1 change: 1 addition & 0 deletions types/locales.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Locales = 'en' | 'es';