Skip to content

Simple and powerful internationalization SDK for React applications. Features hua-api style translation system with fast performance and developer-friendly API. Includes TypeScript support, multi-language support, and debug tools.

License

Notifications You must be signed in to change notification settings

HUA-Labs/i18n-sdk

hua-i18n-sdk

v1.2.0 - React μ• ν”Œλ¦¬μΌ€μ΄μ…˜μ„ μœ„ν•œ κ°„λ‹¨ν•˜κ³  κ°•λ ₯ν•œ κ΅­μ œν™” SDK

npm version License: MIT TypeScript GitHub stars

⭐ 이 ν”„λ‘œμ νŠΈκ°€ 도움이 λ˜μ—ˆλ‹€λ©΄ μŠ€νƒ€λ₯Ό λˆŒλŸ¬μ£Όμ„Έμš”!

μ£Όμš” νŠΉμ§•

  • κ°„λ‹¨ν•œ API: 직관적이고 μ‚¬μš©ν•˜κΈ° μ‰¬μš΄ μΈν„°νŽ˜μ΄μŠ€
  • 초보자 μΉœν™”μ : withDefaultConfig()둜 ν•œ 쀄 μ„€μ •
  • νƒ€μž… μ•ˆμ „μ„±: TypeScript둜 μ™„μ „ν•œ νƒ€μž… 지원
  • SSR 지원: Next.js μ„œλ²„ μ»΄ν¬λ„ŒνŠΈμ™€ μ™„λ²½ ν˜Έν™˜
  • κ°•λ ₯ν•œ μ—λŸ¬ 처리: μžλ™ μž¬μ‹œλ„, 볡ꡬ μ „λž΅, μ‚¬μš©μž μΉœν™”μ  λ©”μ‹œμ§€
  • κ°€λ²Όμš΄ λ²ˆλ“€: Tree-shaking μ§€μ›μœΌλ‘œ μ΅œμ ν™”λœ 크기
  • μ‹€μ‹œκ°„ μ–Έμ–΄ λ³€κ²½: 동적 μ–Έμ–΄ μ „ν™˜ 지원
  • 개발자 μΉœν™”μ : 디버그 λͺ¨λ“œ, λˆ„λ½ ν‚€ ν‘œμ‹œ, μƒμ„Έν•œ λ‘œκΉ…

μ„€μΉ˜

npm install hua-i18n-sdk
# λ˜λŠ”
yarn add hua-i18n-sdk
# λ˜λŠ”
pnpm add hua-i18n-sdk

λΉ λ₯Έ μ‹œμž‘

πŸš€ 초보자용 (μΆ”μ²œ) - Easy Entry Point

// 초보자 μ „μš© μ—”νŠΈλ¦¬ν¬μΈνŠΈ - λ³΅μž‘ν•œ μ„€μ • 없이 λ°”λ‘œ μ‹œμž‘!
import { withDefaultConfig, useTranslation } from 'hua-i18n-sdk/easy';

// ν•œ μ€„λ‘œ 끝! κΈ°λ³Έ μ„€μ •μœΌλ‘œ μ‹œμž‘
export const I18nProvider = withDefaultConfig();

function App() {
  return (
    <I18nProvider>
      <MyComponent />
    </I18nProvider>
  );
}

function MyComponent() {
  const { t, tWithParams } = useTranslation();
  
  return (
    <div>
      <h1>{t('common.welcome')}</h1>
      <p>{tWithParams('common.greeting', { name: '철수' })}</p>
    </div>
  );
}

πŸš€ 초보자용 (μΆ”μ²œ) - κΈ°λ³Έ μ—”νŠΈλ¦¬ν¬μΈνŠΈ

import { withDefaultConfig, useTranslation } from 'hua-i18n-sdk';

// ν•œ μ€„λ‘œ 끝! κΈ°λ³Έ μ„€μ •μœΌλ‘œ μ‹œμž‘
export const I18nProvider = withDefaultConfig();

function App() {
  return (
    <I18nProvider>
      <MyComponent />
    </I18nProvider>
  );
}

function MyComponent() {
  const { t, tWithParams } = useTranslation();
  
  return (
    <div>
      <h1>{t('common.welcome')}</h1>
      <p>{tWithParams('common.greeting', { name: '철수' })}</p>
    </div>
  );
}

βš™οΈ μ€‘κΈ‰μžμš© (일뢀 μ»€μŠ€ν„°λ§ˆμ΄μ§•)

import { withDefaultConfig } from 'hua-i18n-sdk';

// ν•„μš”ν•œ λΆ€λΆ„λ§Œ μ»€μŠ€ν„°λ§ˆμ΄μ§•
export const I18nProvider = withDefaultConfig({
  defaultLanguage: 'en',
  namespaces: ['common', 'auth'],
  debug: true,
});

πŸ”§ κ³ κΈ‰μžμš© (μ™„μ „ μ»€μŠ€ν„°λ§ˆμ΄μ§•)

import { I18nProvider, useTranslation, createI18nConfig } from 'hua-i18n-sdk';

const i18nConfig = createI18nConfig({
  defaultLanguage: 'ko',
  fallbackLanguage: 'en',
  supportedLanguages: [
    { code: 'ko', name: 'Korean', nativeName: 'ν•œκ΅­μ–΄' },
    { code: 'en', name: 'English', nativeName: 'English' },
  ],
  namespaces: ['common', 'auth'],
  loadTranslations: async (language, namespace) => {
    const module = await import(`./translations/${language}/${namespace}.json`);
    return module.default;
  },
  // v1.1.0: μ—λŸ¬ 처리 κ°•ν™”
  errorHandling: {
    recoveryStrategy: {
      maxRetries: 3,
      retryDelay: 1000,
      backoffMultiplier: 2
    },
    logging: { enabled: true, level: 'error' },
    userFriendlyMessages: true
  }
});

function App() {
  return (
    <I18nProvider config={i18nConfig}>
      <MyComponent />
    </I18nProvider>
  );
}

λ²ˆμ—­ 파일 ꡬ쑰

translations/
β”œβ”€β”€ ko/
β”‚   β”œβ”€β”€ common.json
β”‚   └── auth.json
└── en/
    β”œβ”€β”€ common.json
    └── auth.json

λ²ˆμ—­ 파일 μ˜ˆμ‹œ

// translations/ko/common.json
{
  "welcome": "ν™˜μ˜ν•©λ‹ˆλ‹€",
  "greeting": "μ•ˆλ…•ν•˜μ„Έμš”, {{name}}λ‹˜!",
  "buttons": {
    "save": "μ €μž₯",
    "cancel": "μ·¨μ†Œ"
  }
}
// translations/en/common.json
{
  "welcome": "Welcome",
  "greeting": "Hello, {{name}}!",
  "buttons": {
    "save": "Save",
    "cancel": "Cancel"
  }
}

withDefaultConfig() μ˜΅μ…˜

export const I18nProvider = withDefaultConfig({
  // κΈ°λ³Έ μ–Έμ–΄ (κΈ°λ³Έκ°’: 'ko')
  defaultLanguage: 'en',
  
  // 폴백 μ–Έμ–΄ (κΈ°λ³Έκ°’: 'en')
  fallbackLanguage: 'ko',
  
  // λ„€μž„μŠ€νŽ˜μ΄μŠ€ (κΈ°λ³Έκ°’: ['common'])
  namespaces: ['common', 'auth', 'dashboard'],
  
  // 디버그 λͺ¨λ“œ (κΈ°λ³Έκ°’: NODE_ENV === 'development')
  debug: true,
  
  // μžλ™ μ–Έμ–΄ μ „ν™˜ 이벀트 처리 (κΈ°λ³Έκ°’: true)
  // 이벀트 λ¦¬μŠ€λ„ˆ μžλ™ 등둝: huaI18nLanguageChange, i18nLanguageChanged
  // λΈŒλΌμš°μ € μ–Έμ–΄ λ³€κ²½μ΄λ‚˜ μ™ΈλΆ€ μ–Έμ–΄ μ „ν™˜ 이벀트λ₯Ό μžλ™μœΌλ‘œ 감지
  autoLanguageSync: true,
});

autoLanguageSync μ˜΅μ…˜ 상세 μ„€λͺ…

autoLanguageSync μ˜΅μ…˜μ€ μ–Έμ–΄ μ „ν™˜ 이벀트λ₯Ό μžλ™μœΌλ‘œ κ°μ§€ν•˜κ³  μ²˜λ¦¬ν•©λ‹ˆλ‹€:

// μžλ™μœΌλ‘œ κ°μ§€ν•˜λŠ” μ΄λ²€νŠΈλ“€
window.addEventListener('huaI18nLanguageChange', (event) => {
  // SDK λ‚΄λΆ€ μ–Έμ–΄ λ³€κ²½ 이벀트
  const newLanguage = event.detail;
});

window.addEventListener('i18nLanguageChanged', (event) => {
  // 일반적인 μ–Έμ–΄ λ³€κ²½ 이벀트
  const newLanguage = event.detail;
});

μ‚¬μš© μ˜ˆμ‹œ:

// λ‹€λ₯Έ μ»΄ν¬λ„ŒνŠΈμ—μ„œ μ–Έμ–΄ λ³€κ²½ μ‹œ
const changeLanguage = (language) => {
  // 이벀트 λ°œμƒ β†’ withDefaultConfig()κ°€ μžλ™μœΌλ‘œ 감지
  window.dispatchEvent(new CustomEvent('i18nLanguageChanged', { 
    detail: language 
  }));
};

κ³ κΈ‰ κΈ°λŠ₯

μ–Έμ–΄ λ³€κ²½

import { useLanguageChange } from 'hua-i18n-sdk';

function LanguageSwitcher() {
  const { currentLanguage, changeLanguage, supportedLanguages } = useLanguageChange();
  
  return (
    <select 
      value={currentLanguage} 
      onChange={(e) => changeLanguage(e.target.value)}
    >
      {supportedLanguages.map(lang => (
        <option key={lang.code} value={lang.code}>
          {lang.nativeName}
        </option>
      ))}
    </select>
  );
}

μ„œλ²„ μ»΄ν¬λ„ŒνŠΈ (SSR)

import { ssrTranslate } from 'hua-i18n-sdk';

export default function ServerComponent() {
  const title = ssrTranslate({
    translations: translations.ko.common(),
    key: 'common.welcome',
    language: 'ko',
  });

  return <h1>{title}</h1>;
}

νƒ€μž… μ•ˆμ „ν•œ λ²ˆμ—­

interface MyTranslations {
  common: {
    welcome: string;
    greeting: string;
  };
  auth: {
    login: string;
    logout: string;
  };
}

const { t } = useI18n<MyTranslations>();

// μžλ™μ™„μ„± 지원
t('common.welcome'); // βœ… νƒ€μž… μ•ˆμ „
t('common.invalid'); // ❌ νƒ€μž… μ—λŸ¬

μ—λŸ¬ 처리 (v1.1.0)

μžλ™ μž¬μ‹œλ„ 및 볡ꡬ

const config = {
  // ... κΈ°λ³Έ μ„€μ •
  errorHandling: {
    recoveryStrategy: {
      maxRetries: 3,
      retryDelay: 1000,
      backoffMultiplier: 2,
      shouldRetry: (error) => ['NETWORK_ERROR', 'LOAD_FAILED'].includes(error.code),
      onRetry: (error, attempt) => console.log(`μž¬μ‹œλ„ ${attempt}회:`, error.message),
      onMaxRetriesExceeded: (error) => alert('λ²ˆμ—­ 데이터λ₯Ό 뢈러올 수 μ—†μŠ΅λ‹ˆλ‹€')
    },
    logging: {
      enabled: true,
      level: 'error',
      includeStack: true,
      includeContext: true
    },
    userFriendlyMessages: true
  }
};

μ»€μŠ€ν…€ μ—λŸ¬ 처리

import { createTranslationError, logTranslationError } from 'hua-i18n-sdk';

try {
  // λ²ˆμ—­ λ‘œλ”©
} catch (error) {
  const translationError = createTranslationError(
    'LOAD_FAILED',
    error.message,
    error,
    { language: 'ko', namespace: 'common' }
  );
  
  logTranslationError(translationError);
}

λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ (v1.0.x β†’ v1.1.0)

βœ… κΈ°μ‘΄ μ½”λ“œλŠ” λ³€κ²½ 없이 λ™μž‘ν•©λ‹ˆλ‹€

// v1.0.x μ½”λ“œ (κ·ΈλŒ€λ‘œ λ™μž‘)
const config: I18nConfig = {
  defaultLanguage: 'ko',
  fallbackLanguage: 'en',
  supportedLanguages: [
    { code: 'ko', name: 'Korean', nativeName: 'ν•œκ΅­μ–΄' },
    { code: 'en', name: 'English', nativeName: 'English' },
  ],
  namespaces: ['common'],
  loadTranslations: async (language, namespace) => {
    const module = await import(`./translations/${language}/${namespace}.json`);
    return module.default;
  },
};

// v1.1.0μ—μ„œλ„ λ™μΌν•˜κ²Œ λ™μž‘

μƒˆλ‘œμš΄ κΈ°λŠ₯ ν™œμš© (선택사항)

// v1.1.0: μ—λŸ¬ 처리 κ°•ν™” (선택적)
const config: I18nConfig = {
  // ... κΈ°μ‘΄ μ„€μ •
  errorHandling: {
    recoveryStrategy: {
      maxRetries: 3,
      retryDelay: 1000,
      backoffMultiplier: 2
    },
    logging: { enabled: true, level: 'error' },
    userFriendlyMessages: true
  }
};

λ¬Έμ„œ

ν…ŒμŠ€νŠΈ

npm test
npm run test:watch
npm run test:coverage

λΉŒλ“œ

npm run build

κΈ°μ—¬ν•˜κΈ°

ν”„λ‘œμ νŠΈμ— κΈ°μ—¬ν•˜κ³  μ‹ΆμœΌμ‹œλ‹€λ©΄ κΈ°μ—¬ κ°€μ΄λ“œλ₯Ό μ°Έκ³ ν•΄μ£Όμ„Έμš”.

개발 ν™˜κ²½ μ„€μ •

git clone https://github.com/HUA-Labs/i18n-sdk.git
cd hua-i18n-sdk
npm install
npm run dev

λΌμ΄μ„ μŠ€

이 ν”„λ‘œμ νŠΈλŠ” MIT λΌμ΄μ„ μŠ€ ν•˜μ— λ°°ν¬λ©λ‹ˆλ‹€.

κ°μ‚¬μ˜ 말

  • React - λ©‹μ§„ UI 라이브러리
  • TypeScript - νƒ€μž… μ•ˆμ „μ„±
  • Next.js - SSR 지원
  • λͺ¨λ“  κΈ°μ—¬μžλΆ„λ“€κ»˜ κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€!

Made with ❀️ by the hua-i18n-sdk team

About

Simple and powerful internationalization SDK for React applications. Features hua-api style translation system with fast performance and developer-friendly API. Includes TypeScript support, multi-language support, and debug tools.

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

No packages published