refactor: reorganize project structure and improve type definitions

- Split types into separate modules for better organization
- Move data files to dedicated directories with proper documentation
- Enhance i18n utilities with better type safety and performance
- Maintain backward compatibility with legacy imports
This commit is contained in:
joyzhao
2025-06-21 09:18:39 +08:00
parent e38ec6b12f
commit ea01dc6dd8
14 changed files with 784 additions and 523 deletions

143
src/i18n/translations.ts Normal file
View File

@@ -0,0 +1,143 @@
/**
* Translations module
* Contains all translation data for the application
*/
/**
* Available languages in the application
*/
export const languages = {
en: 'English',
zh: '简体中文',
} as const;
/**
* Default language for the application
*/
export const defaultLang = 'en';
/**
* Structured translations data
* Organized by language and feature area
*/
export const translations = {
en: {
nav: {
home: 'Home',
about: 'About',
services: 'Services',
projects: 'Projects',
blog: 'Blog',
contact: 'Contact',
},
site: {
title: 'Joy Zhao - Full Stack Developer',
description: 'Full Stack Developer specializing in React, Node.js, and modern web technologies',
},
hero: {
githubLink: 'GitHub Profile',
linkedinLink: 'LinkedIn Profile',
},
footer: {
rights: 'All rights reserved',
},
project: {
tag: {
business: 'Business Project',
opensource: 'Open Source',
personal: 'Personal Product',
portfolio: 'Portfolio',
ecommerce: 'E-Commerce',
},
visit: 'Visit',
demo: 'Live Demo',
},
projects: {
title: 'My Projects',
description: 'A collection of my recent work, showcasing innovative solutions and clean code. Explore the details of each project below.',
slogan: 'Crafting elegant solutions to complex problems with clean code and innovative thinking.',
},
blog: {
slogan: 'This is where innovative thinking meets complex problems.',
},
},
zh: {
nav: {
home: '首页',
about: '关于',
services: '服务',
projects: '项目',
blog: '博客',
contact: '联系',
},
site: {
title: 'Joy Zhao - 全栈开发者',
description: '专注于 React、Node.js 和现代 Web 技术的全栈开发者',
},
hero: {
githubLink: 'GitHub 主页',
linkedinLink: 'LinkedIn 主页',
},
footer: {
rights: '版权所有',
},
project: {
tag: {
business: '商业项目',
opensource: '开源项目',
personal: '个人产品',
portfolio: '作品集',
ecommerce: '电子商务',
},
visit: '访问',
demo: '在线演示',
},
projects: {
title: '我的项目',
description: '这里展示了我最近的作品集,展现了创新解决方案和整洁的代码。请浏览下方了解每个项目的详细信息。',
slogan: '用优雅的代码和创新的思维,为复杂问题打造精致的解决方案。',
},
blog: {
slogan: '这里是创新思维与复杂问题相遇的地方。',
},
},
} as const;
/**
* Generate flattened translations for better performance
* This creates a flat object with dot notation keys
*/
export function generateFlatTranslations() {
const flattenedTranslations: Record<string, Record<string, string>> = {};
// Initialize for each language
Object.keys(translations).forEach(lang => {
flattenedTranslations[lang] = {};
});
// Recursive function to flatten nested objects
function flatten(obj: any, lang: string, prefix = '') {
for (const key in obj) {
const value = obj[key];
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === 'object' && value !== null) {
flatten(value, lang, newKey);
} else {
flattenedTranslations[lang][newKey] = value;
}
}
}
// Process each language
Object.keys(translations).forEach(lang => {
flatten(translations[lang as keyof typeof translations], lang);
});
return flattenedTranslations;
}
/**
* Flattened translations for faster lookups
*/
export const flatTranslations = generateFlatTranslations();