-
-
Notifications
You must be signed in to change notification settings - Fork 428
fix(web): 完善 Files 页面 i18n #607
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
+307
−38
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,63 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| formatDiffError, | ||
| formatDirectoryError, | ||
| formatFileSearchError, | ||
| formatGitStatusError, | ||
| formatReadFileError, | ||
| getDetachedBranchLabel, | ||
| getProjectRootLabel, | ||
| } from './files-i18n' | ||
|
|
||
| const translations: Record<string, string> = { | ||
| 'files.projectRoot': '项目根目录', | ||
| 'files.branch.detached': '游离 HEAD', | ||
| 'files.search.error.failed': '搜索文件失败', | ||
| 'files.search.error.failedWithDetail': '搜索文件失败:{error}', | ||
| 'files.directories.error.listFailed': '加载目录失败', | ||
| 'files.directories.error.listFailedWithDetail': '加载目录失败:{error}', | ||
| 'files.changes.error.gitStatusUnavailable': 'Git 状态不可用', | ||
| 'files.changes.error.gitStatusUnavailableWithDetail': 'Git 状态不可用:{error}', | ||
| 'files.changes.error.unstagedDiffUnavailableWithDetail': '未暂存 Diff 不可用:{error}', | ||
| 'files.changes.error.stagedDiffUnavailableWithDetail': '已暂存 Diff 不可用:{error}', | ||
| 'file.error.readFailed': '读取文件失败', | ||
| 'file.error.readFailedWithDetail': '读取文件失败:{error}', | ||
| 'file.error.diffUnavailable': 'Diff 不可用', | ||
| 'file.error.diffUnavailableWithDetail': 'Diff 不可用:{error}', | ||
| } | ||
|
|
||
| function t(key: string, params?: Record<string, string | number>): string { | ||
| const template = translations[key] ?? key | ||
| if (!params) return template | ||
| return template.replace(/\{(\w+)\}/g, (_, name: string) => String(params[name] ?? `{${name}}`)) | ||
| } | ||
|
|
||
| describe('files i18n helpers', () => { | ||
| it('localizes common file tree labels', () => { | ||
| expect(getProjectRootLabel('', t)).toBe('项目根目录') | ||
| expect(getProjectRootLabel('src/routes', t)).toBe('src/routes') | ||
| expect(getDetachedBranchLabel('detached', t)).toBe('游离 HEAD') | ||
| expect(getDetachedBranchLabel('main', t)).toBe('main') | ||
| }) | ||
|
|
||
| it('formats git status errors with translated summaries', () => { | ||
| expect(formatGitStatusError('Git status unavailable', t)).toBe('Git 状态不可用') | ||
| expect(formatGitStatusError('Unstaged diff unavailable: fatal', t)).toBe('未暂存 Diff 不可用:fatal') | ||
| expect(formatGitStatusError('Staged diff unavailable: timeout', t)).toBe('已暂存 Diff 不可用:timeout') | ||
| expect(formatGitStatusError('fatal: not a git repository', t)).toBe('Git 状态不可用:fatal: not a git repository') | ||
| }) | ||
|
|
||
| it('formats search and directory errors', () => { | ||
| expect(formatFileSearchError('Failed to search files', t)).toBe('搜索文件失败') | ||
| expect(formatFileSearchError('ripgrep exited 2', t)).toBe('搜索文件失败:ripgrep exited 2') | ||
| expect(formatDirectoryError('Failed to list directory', t)).toBe('加载目录失败') | ||
| expect(formatDirectoryError('permission denied', t)).toBe('加载目录失败:permission denied') | ||
| }) | ||
|
|
||
| it('formats file read and diff errors', () => { | ||
| expect(formatReadFileError('Failed to read file', t)).toBe('读取文件失败') | ||
| expect(formatReadFileError('EACCES', t)).toBe('读取文件失败:EACCES') | ||
| expect(formatDiffError('Failed to load diff', t)).toBe('Diff 不可用') | ||
| expect(formatDiffError('fatal: bad revision', t)).toBe('Diff 不可用:fatal: bad revision') | ||
| }) | ||
| }) |
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,75 @@ | ||
| type Translate = (key: string, params?: Record<string, string | number>) => string | ||
|
|
||
| function normalizeDetail(detail: string | null | undefined): string | null { | ||
| if (typeof detail !== 'string') return null | ||
| const trimmed = detail.trim() | ||
| return trimmed.length > 0 ? trimmed : null | ||
| } | ||
|
|
||
| function stripPrefix(value: string, prefix: string): string | null { | ||
| if (!value.startsWith(prefix)) return null | ||
| return normalizeDetail(value.slice(prefix.length)) | ||
| } | ||
|
|
||
| export function getProjectRootLabel(path: string | null | undefined, t: Translate): string { | ||
| return normalizeDetail(path) ?? t('files.projectRoot') | ||
| } | ||
|
|
||
| export function getDetachedBranchLabel(branch: string | null | undefined, t: Translate): string { | ||
| const value = normalizeDetail(branch) | ||
| if (!value || value === 'detached') { | ||
| return t('files.branch.detached') | ||
| } | ||
| return value | ||
| } | ||
|
|
||
| export function formatFileSearchError(error: string | null | undefined, t: Translate): string { | ||
| const detail = normalizeDetail(error) | ||
| if (!detail || detail === 'Failed to search files' || detail === 'Session unavailable') { | ||
| return t('files.search.error.failed') | ||
| } | ||
| return t('files.search.error.failedWithDetail', { error: detail }) | ||
| } | ||
|
|
||
| export function formatDirectoryError(error: string | null | undefined, t: Translate): string { | ||
| const detail = normalizeDetail(error) | ||
| if (!detail || detail === 'Failed to list directory' || detail === 'Session unavailable') { | ||
| return t('files.directories.error.listFailed') | ||
| } | ||
| return t('files.directories.error.listFailedWithDetail', { error: detail }) | ||
| } | ||
|
|
||
| export function formatGitStatusError(error: string | null | undefined, t: Translate): string { | ||
| const detail = normalizeDetail(error) | ||
| if (!detail || detail === 'Git status unavailable' || detail === 'Session unavailable') { | ||
| return t('files.changes.error.gitStatusUnavailable') | ||
| } | ||
|
|
||
| const unstagedDetail = stripPrefix(detail, 'Unstaged diff unavailable: ') | ||
| if (unstagedDetail) { | ||
| return t('files.changes.error.unstagedDiffUnavailableWithDetail', { error: unstagedDetail }) | ||
| } | ||
|
|
||
| const stagedDetail = stripPrefix(detail, 'Staged diff unavailable: ') | ||
| if (stagedDetail) { | ||
| return t('files.changes.error.stagedDiffUnavailableWithDetail', { error: stagedDetail }) | ||
| } | ||
|
|
||
| return t('files.changes.error.gitStatusUnavailableWithDetail', { error: detail }) | ||
| } | ||
|
|
||
| export function formatReadFileError(error: string | null | undefined, t: Translate): string { | ||
| const detail = normalizeDetail(error) | ||
| if (!detail || detail === 'Failed to read file' || detail === 'Missing session or path' || detail === 'Session unavailable') { | ||
| return t('file.error.readFailed') | ||
| } | ||
| return t('file.error.readFailedWithDetail', { error: detail }) | ||
| } | ||
|
|
||
| export function formatDiffError(error: string | null | undefined, t: Translate): string { | ||
| const detail = normalizeDetail(error) | ||
| if (!detail || detail === 'Failed to load diff' || detail === 'Missing session or path' || detail === 'Session unavailable') { | ||
| return t('file.error.diffUnavailable') | ||
| } | ||
| return t('file.error.diffUnavailableWithDetail', { error: detail }) | ||
| } | ||
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
Oops, something went wrong.
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.
[Minor] This only handles one prefixed failure, but
useGitStatusFilescan concatenate both diff errors into a single string (Unstaged ... Staged ...), so the second failure is folded into the translated detail and lost.Suggested fix:
Uh oh!
There was an error while loading. Please reload this page.
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.
Fixed in
3417f90.formatGitStatusErrornow splits combinedUnstaged ... Staged ...failures into segments and translates each one independently, so both errors are preserved. Added regression coverage inweb/src/lib/files-i18n.test.ts.