Skip to content

[URH-59] useTranslation 신규 #57

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

Merged
merged 1 commit into from
Sep 1, 2024
Merged

[URH-59] useTranslation 신규 #57

merged 1 commit into from
Sep 1, 2024

Conversation

Choozii
Copy link
Member

@Choozii Choozii commented Aug 30, 2024

👾 Pull Request

  • 상태: 신규

1️⃣ Spec

  • 기능 : 사용자가 설정한 언어에 따라 텍스트를 변경해줌
    • changeLanguage 함수를 사용해 언어 변경 가능
    • useTranslation에서 리턴 받은 t 함수를 통해 언어 리소스에서 번역된 텍스트를 가져올 수 있음

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>
);
  • useTranslation을 사용하는 곳 상위 컴포넌트에서 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️⃣ 관련 문서 (선택 사항)

@Choozii Choozii self-assigned this Aug 30, 2024
@Choozii Choozii requested review from bicochan and suhyeoonn August 30, 2024 05:37
Copy link
Member

@bicochan bicochan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수고하셨어요 지예 님👍🏻✨ 리뷰 확인 부탁드려요!

return key in currentTranslations
? currentTranslations[key].replace(
DYNAMIC_VALUE_PATTERN,
(_, dynamic_value) => String(variables[dynamic_value] || '')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(_, dynamic_value) => String(variables[dynamic_value] || '')
(_, dynamicValue) => String(variables[dynamicValue] || '')

[P2] 혹시 스네이크 케이스를 사용하신 이유가 있으실까요?!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

없습니다! 아마 혼동한거 같아요ㅋㅋㅋㅋ감사합니다!

e15891c

Copy link
Contributor

@suhyeoonn suhyeoonn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

코드가 깔끔하게 작성되어 있어 분석하기가 수월했습니다! 👍
수고 많으셨습니다~!

@Choozii Choozii requested a review from bicochan September 1, 2024 12:28
✨ feat: translationContext jsDoc 수정

🩹 chore: case 수정
@Choozii Choozii force-pushed the URH-59/use-translation branch from e15891c to 507012b Compare September 1, 2024 12:52
@Choozii Choozii merged commit 255d127 into master Sep 1, 2024
@Choozii Choozii deleted the URH-59/use-translation branch September 1, 2024 12:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants