1- import translations from '../data/breadcrumb.json' ;
2-
31/**
42 * Breadcrumb 아이템의 세그먼트 데이터 구조
53 * - name: 세그먼트 이름
@@ -27,18 +25,36 @@ interface TranslationData {
2725 [ key : string ] : SegmentData ;
2826}
2927
28+ /**
29+ * Breadcrumb 번역 데이터를 로드합니다.
30+ */
31+ async function loadTranslationData ( ) : Promise < TranslationData > {
32+ try {
33+ const response = await fetch ( '/data/breadcrumb.json' ) ;
34+ if ( ! response . ok ) {
35+ throw new Error ( 'Failed to load breadcrumb translation data' ) ;
36+ }
37+ return await response . json ( ) ;
38+ } catch ( error ) {
39+ console . error ( 'Error loading breadcrumb translation data:' , error ) ;
40+ // 기본값 반환 (빈 객체)
41+ return { } ;
42+ }
43+ }
44+
3045/**
3146 * 경로 세그먼트를 번역하고 링크 가능 여부를 확인합니다.
3247 * 계층적 구조를 고려하여 현재 경로에서 세그먼트를 찾습니다.
3348 * @param segments 전체 경로 세그먼트 배열
3449 * @param currentIndex 현재 처리 중인 세그먼트의 인덱스(깊이를 알기 위함)
50+ * @param translationData 번역 데이터
3551 * @returns 해당 세그먼트 데이터 또는 null (데이터가 없는 경우)
3652 */
3753function getSegmentData (
3854 segments : string [ ] ,
39- currentIndex : number
55+ currentIndex : number ,
56+ translationData : TranslationData
4057) : SegmentData | null {
41- const translationData = translations as TranslationData ;
4258 let current = translationData [ segments [ 0 ] ] ;
4359
4460 if ( ! current ) {
@@ -59,21 +75,22 @@ function getSegmentData(
5975/**
6076 * 현재 hash를 기반으로 breadcrumb 아이템들을 생성합니다.
6177 */
62- function generateBreadcrumbItems ( ) : BreadcrumbItem [ ] {
78+ async function generateBreadcrumbItems ( ) : Promise < BreadcrumbItem [ ] > {
6379 const hash = window . location . hash . slice ( 1 ) ; // # 제거
6480
6581 if ( ! hash || hash === '/' ) {
6682 return [ { name : '홈' , path : '/' , linkable : true } ] ;
6783 }
6884
85+ const translationData = await loadTranslationData ( ) ;
6986 const pathSegments = hash . split ( '/' ) . filter ( ( segment ) => segment !== '' ) ;
7087 const breadcrumbItems : BreadcrumbItem [ ] = [
7188 { name : '홈' , path : '/' , linkable : true } ,
7289 ] ;
7390
7491 let currentPath = '' ;
7592 pathSegments . forEach ( ( segment , index ) => {
76- const segmentData = getSegmentData ( pathSegments , index ) ;
93+ const segmentData = getSegmentData ( pathSegments , index , translationData ) ;
7794 currentPath += `/${ segment } ` ;
7895
7996 // translations 데이터에 없는 세그먼트는 breadcrumb에 추가하지 않음
@@ -131,12 +148,12 @@ function removePreviousBreadcrumb(): void {
131148/**
132149 * Breadcrumb을 생성하고 div#content의 첫 번째 자식으로 추가합니다.
133150 */
134- export function initializeBreadcrumb ( ) : void {
151+ export async function initializeBreadcrumb ( ) : Promise < void > {
135152 const contentDiv = document . getElementById ( 'content' ) ! ;
136153
137154 removePreviousBreadcrumb ( ) ;
138155
139- const breadcrumbItems = generateBreadcrumbItems ( ) ;
156+ const breadcrumbItems = await generateBreadcrumbItems ( ) ;
140157 const breadcrumbElement = createBreadcrumbElement ( breadcrumbItems ) ;
141158
142159 contentDiv . insertBefore ( breadcrumbElement , contentDiv . firstChild ) ;
0 commit comments