-
Notifications
You must be signed in to change notification settings - Fork 2
[FEAT] i18n 자동화 스크립트 도입 #402
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
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
afad13e
chore: 국제화 라이브러리 설치
useon c8cfc46
feat: i18next 초기화 및 언어 감지/HTTP 백엔드 설정
useon 9accb6f
feat: 국제화 라우팅 래퍼 생성
useon f300703
feat: 중첩 라우팅 구조로 변경 및 국제화 라우팅 래퍼 적용
useon 595e717
chore: 필요한 바벨 설정 및 스크립트 추가
useon 7882759
feat: 파일 관련 유틸 생성
useon 8a89637
feat: ATS 관련 및 국제화 훅 추가, t wrapper 유틸 생성
useon b859bc9
feat: 한글 키를 기준으로 각 언어 JSON에 키 추가 유틸 생성
useon 4bc7525
feat: React 컴포넌트의 한글 텍스트 자동 변환 기능 구현
useon 5f02869
fix: 테스트 파일과 스토리북 파일이 포함되는 문제 해결
useon c0eb304
refactor: 컴포넌트 판별 로직을 분리하여 여러 형태에 대응할 수 있도록 수정
useon 87e9a1c
feat: 템플릿 리터럴 자동 변환 및 키 추출 기능 구현
useon 52e7622
refactor: 컴포넌트 내부에 있는 한글 문자열만 변환되도록 수정
useon 21ee43c
refactor: 복잡한 확장자 처리를 위한 라이브러리 사용으로 더이상 사용하지 않는 확장자 찾는 함수 삭제
useon 8ff5e40
fix: ts-node 실행 오류 해결 및 tsx 기반으로 스크립트 전환
useon f4ee51c
Merge branch 'develop' of https://github.com/debate-timer/debate-time…
useon 9914402
refactor: 불필요한 주석 삭제
useon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
|
|
||
| // 화살표 함수 표현식 또는 함수 표현식 | ||
| 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; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
함수 선언문 컴포넌트 판별 로직 오류를 수정하세요.
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 || ''); }또는:
🤖 Prompt for AI Agents