๊ตญ์ ํ ๋์ ํ๊ธฐ
๊ธฐ์กด์ ์ด์ํ๋ ์น์ฌ์ดํธ์ ๊ตญ์ ํ ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ฒ ๋์๋ค. ๊ธฐ์กด ์ฌ์ดํธ๋ ์๋ฌธ ๋ฒํผ์ ๋๋ฅด๋ฉด google translate API๊ฐ ์๋ํ๋ ๋ฐฉ์์ด์๋ค.
๋ฌธ์ ์
1. ์ด์ํ ๋ฒ์ญ
์๋ฌด๋๋ ๋ฒ์ญ๊ธฐ๋ฅผ ๋๋ฆฌ๋ ๋ฐฉ์์ด๋ผ ๋ฒ์ญ์ด ๋งค์ฐ ์ด์ํ๋ค. ํ๊ธ ๋ฌธ๋จ์ ์ธ์ํด ๋ฒ์ญํด ๋ฒ์ญ๋๋ฉด ์๋๋ ๋ถ๋ถ(ํ์ฌ๋ช ๊ฐ์ ๋ถ๋ถ)๋ ๊ฐ์ด ๋ฒ์ญ๋์๋ค. ์ฌ์ดํธ ํ๊ฒ์ธต์ด ์ฐ๋ฆฌ๋๋ผ๋ง ์๋๊ฒ์ด ์๋๋ผ ํ๋ฆฐ ์๋ฌธ๋ฒ์ด ๋ณด์ด๋๊ฑธ ๊ฐฑ์ฅํ ์ซ์ดํ์ จ๋ค,,,
2. ๋๋ฆฐ ์๋
google translate API๋ฅผ ์ฌ์ฉํ ๋ฒ์ญ ๊ธฐ๋ฅ์ ํ์ด์ง์ ๋ ๋๋ง๋ ํ๊ธ์ ์ธ์ํด ์์ด๋ก ๋ฒ์ญํ๋ ๋ฐฉ์์ด๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด ์ค๋๊ฑธ๋ ธ๋ค. ๋ค๋ฅธํ์ด์ง๋ก ์ด๋ํ ๊ธฐ์กด ์ฌ์ดํธ๋ก ์ด๋ํด๋ ๋ค์ ๋ฒ์ญ๋์ด ์๊ฐ์ด ๊ฑธ๋ ธ๋ค.
Next.js ๊ตญ์ ํ
๋ฐฉ๋ฒ 1. ์์ด ์ปดํฌ๋ํธ๋ง ์ถ๊ฐํ๊ธฐ
<p>{lang === "ko" ? "์๋
ํ์ธ์" : "Hello"}</p>
๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ด๋ค. ๊ธฐ์กด ํ์ผ๋ ์ ๋ถ ํ๊ธ์ด ํ๋์ฝ๋ฉ ๋์ด์์ด์ ๊ฑฐ๊ธฐ์ ์๋ฌธ ๋ฒ์ ์ปดํฌ๋ํธ๋ง ์ถ๊ฐํ๋ฉด ๋๋คโฆ ํ์ง๋ง 1) ์ฝ๋ ๊ธธ์ด๊ฐ ๊ธธ์ด์ ธ ๊ฐ๋ ์ฑ์ด ์์ข์์ง๊ณ , 2) ์๋ฌธ ์ด์ธ ๋ค๋ฅธ ์ธ์ด๋ฅผ ์ถ๊ฐํ ๋ ์ดํ ์ ์ง๋ณด์๋ฅผ ์๊ฐํด์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ ์ฉํ๊ธฐ๋ก ํ๋ค.
๋ฐฉ๋ฒ 2. ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉํ๊ธฐ
๊ธฐ์กด ์ฌ์ดํธ๋ Next.js๋ก ๋ง๋ค์ด์ ธ ์ฐพ์๋ณด๋ ์ฌ๋ฌ๊ฐ์ง ๊ตญ์ ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์์๋ค. ๋ํ์ ์ผ๋ก next-i18next, next-intl, next-translate๊ฐ ์์๋ค.
~~๋ฌธ์ ๋ ๋ ํฌํจ ์ธํด๋ค์ด ๋ฐฑ์๋ ๊ฐ๋ฐ์๋ผ Next.js๋ ๋ฌธ์ธํ์ด๋ผ๋ ๊ฒ์ด์๋คโฆ ๊ฒฐ๊ตญ ๋ฌ๋์ปค๋ธ๋๋ฌธ์ ๋์ ๊น์ง ์ดํ์ด ๊ฑธ๋ ธ๋ค. ~~ ์ธ ๊ฐ์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค next-intl์ ์ ํํ๊ณ Docs๋ฅผ ์ฐฌ์ฐฌํ ๋ฏ์ด๋ณด๋ฉฐ ๊ตญ์ ํ๋ฅผ ์์ํ๋ค.
next-intl ๋์
- ๋จผ์ ํ๋ก์ ํธ์ next-intl์ import ํ๊ณ ํ์ผ ๊ตฌ์กฐ๋ฅผ ์๋์ ๊ฐ์ด ๋ง์ถฐ์ค๋ค.
npm install next-intl
โโโ messages
โ โโโ en.json
โ โโโ ...
โโโ next.config.ts
โโโ src
โโโ i18n
โ โโโ routing.ts
โ โโโ navigation.ts
โ โโโ request.ts
โโโ middleware.ts
โโโ app
โโโ [locale]
โโโ layout.tsx
โโโ page.tsx
- messages ํด๋์ ์ธ์ด๋ณ json ํ์ผ์ ์์ฑํ๋ค.
// en.json
"Home": {
"message" : "hello",
}
// ko.json
"Home": {
"message" : "์๋
ํ์ธ์",
}
๋์ผํ key ๊ฐ๊ณผ ๋งค์นญ๋๋ value๋ฅผ ์ ์ฅํด์ฃผ์๋ค.
- next.config.ts
import {NextConfig} from 'next';
import createNextIntlPlugin from 'next-intl/plugin';
const nextConfig: NextConfig = {};
const withNextIntl = createNextIntlPlugin();
export default withNextIntl(nextConfig);
- routing.ts
import {defineRouting} from 'next-intl/routing';
import {createNavigation} from 'next-intl/navigation';
export const routing = defineRouting({
// A list of all locales that are supported
locales: ['en', 'ko'],
// Used when no locale matches
defaultLocale: 'ko'
});
- navigation.ts
import {createNavigation} from 'next-intl/navigation';
import {routing} from './routing';
// Lightweight wrappers around Next.js' navigation
// APIs that consider the routing configuration
export const {Link, redirect, usePathname, useRouter, getPathname} =
createNavigation(routing);
๊ธฐ์กด์ ์ฌ์ฉํ๋ navigation๋ค๋ next-intl/navigation์ผ๋ก ๋ฐ๊ฟ์ฃผ์๋ค.
- middleware.ts
import createMiddleware from 'next-intl/middleware';
import {routing} from './i18n/routing';
export default createMiddleware(routing);
export const config = {
matcher: [
'/', // ๋ฃจํธ ๊ฒฝ๋ก
'/(ko|en)/:path*', // ๊ตญ์ ํ ๊ฒฝ๋ก
'/((?!api|_next/static|_next/image|favicon.ico).*)'
]
};
๊ธฐ์กด ๋ฏธ๋ค์จ์ด๋ ์์ ํ์ฌ ๊ธฐ์กด ๊ฒฝ๋ก๋ก ๋ค์ด์ค๋ ์์ฒญ๋ค์ ๊ตญ์ ํ ๊ฒฝ๋ก๋ก ๋งคํ๋๋๋ก ์์ ํ์๋ค.
- request.ts
import {getRequestConfig} from 'next-intl/server';
import {hasLocale} from 'next-intl';
import {routing} from './routing';
export default getRequestConfig(async ({requestLocale}) => {
// Typically corresponds to the `[locale]` segment
const requested = await requestLocale;
const locale = hasLocale(routing.locales, requested)
? requested
: routing.defaultLocale;
return {
locale,
messages: (await import(`../../messages/${locale}.json`)).default
};
});
- ํ์ด์ง์์ ์ฌ์ฉ
import {useTranslations} from 'next-intl';
const t = useTranslations("Home")
useTranslations๋ฅผ Importํ๊ณ ๋ถ๋ฌ์ฌ json์ key๊ฐ์ผ๋ก ์ค์ ํด์ค๋ค.
{t('message')}
์ด๋ฐ ์์ผ๋ก ์ฌ์ฉํ๋ฉด ๋์ด๋ค. ํ์ฌ ์ค์ ๋ locale์ ๋ฐ๋ผ json ํ์ผ์์ ๊ฐ์ ์ฝ์ด์จ๋ค.
๊ฐ์ ๋ ์
๊ธฐ์กด ์ฒซ html ๋ ๋๋ง 84ms์, ๋ฒ์ญ ์๊ฐ 400ms ์ ๋ ๊ฑธ๋ฆฌ๋ ์๊ฐ์ 47.11ms๋ก ๋จ์ถํ ์ ์์๋ค. ๋ ์์ด๋ ๋ฒ์ญ๊ธฐ ์์ด๊ฐ ์๋ ์ง์ ์์ด ๋ฌธ๋จ์ ์์ ํ ์ ์์๋ค.
๋ง๋ฌด๋ฆฌ
Next.js๋ ๋๋ฌด ์ค๋๋ง์ด๋ผ ๊ธฐ์ ๋์ ์ ์๊ฐ์ด ์ข ๊ฑธ๋ฆฐ ๊ฒ ๊ฐ๋ค.. ๋ Next.js๋ ํ๋ก ํธ๋ก๋ง ์ฌ์ฉํด๋ดค์ง ๋ฐฑ์๋ ๋ก์ง๊น์ง ํจ๊ป ์๋ ํ๋ก์ ํธ๋ ์ฒ์์ด์์ง๋ง ์ด๋ค ์์ผ๋ก ๊ตฌ์ฑ๋์๋์ง ํ์ธํ ์ ์์๋ค. ๋ ๋ด๊ฐ ๋ง๋ ์ฌ์ดํธ๊ฐ ์ค์ ๋ก ์ด์๋๋ค๋ ์ ์ด ์ ์ผ ์ ๊ธฐํ๊ณ ์ฌ๋ฏธ์์๋ค. ๊ธฐํ๊ฐ ๋๋ค๋ฉด Next.js์ TypeScript๋ ๋ฐฐ์์ ํ์คํ์ผ๋ก ์ ํฅํ๊ณ ์ถ๋ค.