Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
afad13e
chore: 국제화 라이브러리 설치
useon Sep 16, 2025
c8cfc46
feat: i18next 초기화 및 언어 감지/HTTP 백엔드 설정
useon Sep 16, 2025
9accb6f
feat: 국제화 라우팅 래퍼 생성
useon Sep 16, 2025
f300703
feat: 중첩 라우팅 구조로 변경 및 국제화 라우팅 래퍼 적용
useon Sep 16, 2025
595e717
chore: 필요한 바벨 설정 및 스크립트 추가
useon Oct 7, 2025
7882759
feat: 파일 관련 유틸 생성
useon Oct 7, 2025
8a89637
feat: ATS 관련 및 국제화 훅 추가, t wrapper 유틸 생성
useon Oct 7, 2025
b859bc9
feat: 한글 키를 기준으로 각 언어 JSON에 키 추가 유틸 생성
useon Oct 7, 2025
4bc7525
feat: React 컴포넌트의 한글 텍스트 자동 변환 기능 구현
useon Oct 7, 2025
5f02869
fix: 테스트 파일과 스토리북 파일이 포함되는 문제 해결
useon Oct 7, 2025
c0eb304
refactor: 컴포넌트 판별 로직을 분리하여 여러 형태에 대응할 수 있도록 수정
useon Oct 8, 2025
87e9a1c
feat: 템플릿 리터럴 자동 변환 및 키 추출 기능 구현
useon Nov 19, 2025
52e7622
refactor: 컴포넌트 내부에 있는 한글 문자열만 변환되도록 수정
useon Nov 21, 2025
21ee43c
refactor: 복잡한 확장자 처리를 위한 라이브러리 사용으로 더이상 사용하지 않는 확장자 찾는 함수 삭제
useon Nov 21, 2025
8ff5e40
fix: ts-node 실행 오류 해결 및 tsx 기반으로 스크립트 전환
useon Dec 1, 2025
f4ee51c
Merge branch 'develop' of https://github.com/debate-timer/debate-time…
useon Dec 8, 2025
9914402
refactor: 불필요한 주석 삭제
useon Dec 8, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,110 changes: 995 additions & 115 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,33 @@
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"test": "vitest",
"chromatic": "npx chromatic --project-token=chpt_a6dc39eba6488b2"
"chromatic": "npx chromatic --project-token=chpt_a6dc39eba6488b2",
"i18n:transform": "tsx scripts/i18nTransform.ts"
},
"dependencies": {
"@tanstack/eslint-plugin-query": "^5.62.9",
"@tanstack/react-query": "^5.62.12",
"axios": "^1.7.9",
"clsx": "^2.1.1",
"framer-motion": "^12.23.11",
"i18next": "^25.5.2",
"i18next-browser-languagedetector": "^8.2.0",
"i18next-http-backend": "^3.0.2",
"pako": "^2.1.0",
"qrcode.react": "^4.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-ga4": "^2.1.0",
"react-i18next": "^15.7.3",
"react-icons": "^5.4.0",
"react-router-dom": "^7.1.0",
"vite-bundle-visualizer": "^1.2.1"
},
"devDependencies": {
"@babel/generator": "^7.28.3",
"@babel/parser": "^7.28.4",
"@babel/traverse": "^7.28.4",
"@babel/types": "^7.28.4",
"@chromatic-com/storybook": "^3.2.2",
"@eslint/js": "^9.15.0",
"@storybook/addon-essentials": "^8.6.0",
Expand All @@ -46,7 +55,10 @@
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.1.0",
"@testing-library/user-event": "^14.5.2",
"@types/babel__traverse": "^7.28.0",
"@types/glob": "^8.1.0",
"@types/jest": "^29.5.14",
"@types/node": "^24.6.0",
"@types/pako": "^2.0.3",
"@types/react": "^18.3.12",
"@types/react-dom": "^18.3.1",
Expand All @@ -64,6 +76,7 @@
"eslint-plugin-react-refresh": "^0.4.14",
"eslint-plugin-storybook": "^0.11.1",
"eslint-plugin-tailwindcss": "^3.17.5",
"glob": "^11.0.3",
"globals": "^15.12.0",
"jsdom": "^25.0.1",
"msw": "^2.7.0",
Expand All @@ -76,6 +89,7 @@
"stylelint-config-recommended": "^14.0.1",
"stylelint-config-tailwindcss": "^0.0.7",
"tailwindcss": "^3.4.16",
"tsx": "^4.21.0",
"typescript": "^5.7.2",
"typescript-eslint": "^8.15.0",
"vite": "^6.0.1",
Expand Down
44 changes: 44 additions & 0 deletions scripts/i18nTransform.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as fs from 'fs/promises';
import { glob } from 'glob';
import { parseCode, transformAST, generateCode } from './utils/astUtils.ts';
import { updateTranslationFiles } from './utils/translationUtils.ts';

async function processFile(filePath: string) {
console.log(`\n파일 처리 중: ${filePath}`);
const originalCode = await fs.readFile(filePath, 'utf-8');
const ast = parseCode(originalCode);

const koreanKeys = transformAST(ast);
if (koreanKeys.size === 0) {
console.log('한글 텍스트를 찾지 못했습니다.');
return;
}

await updateTranslationFiles(koreanKeys);

const newCode = generateCode(ast);
if (newCode !== originalCode) {
await fs.writeFile(filePath, newCode, 'utf-8');
console.log(`파일 업데이트 완료: ${filePath}`);
} else {
console.log('변경 사항이 없습니다.');
}
}

async function main() {
const files = await glob('src/**/*.tsx', {
ignore: ['src/**/*.test.tsx', 'src/**/*.stories.tsx'],
});
if (files.length === 0) {
console.log('.tsx 파일을 찾지 못했습니다.');
return;
}

for (const file of files) {
await processFile(file);
}

console.log('\ni18n 변환 작업이 완료되었습니다.');
}

main().catch(console.error);
280 changes: 280 additions & 0 deletions scripts/utils/astUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,280 @@
import * as parser from '@babel/parser';
import type { NodePath } from '@babel/traverse';
import _traverse from '@babel/traverse';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const traverse = (_traverse as any).default;
import _generate from '@babel/generator';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const generate = (_generate as any).default;
import * as t from '@babel/types';

const KOREAN_REGEX = /[가-힣]/;

/**
* 코드 문자열을 파싱하여 AST로 변환
*/
export function parseCode(code: string) {
return parser.parse(code, {
sourceType: 'module',
plugins: ['jsx', 'typescript'],
});
}

/**
* 리액트 컴포넌트 함수인지 판별
*/
function isReactComponentFunction(path: NodePath): boolean {
// 함수 선언문
if (path.isFunctionDeclaration()) {
return path.node.id?.name?.[0] === path.node.id?.name?.[0]?.toUpperCase();
}
Comment on lines +26 to +30
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

함수 선언문 컴포넌트 판별 로직 오류를 수정하세요.

Line 29의 조건문 path.node.id?.name?.[0] === path.node.id?.name?.[0]?.toUpperCase()는 첫 글자를 자기 자신과 비교하여 항상 true를 반환합니다. 이는 React 컴포넌트가 아닌 일반 함수도 컴포넌트로 잘못 인식할 수 있습니다.

다음과 같이 수정하세요:

   // 함수 선언문
   if (path.isFunctionDeclaration()) {
-    return path.node.id?.name?.[0] === path.node.id?.name?.[0]?.toUpperCase();
+    return /^[A-Z]/.test(path.node.id?.name || '');
   }

또는:

-    return path.node.id?.name?.[0] === path.node.id?.name?.[0]?.toUpperCase();
+    const name = path.node.id?.name;
+    return name ? name[0] === name[0].toUpperCase() : false;
🤖 Prompt for AI Agents
In scripts/utils/astUtils.ts around lines 26 to 30, the function-declaration
check compares the first character to itself which is always true; replace that
condition by first extracting the identifier name (e.g., const name =
path.node.id?.name), ensure it exists, then compare its first character to its
uppercase form (e.g., const first = name[0]; return first ===
first.toUpperCase() && first !== first.toLowerCase()) so only identifiers
starting with an uppercase letter (actual alphabetic characters) are treated as
React components.


// 화살표 함수 표현식 또는 함수 표현식
if (path.isArrowFunctionExpression() || path.isFunctionExpression()) {
const parent = path.parentPath;

// 변수 선언문
if (parent?.isVariableDeclarator()) {
const varName = (parent.node.id as t.Identifier)?.name;
return /^[A-Z]/.test(varName);
}

// 합성 컴포넌트
if (parent?.isAssignmentExpression()) {
const left = parent.get('left');
if (left.isMemberExpression()) {
const property = left.get('property');
if (property.isIdentifier()) {
return /^[A-Z]/.test(property.node.name);
}
}
}
}

return false;
}

/**
* AST에서 한글 문자열 탐색 및 변환
*/
export function transformAST(ast: t.File) {
const koreanKeys = new Set<string>();
const componentsToModify = new Set<NodePath>();
let hasUseTranslationImport = false;
const simpleStringsToTransform: NodePath<t.StringLiteral | t.JSXText>[] = [];
const templateLiteralsToTransform: {
path: NodePath<t.TemplateLiteral>;
i18nKey: string;
objectProperties: t.ObjectProperty[];
}[] = [];

// 1️. 한글 문자열 탐색 및 변환 대상 수집
traverse(ast, {
JSXText(path) {
const value = path.node.value.trim();
if (value && KOREAN_REGEX.test(value)) {
const component = path.findParent((p) => isReactComponentFunction(p));
if (component) {
const parentT = path.findParent(
(p) =>
p.isCallExpression() &&
p.get('callee').isIdentifier({ name: 't' }),
);
if (parentT) return;

simpleStringsToTransform.push(path);
koreanKeys.add(value);
componentsToModify.add(component);
}
}
},
StringLiteral(path) {
const value = path.node.value.trim();
if (
value &&
KOREAN_REGEX.test(value) &&
path.parent.type !== 'ImportDeclaration' &&
path.parent.type !== 'ExportNamedDeclaration' &&
!(
path.parent.type === 'ObjectProperty' && path.parent.key === path.node
)
) {
const component = path.findParent((p) => isReactComponentFunction(p));
if (component) {
const parentT = path.findParent(
(p) =>
p.isCallExpression() &&
p.get('callee').isIdentifier({ name: 't' }),
);
if (parentT) return;

simpleStringsToTransform.push(path);
koreanKeys.add(value);
componentsToModify.add(component);
}
}
},
TemplateLiteral(path) {
const { quasis, expressions } = path.node;
const hasKorean = quasis.some((q) => KOREAN_REGEX.test(q.value.raw));
if (!hasKorean) return;

if (
path.parent.type === 'CallExpression' &&
t.isIdentifier(path.parent.callee) &&
path.parent.callee.name === 't'
) {
return;
}

const component = path.findParent((p) => isReactComponentFunction(p));
if (!component) return;

let i18nKey = '';
const objectProperties: t.ObjectProperty[] = [];

for (let i = 0; i < quasis.length; i++) {
i18nKey += quasis[i].value.raw;
if (i < expressions.length) {
const expr = expressions[i];
let placeholderName: string;

if (t.isIdentifier(expr)) {
placeholderName = expr.name;
} else if (
t.isMemberExpression(expr) &&
t.isIdentifier(expr.property)
) {
placeholderName = expr.property.name;
} else {
placeholderName = `val${i}`;
}

let finalName = placeholderName;
let count = 1;
while (
objectProperties.some(
(p) => t.isIdentifier(p.key) && p.key.name === finalName,
)
) {
finalName = `${placeholderName}${count++}`;
}

i18nKey += `{{${finalName}}}`;
objectProperties.push(
t.objectProperty(
t.identifier(finalName),
expr,
false,
t.isIdentifier(expr) && finalName === expr.name,
),
);
}
}

koreanKeys.add(i18nKey);
componentsToModify.add(component);
templateLiteralsToTransform.push({ path, i18nKey, objectProperties });
},
ImportDeclaration(path) {
if (path.node.source.value === 'react-i18next') {
hasUseTranslationImport = true;
}
},
});

// 2️. useTranslation import 추가
if (koreanKeys.size > 0 && !hasUseTranslationImport) {
const importDecl = t.importDeclaration(
[
t.importSpecifier(
t.identifier('useTranslation'),
t.identifier('useTranslation'),
),
],
t.stringLiteral('react-i18next'),
);
ast.program.body.unshift(importDecl);
}

// 3️. 각 컴포넌트에 const { t } = useTranslation() 추가
componentsToModify.forEach((componentPath) => {
const bodyPath = componentPath.get('body');
if (Array.isArray(bodyPath) || !bodyPath.isBlockStatement()) return;

let hasHook = false;
bodyPath.get('body').forEach((stmt) => {
if (stmt.isVariableDeclaration()) {
const declaration = stmt.node.declarations[0];
if (
declaration?.init?.type === 'CallExpression' &&
t.isIdentifier(declaration.init.callee) &&
declaration.init.callee.name === 'useTranslation'
) {
hasHook = true;
}
}
});

if (!hasHook) {
const hookDecl = t.variableDeclaration('const', [
t.variableDeclarator(
t.objectPattern([
t.objectProperty(t.identifier('t'), t.identifier('t'), false, true),
]),
t.callExpression(t.identifier('useTranslation'), []),
),
]);
bodyPath.unshiftContainer('body', hookDecl);
}
});

// 4️. 템플릿 리터럴 변환
templateLiteralsToTransform.forEach(({ path, i18nKey, objectProperties }) => {
const keyLiteral = t.stringLiteral(i18nKey);
if (objectProperties.length > 0) {
const interpolationObject = t.objectExpression(objectProperties);
const tCall = t.callExpression(t.identifier('t'), [
keyLiteral,
interpolationObject,
]);
path.replaceWith(tCall);
} else {
const tCall = t.callExpression(t.identifier('t'), [keyLiteral]);
path.replaceWith(tCall);
}
});

// 5️. 컴포넌트 내부 한글 텍스트 t()로 감싸기
simpleStringsToTransform.forEach((path) => {
const value =
path.node.type === 'JSXText'
? path.node.value.trim()
: (path.node as t.StringLiteral).value;

const tCall = t.callExpression(t.identifier('t'), [t.stringLiteral(value)]);

if (path.isJSXText()) {
path.replaceWith(t.jsxExpressionContainer(tCall));
} else if (path.isStringLiteral()) {
if (path.parent.type === 'JSXAttribute') {
path.replaceWith(t.jsxExpressionContainer(tCall));
} else {
path.replaceWith(tCall);
}
}
});

return koreanKeys;
}

/**
* AST를 코드 문자열로 다시 변환
*/
export function generateCode(ast: t.File) {
const { code } = generate(ast, {
retainLines: true,
jsescOption: { minimal: true },
});
return code;
}
Loading