Ver3.0 ๐ ํ์ ์คํฌ๋ฆฝํธ๋ก ์ธ๊ณ๋ฅผ ์ ๋ณตํ์! ๊ตญ์ ํ์ i18n ์๋ฒฝ ๊ฐ์ด๋

๐ ํ์ ์คํฌ๋ฆฝํธ๋ก ์ธ๊ณ๋ฅผ ์ ๋ณตํ์! ๊ตญ์ ํ์ i18n ์๋ฒฝ ๊ฐ์ด๋
์๋ ! ์ค๋์ ์ ๋ง ์ฌ๋ฏธ์๋ ์ฃผ์ ๋ฅผ ๊ฐ์ง๊ณ ์์ด. ๋ฐ๋ก ํ์ ์คํฌ๋ฆฝํธ๋ก ๋ค๊ตญ์ด ์ง์ํ๊ธฐ์ผ! ๐
์์ฆ ์น ์๋น์ค๋ฅผ ๋ง๋ค๋ค ๋ณด๋ฉด ํ๊ตญ์ด๋ง ์ง์ํ๋ ๊ฑด ๋ญ๊ฐ ์์ฝ์์? ๋ฏธ๊ตญ ์น๊ตฌ๋ค๋ ์ฐ๊ณ , ์ผ๋ณธ ์น๊ตฌ๋ค๋ ์ฐ๊ณ , ํ๋์ค ์น๊ตฌ๋ค๋ ์ธ ์ ์๊ฒ ๋ง๋ค๊ณ ์ถ์๋ฐ... ๊ทธ๋ฐ๋ฐ ๋ง์ ์์ํ๋ ค๋๊น "์ด? ์ด๊ฑฐ ์ด๋ป๊ฒ ํ์ง?" ์ถ์ ๊ฑฐ์ผ. ํนํ ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฐ๋ฉด์ ํ์ ์์ ์ฑ๊น์ง ์ฑ๊ธฐ๋ ค๋ฉด ๋จธ๋ฆฌ๊ฐ ์ข ๋ณต์กํด์ง๊ฑฐ๋ . ๐
๊ทธ๋์ ์ค๋์ ์น๊ตฌํํ ์ค๋ช ํ๋ฏ์ด ํธํ๊ฒ, ๊ทธ๋ฌ๋ฉด์๋ ์ ๋๋ก ์๋ ค์ค๊ฒ. ๊ตญ์ ํ(i18n)๊ฐ ๋ญ์ง๋ถํฐ ์์ํด์, ์ค์ ๋ก ์ด๋ป๊ฒ ๊ตฌํํ๋์ง, ํ์ ์์คํ ์ ์ด๋ป๊ฒ ํ์ฉํ๋์ง๊น์ง ์น ๋ค ๋ค๋ค๋ณผ ๊ฑฐ์ผ!
๐ค i18n์ด ๋ญ๊ธธ๋?
๋จผ์ i18n์ด๋ผ๋ ์ฉ์ด๋ถํฐ ์์๋ณด์. ์ด๊ฑฐ ์ฒ์ ๋ณด๋ฉด "์์ด์ผํ์? ์ด๊ฒ ๋ญ์ผ?" ์ถ์ ๊ฑฐ์ผ. ์ฌ์ค ์ด๊ฑด internationalization์ ์ค์๋ง์ด์ผ. i๋ก ์์ํด์ n์ผ๋ก ๋๋๋๋ฐ, ๊ทธ ์ฌ์ด์ ๊ธ์๊ฐ 18๊ฐ ์์ด์ i18n์ด๋ผ๊ณ ๋ถ๋ฅด๋ ๊ฑฐ์ง. ํ๋ก๊ทธ๋๋จธ๋ค ํน์ ์ ๊ฒ์ผ๋ฆ(?)์ด ๋ง๋ค์ด๋ธ ์ฝ์ด์ผ. ๐
์ง์ญํ(l10n): ํน์ ์ธ์ด์ ์ง์ญ์ ๋ง๊ฒ ์ค์ ๋ก ๋ฒ์ญํ๊ณ ์ ์ฉํ๋ ๊ณผ์
์ฝ๊ฒ ๋งํ๋ฉด, ๊ตญ์ ํ๋ "๋ค๊ตญ์ด ์ง์์ด ๊ฐ๋ฅํ๋๋ก ๊ตฌ์กฐ๋ฅผ ๋ง๋๋ ๊ฒ"์ด๊ณ , ์ง์ญํ๋ "์ค์ ๋ก ํ๊ตญ์ด, ์์ด, ์ผ๋ณธ์ด ๋ฑ์ผ๋ก ๋ฒ์ญํ๋ ๊ฒ"์ด์ผ. ์ง์ ์ง์ ๋ ๊ตญ์ ํ๋ ์ค๊ณ๋๋ฅผ ๊ทธ๋ฆฌ๋ ๊ฑฐ๊ณ , ์ง์ญํ๋ ์ค์ ๋ก ๋ฒฝ๋์ ์๋ ๊ฑฐ๋ผ๊ณ ์๊ฐํ๋ฉด ๋ผ!
๐ฏ ์ ํ์ ์คํฌ๋ฆฝํธ์์ i18n์ด ์ค์ํ ๊น?
์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ค๊ตญ์ด ์ง์์ ๊ตฌํํ๋ฉด ์ด๋ฐ ์ํฉ์ด ์๊ฒจ:
// ์๋ฐ์คํฌ๋ฆฝํธ์์ ํํ ๋ฐ์ํ๋ ๋ฌธ์
const greeting = t('welcom'); // ์คํ! welcome์ด ์๋๋ผ welcom
console.log(greeting); // undefined ๋๋ ์๋ฌ
๋ณด์ด์ง? ํค ์ด๋ฆ์ ์๋ชป ์ฐ๋ฉด ๋ฐํ์์์์ผ ์๋ฌ๊ฐ ๋ฐ์ํด. ๊ทธ๊ฒ๋ ์ฌ์ฉ์๊ฐ ์ค์ ๋ก ๊ทธ ํ์ด์ง๋ฅผ ์ด์์ ๋! ๐ฑ
ํ์ง๋ง ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ ๋๋ก ํ์ฉํ๋ฉด:
// ํ์
์คํฌ๋ฆฝํธ์ ๋ง๋ฒ โจ
const greeting = t('welcom'); // ๋นจ๊ฐ ์ค! IDE๊ฐ ๋ฐ๋ก ์๋ ค์ค
// Error: Argument of type '"welcom"' is not assignable to parameter of type '"welcome" | "goodbye"'
ํนํ ํ๋ก์ ํธ๊ฐ ์ปค์ง๊ณ ๋ฒ์ญ ํค๊ฐ ์๋ฐฑ ๊ฐ๊ฐ ๋๋ฉด, ์ด๋ฐ ํ์ ์์ ์ฑ์ ์ ๋ง ์๋ช ์ค์ด ๋ผ. ์ค์ ๋ก ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์๋ ๋ค๊ตญ์ด ์ง์์ ํ ๋ ์ด๋ฐ ํ์ ์์คํ ์ด ์ผ๋ง๋ ์ค์ํ์ง ์ฒด๊ฐํ ์ ์์ ๊ฑฐ์ผ.
๐ ์ฃผ์ i18n ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ
ํ์ ์คํฌ๋ฆฝํธ์์ ์ฌ์ฉํ ์ ์๋ i18n ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ์ฌ๋ฌ ๊ฐ์ง๊ฐ ์์ด. ๊ฐ๊ฐ์ ํน์ง์ ์์๋ณด์!
1๏ธโฃ react-i18next
React ์ํ๊ณ์์ ๊ฐ์ฅ ์ธ๊ธฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ผ. i18next๋ฅผ React์ ๋ง๊ฒ ๋ํํ ๊ฑฐ์ง.
// react-i18next ๊ธฐ๋ณธ ์ค์
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
i18n
.use(initReactI18next)
.init({
resources: {
ko: {
translation: {
welcome: "ํ์ํฉ๋๋ค!",
goodbye: "์๋
ํ ๊ฐ์ธ์!"
}
},
en: {
translation: {
welcome: "Welcome!",
goodbye: "Goodbye!"
}
}
},
lng: 'ko',
fallbackLng: 'en',
interpolation: {
escapeValue: false
}
});
export default i18n;
์ฌ์ฉํ ๋๋ ์ด๋ ๊ฒ ๊ฐ๋จํด:
import { useTranslation } from 'react-i18next';
function MyComponent() {
const { t, i18n } = useTranslation();
const changeLanguage = (lng: string) => {
i18n.changeLanguage(lng);
};
return (
<div>
<h1>{t('welcome')}</h1>
<button onClick={() => changeLanguage('en')}>English</button>
<button onClick={() => changeLanguage('ko')}>ํ๊ตญ์ด</button>
</div>
);
}
2๏ธโฃ next-i18next
Next.js๋ฅผ ์ด๋ค๋ฉด ์ด๊ฒ ์ต๊ณ ์ผ! Next.js์ ๋ผ์ฐํ ์์คํ ๊ณผ ์๋ฒฝํ๊ฒ ํตํฉ๋ผ ์์ด.
โข SSR/SSG ์๋ฒฝ ์ง์
โข ํ์ผ ๊ธฐ๋ฐ ๋ผ์ฐํ ๊ณผ ํตํฉ
โข ์๋ ๋ฒ์ญ ํ์ผ ๋ก๋ฉ
// next-i18next.config.js
module.exports = {
i18n: {
defaultLocale: 'ko',
locales: ['ko', 'en', 'ja'],
localeDetection: true,
},
localePath: './public/locales',
};
// pages/_app.tsx
import { appWithTranslation } from 'next-i18next';
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
export default appWithTranslation(MyApp);
3๏ธโฃ vue-i18n
Vue.js ์ฌ์ฉ์๋ผ๋ฉด ๋น์ฐํ ์ด๊ฑฐ์ง! Vue 3์ Composition API๋ฅผ ์๋ฒฝํ๊ฒ ์ง์ํด.
// Vue 3 + TypeScript
import { createI18n } from 'vue-i18n';
const i18n = createI18n({
legacy: false,
locale: 'ko',
fallbackLocale: 'en',
messages: {
ko: {
message: {
hello: '์๋
ํ์ธ์!'
}
},
en: {
message: {
hello: 'Hello!'
}
}
}
});
// ์ปดํฌ๋ํธ์์ ์ฌ์ฉ
import { useI18n } from 'vue-i18n';
export default {
setup() {
const { t, locale } = useI18n();
return { t, locale };
}
};
4๏ธโฃ typesafe-i18n
์ด๊ฑด ์ง์ง ํ์ ์์ ์ฑ์ ๋ฏธ์น(?) ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ผ. 100% ํ์ ์ธ์ดํ๋ฅผ ๋ณด์ฅํด!
// ์๋ ์์ฑ๋ ํ์
type Translation = {
'welcome': string
'user.greeting': (name: string) => string
'items.count': (count: number) => string
}
// ์ฌ์ฉํ ๋
LL.welcome() // โ
OK
LL['user.greeting']('์ฒ ์') // โ
OK
LL.welcom() // โ ์ปดํ์ผ ์๋ฌ!
LL['user.greeting']() // โ ์ธ์ ๋๋ฝ ์๋ฌ!
๐ง ํ์ ์์คํ ๊ตฌ์ถํ๊ธฐ
์, ์ด์ ๋ณธ๊ฒฉ์ ์ผ๋ก ํ์ ์คํฌ๋ฆฝํธ์ ํ์ ์์คํ ์ ํ์ฉํด์ ์์ ํ i18n์ ๊ตฌ์ถํด๋ณด์! ๐จ
๊ธฐ๋ณธ ํ์ ์ ์
๋จผ์ ๋ฒ์ญ ๊ฐ์ฒด์ ๊ตฌ์กฐ๋ฅผ ํ์ ์ผ๋ก ์ ์ํด์ผ ํด:
// types/i18n.ts
export interface Translations {
common: {
welcome: string;
goodbye: string;
loading: string;
};
auth: {
login: string;
logout: string;
signup: string;
forgotPassword: string;
};
errors: {
notFound: string;
serverError: string;
unauthorized: string;
};
}
// ์ง์ํ๋ ์ธ์ด ํ์
export type Locale = 'ko' | 'en' | 'ja';
// ๋ฒ์ญ ํค ํ์
(์ค์ฒฉ๋ ๊ฐ์ฒด์ ๋ชจ๋ ๊ฒฝ๋ก)
export type TranslationKey =
| 'common.welcome'
| 'common.goodbye'
| 'common.loading'
| 'auth.login'
| 'auth.logout'
| 'auth.signup'
| 'auth.forgotPassword'
| 'errors.notFound'
| 'errors.serverError'
| 'errors.unauthorized';
๊ทผ๋ฐ ์ด๋ ๊ฒ ํ๋ํ๋ ํ์ดํํ๋ ๊ฑด ๋๋ฌด ๊ท์ฐฎ์์? ํ์ ์คํฌ๋ฆฝํธ์ ๊ณ ๊ธ ๊ธฐ๋ฅ์ ํ์ฉํ๋ฉด ์๋์ผ๋ก ๋ง๋ค ์ ์์ด!
์๋ ํ์ ์์ฑ ์ ํธ๋ฆฌํฐ
// ์ค์ฒฉ๋ ๊ฐ์ฒด์ ๋ชจ๋ ๊ฒฝ๋ก๋ฅผ ํ์
์ผ๋ก ์ถ์ถ
type PathsToStringProps<T> = T extends string
? []
: {
[K in Extract<keyof T, string>]: [K, ...PathsToStringProps<T[K]>];
}[Extract<keyof T, string>];
type Join<T extends string[], D extends string> = T extends []
? never
: T extends [infer F]
? F
: T extends [infer F, ...infer R]
? F extends string
? `${F}${D}${Join<Extract<R, string[]>, D>}`
: never
: string;
// ์ต์ข
ํ์
์์ฑ
export type TranslationKey = Join<PathsToStringProps<Translations>, '.'>;
// ์ด์ ์๋์ผ๋ก ๋ชจ๋ ๊ฒฝ๋ก๊ฐ ํ์
์ผ๋ก ์์ฑ๋ผ!
const key: TranslationKey = 'common.welcome'; // โ
const invalid: TranslationKey = 'common.invalid'; // โ ์๋ฌ!
๋งค๊ฐ๋ณ์๊ฐ ์๋ ๋ฒ์ญ ์ฒ๋ฆฌ
์ค์ ํ๋ก์ ํธ์์๋ ๋์ ์ธ ๊ฐ์ ๋ฃ์ด์ผ ํ ๋๊ฐ ๋ง์. ์๋ฅผ ๋ค์ด "์๋ ํ์ธ์, {{name}}๋!" ๊ฐ์ ๊ฑฐ ๋ง์ด์ผ.
// ๋งค๊ฐ๋ณ์ ํ์
์ ์
export interface TranslationParams {
'user.greeting': { name: string };
'items.count': { count: number };
'order.total': { amount: number; currency: string };
}
// ๋ฒ์ญ ํจ์ ํ์
type TranslateFn = <K extends TranslationKey>(
key: K,
...params: K extends keyof TranslationParams
? [TranslationParams[K]]
: []
) => string;
// ์ฌ์ฉ ์์
const t: TranslateFn = (key, params) => {
// ์ค์ ๋ฒ์ญ ๋ก์ง
return '๋ฒ์ญ๋ ๋ฌธ์์ด';
};
// ํ์
์์ ํ๊ฒ ์ฌ์ฉ
t('user.greeting', { name: '์ฒ ์' }); // โ
t('user.greeting'); // โ ๋งค๊ฐ๋ณ์ ํ์!
t('user.greeting', { age: 20 }); // โ ์๋ชป๋ ๋งค๊ฐ๋ณ์!
t('common.welcome'); // โ
๋งค๊ฐ๋ณ์ ๋ถํ์
๋ค์์คํ์ด์ค ๊ธฐ๋ฐ ํ์ ์์คํ
ํ๋ก์ ํธ๊ฐ ์ปค์ง๋ฉด ๋ฒ์ญ์ ๋ค์์คํ์ด์ค๋ณ๋ก ๋๋๋ ๊ฒ ์ข์. ์๋ฅผ ๋ค์ด common, auth, dashboard ์ด๋ฐ ์์ผ๋ก ๋ง์ด์ผ.
// ๋ค์์คํ์ด์ค๋ณ ํ์
์ ์
interface CommonTranslations {
welcome: string;
goodbye: string;
loading: string;
}
interface AuthTranslations {
login: string;
logout: string;
signup: string;
}
interface DashboardTranslations {
overview: string;
statistics: string;
settings: string;
}
// ์ ์ฒด ๋ฒ์ญ ํ์
interface AllTranslations {
common: CommonTranslations;
auth: AuthTranslations;
dashboard: DashboardTranslations;
}
// ๋ค์์คํ์ด์ค๋ฅผ ํฌํจํ ๋ฒ์ญ ํจ์
function createTypedTranslation<T extends AllTranslations>() {
return function t<
NS extends keyof T,
K extends keyof T[NS]
>(namespace: NS, key: K): string {
// ์ค์ ๋ฒ์ญ ๋ก์ง
return '';
};
}
const t = createTypedTranslation<AllTranslations>();
// ์ฌ์ฉ
t('common', 'welcome'); // โ
t('auth', 'login'); // โ
t('common', 'login'); // โ common์๋ login์ด ์์!
t('dashboard', 'invalid'); // โ ์กด์ฌํ์ง ์๋ ํค!
๐จ ์ค์ ๊ตฌํ ํจํด
์ด๋ก ์ ์ถฉ๋ถํ ๋ดค์ผ๋, ์ด์ ์ค์ ๋ก ์ด๋ป๊ฒ ๊ตฌํํ๋์ง ํจํด๋ณ๋ก ์์๋ณด์!
ํจํด 1: ์ปจํ ์คํธ ๊ธฐ๋ฐ ์ ๊ทผ
React์์ ๊ฐ์ฅ ๋ง์ด ์ฐ๋ ํจํด์ด์ผ. Context API๋ฅผ ํ์ฉํด์ ์ ์ญ์ ์ผ๋ก ๋ฒ์ญ ํจ์๋ฅผ ์ ๊ณตํ๋ ๊ฑฐ์ง.
// I18nContext.tsx
import React, { createContext, useContext, useState } from 'react';
interface I18nContextType {
locale: Locale;
setLocale: (locale: Locale) => void;
t: TranslateFn;
}
const I18nContext = createContext<I18nContextType | undefined>(undefined);
export function I18nProvider({ children }: { children: React.ReactNode }) {
const [locale, setLocale] = useState<Locale>('ko');
const t: TranslateFn = (key, params) => {
// ๋ฒ์ญ ๋ก์ง
const translation = translations[locale][key];
if (params) {
return Object.entries(params).reduce(
(str, [key, value]) => str.replace(`{{${key}}}`, String(value)),
translation
);
}
return translation;
};
return (
<I18nContext.Provider value={{ locale, setLocale, t }}>
{children}
</I18nContext.Provider>
);
}
export function useI18n() {
const context = useContext(I18nContext);
if (!context) {
throw new Error('useI18n must be used within I18nProvider');
}
return context;
}
// ์ฌ์ฉ
function MyComponent() {
const { t, locale, setLocale } = useI18n();
return (
<div>
<h1>{t('common.welcome')}</h1>
<p>{t('user.greeting', { name: '์ฒ ์' })}</p>
<button onClick={() => setLocale('en')}>English</button>
</div>
);
}
ํจํด 2: ํ ๊ธฐ๋ฐ ์ ๊ทผ
๋ ๊ฐ๋จํ๊ฒ ์ปค์คํ ํ ๋ง ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ๋ ์์ด:
// useTranslation.ts
import { useState, useCallback } from 'react';
export function useTranslation() {
const [locale, setLocale] = useState<Locale>('ko');
const t = useCallback<TranslateFn>(
(key, params) => {
const translation = getTranslation(locale, key);
return interpolate(translation, params);
},
[locale]
);
const changeLanguage = useCallback((newLocale: Locale) => {
setLocale(newLocale);
localStorage.setItem('locale', newLocale);
}, []);
return { t, locale, changeLanguage };
}
// ๋ฒ์ญ ๊ฐ์ ธ์ค๊ธฐ
function getTranslation(locale: Locale, key: TranslationKey): string {
const keys = key.split('.');
let value: any = translations[locale];
for (const k of keys) {
value = value[k];
if (value === undefined) {
console.warn(`Translation missing: ${key} for locale ${locale}`);
return key;
}
}
return value;
}
// ๋ณ์ ์นํ
function interpolate(template: string, params?: Record<string, any>): string {
if (!params) return template;
return template.replace(/\{\{(\w+)\}\}/g, (match, key) => {
return params[key] !== undefined ? String(params[key]) : match;
});
}
ํจํด 3: ๊ณ ์ฐจ ์ปดํฌ๋ํธ(HOC) ํจํด
ํด๋์ค ์ปดํฌ๋ํธ๋ฅผ ์ฐ๊ฑฐ๋ ๋ ๊ฑฐ์ ์ฝ๋๋ฅผ ๋ค๋ฃฐ ๋ ์ ์ฉํด:
// withTranslation.tsx
import React from 'react';
interface WithTranslationProps {
t: TranslateFn;
locale: Locale;
changeLanguage: (locale: Locale) => void;
}
export function withTranslation<P extends WithTranslationProps>(
Component: React.ComponentType<P>
) {
return function TranslatedComponent(
props: Omit<P, keyof WithTranslationProps>
) {
const { t, locale, changeLanguage } = useTranslation();
return (
<Component
{...(props as P)}
t={t}
locale={locale}
changeLanguage={changeLanguage}
/>
);
};
}
// ์ฌ์ฉ
interface MyComponentProps extends WithTranslationProps {
title: string;
}
class MyComponent extends React.Component<MyComponentProps> {
render() {
const { t, title } = this.props;
return <h1>{t('common.welcome')} - {title}</h1>;
}
}
export default withTranslation(MyComponent);
ํ ํจํด: ์ํ ํ๋ก์ ํธ, ๊ฐ๋จํ ๊ตฌ์กฐ
HOC ํจํด: ๋ ๊ฑฐ์ ์ฝ๋, ํด๋์ค ์ปดํฌ๋ํธ ์ฌ์ฉ
๐ ๊ณ ๊ธ ๊ธฐ๋ฅ ๊ตฌํํ๊ธฐ
1. ๋ณต์ํ ์ฒ๋ฆฌ (Pluralization)
์ธ์ด๋ง๋ค ๋ณต์ํ ๊ท์น์ด ๋ฌ๋ผ. ์์ด๋ 1๊ฐ์ผ ๋์ 2๊ฐ ์ด์์ผ ๋๋ง ๊ตฌ๋ถํ์ง๋ง, ๋ฌ์์์ด๋ ํจ์ฌ ๋ณต์กํด!
// ๋ณต์ํ ๊ท์น ์ ์
type PluralRule = 'zero' | 'one' | 'two' | 'few' | 'many' | 'other';
interface PluralTranslation {
zero?: string;
one?: string;
two?: string;
few?: string;
many?: string;
other: string;
}
// ์ธ์ด๋ณ ๋ณต์ํ ๊ท์น
function getPluralRule(locale: Locale, count: number): PluralRule {
const pluralRules = new Intl.PluralRules(locale);
return pluralRules.select(count) as PluralRule;
}
// ๋ณต์ํ ๋ฒ์ญ ํจ์
function translatePlural(
locale: Locale,
translation: PluralTranslation,
count: number
): string {
const rule = getPluralRule(locale, count);
const text = translation[rule] || translation.other;
return text.replace('{{count}}', String(count));
}
// ์ฌ์ฉ ์์
const itemTranslations = {
ko: {
other: '{{count}}๊ฐ์ ์์ดํ
'
},
en: {
one: '{{count}} item',
other: '{{count}} items'
}
};
console.log(translatePlural('en', itemTranslations.en, 1)); // "1 item"
console.log(translatePlural('en', itemTranslations.en, 5)); // "5 items"
console.log(translatePlural('ko', itemTranslations.ko, 5)); // "5๊ฐ์ ์์ดํ
"
2. ๋ ์ง์ ์ซ์ ํฌ๋งทํ
๋ฒ์ญ๋งํผ์ด๋ ์ค์ํ ๊ฒ ๋ ์ง์ ์ซ์ ํ์์ด์ผ. ํ๊ตญ์ "2024๋ 1์ 15์ผ"์ด์ง๋ง ๋ฏธ๊ตญ์ "January 15, 2024"์์?
// ๋ ์ง ํฌ๋งทํ
function formatDate(locale: Locale, date: Date, options?: Intl.DateTimeFormatOptions): string {
return new Intl.DateTimeFormat(locale, options).format(date);
}
// ์ซ์ ํฌ๋งทํ
function formatNumber(locale: Locale, number: number, options?: Intl.NumberFormatOptions): string {
return new Intl.NumberFormat(locale, options).format(number);
}
// ํตํ ํฌ๋งทํ
function formatCurrency(locale: Locale, amount: number, currency: string): string {
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency
}).format(amount);
}
// ์ฌ์ฉ ์์
const date = new Date('2024-01-15');
console.log(formatDate('ko', date)); // "2024. 1. 15."
console.log(formatDate('en', date)); // "1/15/2024"
console.log(formatDate('ja', date)); // "2024/1/15"
console.log(formatNumber('ko', 1234567.89)); // "1,234,567.89"
console.log(formatNumber('de', 1234567.89)); // "1.234.567,89"
console.log(formatCurrency('ko', 10000, 'KRW')); // "โฉ10,000"
console.log(formatCurrency('en', 10000, 'USD')); // "$10,000.00"
3. ์ง์ฐ ๋ก๋ฉ (Lazy Loading)
๋ชจ๋ ์ธ์ด์ ๋ฒ์ญ ํ์ผ์ ํ ๋ฒ์ ๋ก๋ํ๋ฉด ์ด๊ธฐ ๋ก๋ฉ์ด ๋๋ ค์ ธ. ํ์ํ ๋๋ง ๋ก๋ํ๋ ๊ฒ ์ข์!
// ๋ฒ์ญ ํ์ผ ๋์ ๋ก๋ฉ
const translationCache = new Map<Locale, AllTranslations>();
async function loadTranslations(locale: Locale): Promise<AllTranslations> {
// ์บ์ ํ์ธ
if (translationCache.has(locale)) {
return translationCache.get(locale)!;
}
// ๋์ import
const translations = await import(`./locales/${locale}.json`);
// ์บ์ ์ ์ฅ
translationCache.set(locale, translations.default);
return translations.default;
}
// React์์ ์ฌ์ฉ
function I18nProvider({ children }: { children: React.ReactNode }) {
const [locale, setLocale] = useState<Locale>('ko');
const [translations, setTranslations] = useState<AllTranslations | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
setLoading(true);
loadTranslations(locale)
.then(setTranslations)
.finally(() => setLoading(false));
}, [locale]);
if (loading || !translations) {
return <div>Loading...</div>;
}
// ... ๋๋จธ์ง ๋ก์ง
}
4. ๋ฒ์ญ ๋๋ฝ ๊ฐ์ง
๊ฐ๋ฐํ๋ค ๋ณด๋ฉด ๋ฒ์ญ์ ๊น๋นกํ๋ ๊ฒฝ์ฐ๊ฐ ์์ด. ์ด๊ฑธ ์๋์ผ๋ก ๊ฐ์งํ๋ฉด ์ข๊ฒ ์ง?
// ๋ฒ์ญ ๋๋ฝ ์ฒดํฌ ์ ํธ๋ฆฌํฐ
interface MissingTranslation {
key: TranslationKey;
locale: Locale;
}
class TranslationValidator {
private missingTranslations: MissingTranslation[] = [];
validate(locales: Locale[], baseLocale: Locale = 'ko'): MissingTranslation[] {
this.missingTranslations = [];
const baseKeys = this.getAllKeys(translations[baseLocale]);
for (const locale of locales) {
if (locale === baseLocale) continue;
const localeKeys = this.getAllKeys(translations[locale]);
for (const key of baseKeys) {
if (!localeKeys.includes(key)) {
this.missingTranslations.push({ key: key as TranslationKey, locale });
}
}
}
return this.missingTranslations;
}
private getAllKeys(obj: any, prefix = ''): string[] {
let keys: string[] = [];
for (const [key, value] of Object.entries(obj)) {
const fullKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'object' && value !== null) {
keys = keys.concat(this.getAllKeys(value, fullKey));
} else {
keys.push(fullKey);
}
}
return keys;
}
report(): void {
if (this.missingTranslations.length === 0) {
console.log('โ
All translations are complete!');
return;
}
console.warn('โ ๏ธ Missing translations:');
for (const { key, locale } of this.missingTranslations) {
console.warn(` - ${locale}: ${key}`);
}
}
}
// ์ฌ์ฉ
const validator = new TranslationValidator();
const missing = validator.validate(['ko', 'en', 'ja']);
validator.report();
๐ฏ ์ค์ ํ๋ก์ ํธ ๊ตฌ์กฐ
์ด์ ์ค์ ํ๋ก์ ํธ์์ ์ด๋ป๊ฒ ๊ตฌ์กฐ๋ฅผ ์ก๋์ง ์์๋ณด์. ์ด๊ฑด ๋ด๊ฐ ์ค๋ฌด์์ ์จ๋ณธ ๊ตฌ์กฐ์ผ!
src/
โโโ i18n/
โ โโโ locales/
โ โ โโโ ko/
โ โ โ โโโ common.json
โ โ โ โโโ auth.json
โ โ โ โโโ dashboard.json
โ โ โ โโโ errors.json
โ โ โโโ en/
โ โ โ โโโ common.json
โ โ โ โโโ auth.json
โ โ โ โโโ dashboard.json
โ โ โ โโโ errors.json
โ โ โโโ ja/
โ โ โโโ common.json
โ โ โโโ auth.json
โ โ โโโ dashboard.json
โ โ โโโ errors.json
โ โโโ types/
โ โ โโโ translations.ts
โ โ โโโ locale.ts
โ โ โโโ params.ts
โ โโโ utils/
โ โ โโโ interpolate.ts
โ โ โโโ pluralize.ts
โ โ โโโ format.ts
โ โโโ hooks/
โ โ โโโ useTranslation.ts
โ โ โโโ useLocale.ts
โ โโโ context/
โ โ โโโ I18nContext.tsx
โ โโโ config.ts
โ โโโ index.ts
โโโ ...
config.ts - ์ค์ ํ์ผ
// i18n/config.ts
export const i18nConfig = {
// ์ง์ํ๋ ์ธ์ด
locales: ['ko', 'en', 'ja'] as const,
// ๊ธฐ๋ณธ ์ธ์ด
defaultLocale: 'ko' as const,
// ํด๋ฐฑ ์ธ์ด
fallbackLocale: 'en' as const,
// ๋ค์์คํ์ด์ค
namespaces: ['common', 'auth', 'dashboard', 'errors'] as const,
// ๋ก์ปฌ ์คํ ๋ฆฌ์ง ํค
storageKey: 'app_locale',
// ๋๋ฒ๊ทธ ๋ชจ๋
debug: process.env.NODE_ENV === 'development',
// ๋๋ฝ๋ ๋ฒ์ญ ์ฒ๋ฆฌ
missingKeyHandler: (locale: string, key: string) => {
if (i18nConfig.debug) {
console.warn(`Missing translation: ${key} for locale ${locale}`);
}
return key;
}
} as const;
export type I18nConfig = typeof i18nConfig;
export type SupportedLocale = typeof i18nConfig.locales[number];
export type Namespace = typeof i18nConfig.namespaces[number];
index.ts - ์ง์ ์
// i18n/index.ts
export { I18nProvider, useI18n } from './context/I18nContext';
export { useTranslation } from './hooks/useTranslation';
export { useLocale } from './hooks/useLocale';
export { i18nConfig } from './config';
export type {
SupportedLocale,
Namespace,
TranslationKey,
TranslationParams
} from './types';
// ํธ์ ํจ์๋ค
export { formatDate, formatNumber, formatCurrency } from './utils/format';
export { pluralize } from './utils/pluralize';
๐ ํ ์คํ ์ ๋ต
i18n ์์คํ ๋ ๋น์ฐํ ํ ์คํธํด์ผ ํด! ํนํ ํ์ ์์ ์ฑ์ ๊ฒ์ฆํ๋ ๊ฒ ์ค์ํ์ง.
๋จ์ ํ ์คํธ
// __tests__/i18n.test.ts
import { describe, it, expect } from 'vitest';
import { useTranslation } from '../i18n';
describe('Translation System', () => {
it('should translate simple keys', () => {
const { t } = useTranslation();
expect(t('common.welcome')).toBe('ํ์ํฉ๋๋ค!');
});
it('should interpolate parameters', () => {
const { t } = useTranslation();
expect(t('user.greeting', { name: '์ฒ ์' })).toBe('์๋
ํ์ธ์, ์ฒ ์๋!');
});
it('should handle missing translations', () => {
const { t } = useTranslation();
expect(t('nonexistent.key' as any)).toBe('nonexistent.key');
});
it('should change locale', () => {
const { t, changeLanguage } = useTranslation();
expect(t('common.welcome')).toBe('ํ์ํฉ๋๋ค!');
changeLanguage('en');
expect(t('common.welcome')).toBe('Welcome!');
});
});
ํ์ ํ ์คํธ
ํ์ ์คํฌ๋ฆฝํธ์ ํ์ ์์คํ ์์ฒด๋ฅผ ํ ์คํธํ ์๋ ์์ด!
// __tests__/types.test.ts
import { expectType, expectError } from 'tsd';
import type { TranslationKey, TranslateFn } from '../i18n/types';
// ์ฌ๋ฐ๋ฅธ ํค๋ ํ์ฉ
expectType<TranslationKey>('common.welcome');
expectType<TranslationKey>('auth.login');
// ์๋ชป๋ ํค๋ ์๋ฌ
expectError<TranslationKey>('invalid.key');
// ๋ฒ์ญ ํจ์ ํ์
๊ฒ์ฆ
declare const t: TranslateFn;
// ๋งค๊ฐ๋ณ์ ์๋ ๋ฒ์ญ
expectType<string>(t('common.welcome'));
// ๋งค๊ฐ๋ณ์ ์๋ ๋ฒ์ญ
expectType<string>(t('user.greeting', { name: 'test' }));
// ์๋ชป๋ ๋งค๊ฐ๋ณ์๋ ์๋ฌ
expectError(t('user.greeting', { age: 20 }));
expectError(t('user.greeting')); // ๋งค๊ฐ๋ณ์ ๋๋ฝ
ํตํฉ ํ ์คํธ
// __tests__/integration.test.tsx
import { render, screen, fireEvent } from '@testing-library/react';
import { I18nProvider } from '../i18n';
import MyComponent from '../components/MyComponent';
describe('I18n Integration', () => {
it('should render translated content', () => {
render(
<I18nProvider>
<MyComponent />
</I18nProvider>
);
expect(screen.getByText('ํ์ํฉ๋๋ค!')).toBeInTheDocument();
});
it('should change language on button click', () => {
render(
<I18nProvider>
<MyComponent />
</I18nProvider>
);
const button = screen.getByText('English');
fireEvent.click(button);
expect(screen.getByText('Welcome!')).toBeInTheDocument();
});
});
โก ์ฑ๋ฅ ์ต์ ํ
i18n ์์คํ ์ด ๋๋ฆฌ๋ฉด ์ฌ์ฉ์ ๊ฒฝํ์ด ๋๋น ์ ธ. ๋ช ๊ฐ์ง ์ต์ ํ ๊ธฐ๋ฒ์ ์์๋ณด์!
1. ๋ฉ๋ชจ์ด์ ์ด์
import { useMemo } from 'react';
function useTranslation() {
const [locale] = useLocale();
// ๋ฒ์ญ ํจ์ ๋ฉ๋ชจ์ด์ ์ด์
const t = useMemo<TranslateFn>(() => {
return (key, params) => {
const translation = getTranslation(locale, key);
return params ? interpolate(translation, params) : translation;
};
}, [locale]);
return { t, locale };
}
// ๋ฒ์ญ ๊ฒฐ๊ณผ ์บ์ฑ
const translationCache = new Map<string, string>();
function getTranslation(locale: Locale, key: TranslationKey): string {
const cacheKey = `${locale}:${key}`;
if (translationCache.has(cacheKey)) {
return translationCache.get(cacheKey)!;
}
const translation = fetchTranslation(locale, key);
translationCache.set(cacheKey, translation);
return translation;
}
2. ์ฝ๋ ์คํ๋ฆฌํ
// ๋ผ์ฐํธ๋ณ๋ก ๋ฒ์ญ ํ์ผ ๋ถ๋ฆฌ
const DashboardPage = lazy(() =>
Promise.all([
import('./pages/Dashboard'),
import('./i18n/locales/ko/dashboard.json')
]).then(([module]) => module)
);
// ๋ค์์คํ์ด์ค๋ณ ์ง์ฐ ๋ก๋ฉ
async function loadNamespace(locale: Locale, namespace: Namespace) {
const translations = await import(
`./i18n/locales/${locale}/${namespace}.json`
);
return translations.default;
}
3. ๋ฒ๋ค ํฌ๊ธฐ ์ต์ ํ
// webpack.config.js
module.exports = {
optimization: {
splitChunks: {
cacheGroups: {
i18n: {
test: /[\\/]i18n[\\/]locales[\\/]/,
name: 'i18n',
chunks: 'all',
priority: 10
}
}
}
}
};
// ์ฌ์ฉํ์ง ์๋ ๋ฒ์ญ ์ ๊ฑฐ
// tree-shaking์ ์ํด named export ์ฌ์ฉ
export const translations = {
common: {
welcome: 'ํ์ํฉ๋๋ค!'
}
} as const;
๐ ๏ธ ์ค๋ฌด ํ๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
์ด์ ์ค๋ฌด์์ ๋ฐ๋ก ์ธ ์ ์๋ ํ๋ค์ ๊ณต์ ํ ๊ฒ! ์ด๊ฑด ์ง์ง ๊ฒฝํ์์ ๋์จ ๊ฑฐ์ผ. ๐
โ DO - ์ด๋ ๊ฒ ํ์ธ์
1. ๋ฒ์ญ ํค๋ ์๋ฏธ ์๊ฒ ์์ฑํ๊ธฐ
// โ
Good
'auth.login.button.submit'
'dashboard.user.profile.edit'
'errors.validation.email.invalid'
// โ Bad
'button1'
'text123'
'error_msg'
2. ๋ค์์คํ์ด์ค ํ์ฉํ๊ธฐ
// โ
Good - ๊ธฐ๋ฅ๋ณ๋ก ๋ถ๋ฆฌ
{
common: { ... },
auth: { ... },
dashboard: { ... }
}
// โ Bad - ๋ชจ๋ ๋ฒ์ญ์ ํ ํ์ผ์
{
welcome: '...',
login: '...',
dashboard_title: '...'
}
3. ๋ณ์๋ช ์ ๋ช ํํ๊ฒ
// โ
Good
t('user.greeting', { userName: '์ฒ ์', loginCount: 5 })
// โ Bad
t('user.greeting', { x: '์ฒ ์', n: 5 })
โ DON'T - ์ด๊ฑด ํผํ์ธ์
1. ๋ฒ์ญ ์์ HTML ๋ฃ์ง ์๊ธฐ
// โ Bad
{
"message": "<strong>์๋
ํ์ธ์</strong> {{name}}๋"
}
// โ
Good - ์ปดํฌ๋ํธ์์ ์ฒ๋ฆฌ
{
"greeting": "์๋
ํ์ธ์",
"name": "{{name}}๋"
}
<div>
<strong>{t('greeting')}</strong> {t('name', { name })}
</div>
2. ๋ฌธ์ฅ์ ์ชผ๊ฐ์ง ์๊ธฐ
// โ Bad - ์ธ์ด๋ง๋ค ์ด์์ด ๋ค๋ฆ
{
"you_have": "๋น์ ์",
"items": "๊ฐ์ ์์ดํ
์",
"have": "๊ฐ์ง๊ณ ์์ต๋๋ค"
}
// โ
Good - ์์ ํ ๋ฌธ์ฅ์ผ๋ก
{
"items_count": "{{count}}๊ฐ์ ์์ดํ
์ ๊ฐ์ง๊ณ ์์ต๋๋ค"
}
3. ํ๋์ฝ๋ฉ๋ ํ ์คํธ ๋จ๊ธฐ์ง ์๊ธฐ
// โ Bad
<button>ํ์ธ</button>
// โ
Good
<button>{t('common.confirm')}</button>
๐ ์ค์ ์์ : ์์ ํ ๋ค๊ตญ์ด ์ฑ ๋ง๋ค๊ธฐ
์, ์ด์ ๋ฐฐ์ด ๊ฑธ ๋ค ํฉ์ณ์ ์ค์ ๋ก ๋์ํ๋ ์ฑ์ ๋ง๋ค์ด๋ณด์! ๊ฐ๋จํ ํ ์ผ ๊ด๋ฆฌ ์ฑ์ ๋ง๋ค ๊ฑฐ์ผ.
1๋จ๊ณ: ํ๋ก์ ํธ ์ค์
// package.json
{
"dependencies": {
"react": "^18.2.0",
"typescript": "^5.0.0",
"i18next": "^23.0.0",
"react-i18next": "^13.0.0"
}
}
// tsconfig.json
{
"compilerOptions": {
"strict": true,
"esModuleInterop": true,
"resolveJsonModule": true
}
}
2๋จ๊ณ: ๋ฒ์ญ ํ์ผ ์์ฑ
// locales/ko/translation.json
{
"app": {
"title": "ํ ์ผ ๊ด๋ฆฌ",
"subtitle": "์ค๋ ํ ์ผ์ ๊ด๋ฆฌํ์ธ์"
},
"todo": {
"add": "ํ ์ผ ์ถ๊ฐ",
"placeholder": "๋ฌด์์ ํ์๊ฒ ์ด์?",
"empty": "ํ ์ผ์ด ์์ต๋๋ค",
"completed": "์๋ฃ๋จ",
"delete": "์ญ์ ",
"count": "{{count}}๊ฐ์ ํ ์ผ"
},
"language": {
"select": "์ธ์ด ์ ํ",
"ko": "ํ๊ตญ์ด",
"en": "English",
"ja": "ๆฅๆฌ่ช"
}
}
// locales/en/translation.json
{
"app": {
"title": "Todo Manager",
"subtitle": "Manage your daily tasks"
},
"todo": {
"add": "Add Todo",
"placeholder": "What do you want to do?",
"empty": "No todos yet",
"completed": "Completed",
"delete": "Delete",
"count_one": "{{count}} todo",
"count_other": "{{count}} todos"
},
"language": {
"select": "Select Language",
"ko": "ํ๊ตญ์ด",
"en": "English",
"ja": "ๆฅๆฌ่ช"
}
}
3๋จ๊ณ: ํ์ ์ ์
// types/i18n.ts
import translation from '../locales/ko/translation.json';
export type TranslationResource = typeof translation;
declare module 'i18next' {
interface CustomTypeOptions {
defaultNS: 'translation';
resources: {
translation: TranslationResource;
};
}
}
4๋จ๊ณ: i18n ์ค์
// i18n/config.ts
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import ko from '../locales/ko/translation.json';
import en from '../locales/en/translation.json';
import ja from '../locales/ja/translation.json';
i18n
.use(initReactI18next)
.init({
resources: {
ko: { translation: ko },
en: { translation: en },
ja: { translation: ja }
},
lng: localStorage.getItem('locale') || 'ko',
fallbackLng: 'ko',
interpolation: {
escapeValue: false
}
});
export default i18n;
5๋จ๊ณ: ์ปดํฌ๋ํธ ์์ฑ
// App.tsx
import { useState } from 'react';
import { useTranslation } from 'react-i18next';
interface Todo {
id: number;
text: string;
completed: boolean;
}
function App() {
const { t, i18n } = useTranslation();
const [todos, setTodos] = useState<Todo[]>([]);
const [input, setInput] = useState('');
const addTodo = () => {
if (!input.trim()) return;
setTodos([
...todos,
{ id: Date.now(), text: input, completed: false }
]);
setInput('');
};
const toggleTodo = (id: number) => {
setTodos(todos.map(todo =>
todo.id === id ? { ...todo, completed: !todo.completed } : todo
));
};
const deleteTodo = (id: number) => {
setTodos(todos.filter(todo => todo.id !== id));
};
const changeLanguage = (lng: string) => {
i18n.changeLanguage(lng);
localStorage.setItem('locale', lng);
};
return (
<div style={{ maxWidth: '600px', margin: '0 auto', padding: '20px' }}>
<header>
<h1>{t('app.title')}</h1>
<p>{t('app.subtitle')}</p>
<select
value={i18n.language}
onChange={(e) => changeLanguage(e.target.value)}
>
<option value="ko">{t('language.ko')}</option>
<option value="en">{t('language.en')}</option>
<option value="ja">{t('language.ja')}</option>
</select>
</header>
<div>
<input
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && addTodo()}
placeholder={t('todo.placeholder')}
/>
<button onClick={addTodo}>{t('todo.add')}</button>
</div>
<p>{t('todo.count', { count: todos.length })}</p>
{todos.length === 0 ? (
<p>{t('todo.empty')}</p>
) : (
<ul>
{todos.map(todo => (
<li key={todo.id}>
<input
type="checkbox"
checked={todo.completed}
onChange={() => toggleTodo(todo.id)}
/>
<span style={{
textDecoration: todo.completed ? 'line-through' : 'none'
}}>
{todo.text}
</span>
{todo.completed && <span> ({t('todo.completed')})</span>}
<button onClick={() => deleteTodo(todo.id)}>
{t('todo.delete')}
</button>
</li>
))}
</ul>
)}
</div>
);
}
export default App;
์ง์! ๐ ์ด์ ์์ ํ ํ์ ์์ ํ ๋ค๊ตญ์ด ์ฑ์ด ์์ฑ๋์ด. IDE์์ ์๋์์ฑ๋ ๋๊ณ , ์คํ๋ ๋ฐ๋ก ์ก์์ฃผ์ง!
๐จ ํํ ์ค์์ ํด๊ฒฐ๋ฒ
์ค๋ฌด์์ ์์ฃผ ๋ง์ฃผ์น๋ ๋ฌธ์ ๋ค๊ณผ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํด๋ดค์ด.
์์ธ: ๋ฒ์ญ ํ์ผ์ ์์ ํ๋๋ฐ ์บ์ ๋๋ฌธ์ ๋ฐ์์ด ์ ๋จ
ํด๊ฒฐ: ๊ฐ๋ฐ ๋ชจ๋์์๋ ์บ์ ๋นํ์ฑํ
i18n.init({
cache: {
enabled: process.env.NODE_ENV === 'production'
}
});
์์ธ: ํ์ ์ ์๊ฐ ์ ๋๋ก import๋์ง ์์
ํด๊ฒฐ: tsconfig.json์์ ํ์ ๊ฒฝ๋ก ํ์ธ
{
"compilerOptions": {
"typeRoots": ["./node_modules/@types", "./src/types"]
}
}
์์ธ: ๊น์ ์ค์ฒฉ ๊ตฌ์กฐ
ํด๊ฒฐ: ๋ค์์คํ์ด์ค ํ์ฉ
// โ Bad
t('pages.dashboard.user.profile.settings.general.name')
// โ
Good
const { t } = useTranslation('dashboard');
t('user.profile.name')
๐ ํ๋ก์ ํธ ํ์ฅํ๊ธฐ
ํ๋ก์ ํธ๊ฐ ์ปค์ง๋ฉด์ ๊ณ ๋ คํด์ผ ํ ๊ฒ๋ค์ ์์๋ณด์.
1. ๋ฒ์ญ ๊ด๋ฆฌ ์์คํ ๋์
๋ฒ์ญ๊ฐ์ ํ์ ํ ๋๋ ์ ๋ฌธ ๋๊ตฌ๋ฅผ ์ฐ๋ ๊ฒ ์ข์:
โข Crowdin: ์คํ์์ค ํ๋ก์ ํธ์ ์ข์
โข Phrase: ๊ฐ๋ฐ์ ์นํ์
โข POEditor: ๊ฐ์ฑ๋น ์ข์
2. CI/CD ํตํฉ
// .github/workflows/i18n.yml
name: Check Translations
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: npm install
- name: Check missing translations
run: npm run i18n:validate
- name: Check type safety
run: npm run type-check
3. ์๋ํ ์คํฌ๋ฆฝํธ
// scripts/extract-translations.ts
import * as fs from 'fs';
import * as path from 'path';
// ์์ค ์ฝ๋์์ ์ฌ์ฉ๋ ๋ฒ์ญ ํค ์ถ์ถ
function extractTranslationKeys(sourceDir: string): Set<string> {
const keys = new Set<string>();
const files = getAllFiles(sourceDir);
for (const file of files) {
const content = fs.readFileSync(file, 'utf-8');
// t('key') ํจํด ์ฐพ๊ธฐ
const matches = content.matchAll(/t\(['"]([^'"]+)['"]\)/g);
for (const match of matches) {
keys.add(match[1]);
}
}
return keys;
}
// ์ฌ์ฉ๋์ง ์๋ ๋ฒ์ญ ํค ์ฐพ๊ธฐ
function findUnusedKeys(
usedKeys: Set<string>,
translationKeys: Set<string>
): string[] {
return Array.from(translationKeys).filter(key => !usedKeys.has(key));
}
// ์คํ
const usedKeys = extractTranslationKeys('./src');
const translationKeys = getAllTranslationKeys('./locales');
const unused = findUnusedKeys(usedKeys, translationKeys);
console.log(`Found ${unused.length} unused translation keys:`);
unused.forEach(key => console.log(` - ${key}`));
๐ ํ์ต ๋ฆฌ์์ค
๋ ๊น์ด ๊ณต๋ถํ๊ณ ์ถ๋ค๋ฉด ์ด๋ฐ ์๋ฃ๋ค์ ์ถ์ฒํด:
โข i18next ๊ณต์ ๋ฌธ์: https://www.i18next.com/
โข react-i18next: https://react.i18next.com/
โข TypeScript ํธ๋๋ถ: https://www.typescriptlang.org/docs/
โข Udemy: "React Internationalization with i18next"
โข Frontend Masters: "TypeScript Fundamentals"
โข YouTube: "i18n Best Practices" ์๋ฆฌ์ฆ
โข i18next Discord ์๋ฒ
โข Stack Overflow์ i18next ํ๊ทธ
โข Reddit์ r/typescript ์ปค๋ฎค๋ํฐ
๊ทธ๋ฆฌ๊ณ ์ฌ๋ฅ๋ท์์๋ i18n์ด๋ TypeScript ๊ด๋ จ ์ฌ๋ฅ์ ๊ฐ์ง ์ ๋ฌธ๊ฐ๋ค์ ๋ง๋ ์ ์์ด. ๋งํ๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ๋์์ ๋ฐ๋ ๊ฒ๋ ์ข์ ๋ฐฉ๋ฒ์ด์ผ!
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์, ์ฌ๊ธฐ๊น์ง ์ฝ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ๐ ํ์ ์คํฌ๋ฆฝํธ๋ก ์์ ํ๊ณ ํ์ฅ ๊ฐ๋ฅํ ๊ตญ์ ํ ์์คํ ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ค ๋ฐฐ์ ์ด.
ํต์ฌ์ ์ ๋ฆฌํ๋ฉด:
2๏ธโฃ ๊ตฌ์กฐํ: ๋ค์์คํ์ด์ค์ ๋ชจ๋ํ๋ก ๊ด๋ฆฌํ๊ธฐ ์ฌ์ด ๊ตฌ์กฐ ๋ง๋ค๊ธฐ
3๏ธโฃ ์ฑ๋ฅ: ์ง์ฐ ๋ก๋ฉ๊ณผ ์บ์ฑ์ผ๋ก ๋น ๋ฅธ ์ฌ์ฉ์ ๊ฒฝํ ์ ๊ณต
4๏ธโฃ ํ์ฅ์ฑ: ํ๋ก์ ํธ๊ฐ ์ปค์ ธ๋ ์ ์ง๋ณด์๊ฐ ์ฌ์ด ์์คํ ๊ตฌ์ถ
5๏ธโฃ ์๋ํ: ํ ์คํธ์ ๊ฒ์ฆ์ ์๋ํํด์ ์ค์ ์ค์ด๊ธฐ
์ฒ์์๋ ๋ณต์กํด ๋ณด์ผ ์ ์์ง๋ง, ํ ๋ฒ ์ ๋๋ก ๊ตฌ์ถํด๋๋ฉด ์ ๋ง ํธํด. ํนํ ๊ธ๋ก๋ฒ ์๋น์ค๋ฅผ ๋ง๋ค ๋๋ ํ์์ผ!
์ด์ ๋น์ ๋ ์ธ๊ณ๋ฅผ ์๋๋ก ์๋น์ค๋ฅผ ๋ง๋ค ์ค๋น๊ฐ ๋์ด. ํ๊ตญ ์ฌ์ฉ์๋ฟ๋ง ์๋๋ผ ์ ์ธ๊ณ ์ฌ๋๋ค์ด ๋น์ ์ ์๋น์ค๋ฅผ ํธํ๊ฒ ์ธ ์ ์๊ฒ ๋๋ ๊ฑฐ์ง. ๋ฉ์ง์ง ์์? ๐โจ
๊ถ๊ธํ ์ ์ด ์๊ฑฐ๋ ๋งํ๋ ๋ถ๋ถ์ด ์์ผ๋ฉด ์ธ์ ๋ ์ปค๋ฎค๋ํฐ์ ๋ฌผ์ด๋ด. ๊ทธ๋ฆฌ๊ณ ์ค์ ๋ก ํ๋ก์ ํธ์ ์ ์ฉํด๋ณด๋ฉด์ ๋ฐฐ์ฐ๋ ๊ฒ ๊ฐ์ฅ ์ข์. ์์ ํ๋ก์ ํธ๋ถํฐ ์์ํด์ ์ ์ ํ์ฅํด๋๊ฐ๋ด!
ํ์ดํ ! ๐
๐ ์ด ๊ธ์ด ๋์์ด ๋์๋ค๋ฉด ๋ค๋ฅธ ๊ฐ๋ฐ์๋ค๊ณผ๋ ๊ณต์ ํด์ฃผ์ธ์! ๐
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.