frontend-opensource-project / use-react-hooks

React hoooooooks๐Ÿ˜Ž
5 stars 0 forks source link

[URH-59] useTranslation ์‹ ๊ทœ #57

Closed Choozii closed 2 weeks ago

Choozii commented 2 weeks ago

๐Ÿ‘พ Pull Request

1๏ธโƒฃ Spec

3๏ธโƒฃ ์˜ˆ์‹œ ์ฝ”๋“œ

import ReactDOM from 'react-dom/client';
import App from './App.tsx';
import { TranslationProvider } from './context/TranslationContext';

const translations = {
  en: {
    greeting: 'Hello ${name}, You have ${count} new messages.',
    farewell: 'Goodbye ${name}',
  },
  ko: {
    greeting: '์•ˆ๋…•ํ•˜์„ธ์š” ${name}, ์ƒˆ ๋ฉ”์‹œ์ง€๊ฐ€ ${count}๊ฐœ ์žˆ์Šต๋‹ˆ๋‹ค.',
    farewell: '์•ˆ๋…•ํžˆ ๊ฐ€์„ธ์š” ${name}',
  },
};

ReactDOM.createRoot(document.getElementById('root')!).render(
  <TranslationProvider translations={translations} defaultLanguage="en">
    <App />{' '}
  </TranslationProvider>
);
import { useTranslation } from './hooks/useTranslation';

const App = () => {
  const { t, language, changeLanguage } = useTranslation();

  const handleLanguageChange = (lang: string) => {
    changeLanguage(lang);
  };

  return (
    <div>
      <h1>{t('greeting', { name: 'Choo', count: 5 })}</h1>
      <p>{t('farewell', { name: 'Choo' })}</p>

      <div>
        <button onClick={() => handleLanguageChange('en')}>English</button>
        <button onClick={() => handleLanguageChange('ko')}>ํ•œ๊ตญ์–ด</button>
      </div>

      <p>Current Language: {language}</p>
    </div>
  );
};

export default App;

4๏ธโƒฃ ๊ด€๋ จ ๋ฌธ์„œ (์„ ํƒ ์‚ฌํ•ญ)