콘텐츠로 이동

딕셔너리 사용하기

딕셔너리는 전통적인 키-값 번역 파일입니다. 다음과 같은 경우에 유용합니다:

  • 여러 컴포넌트에서 공통으로 사용하는 문자열(버튼 라벨, 오류 메시지)
  • 코드베이스 외부에서 관리되는 콘텐츠
  • 기존 i18n 설정에서의 마이그레이션
{
"dictionaries": {
"include": ["src/dictionaries/*.json"],
"format": "key-value"
}
}
src/dictionaries/common.json
{
"save": "Save",
"cancel": "Cancel",
"delete": "Delete",
"loading": "Loading..."
}

useDictionary(filenameKey)는 하나의 딕셔너리 파일을 조회하고 Record<string, string>을 반환합니다. 파일명 키는 .json 확장자를 제외한 딕셔너리 경로입니다:

  • src/dictionaries/common.jsoncommon
  • src/dictionaries/pages/home.jsonpages/home
import { useDictionary } from 'tyndale-react';
export function ActionButtons() {
const labels = useDictionary('common');
return (
<div>
<button>{labels.save ?? 'Save'}</button>
<button>{labels.cancel ?? 'Cancel'}</button>
</div>
);
}

파일명 키와 일치하는 항목이 없으면 useDictionary(filenameKey)는 빈 객체를 반환합니다.