Skip to content

Commit bc4d0f8

Browse files
committed
feat: add localization support
1 parent 242746b commit bc4d0f8

File tree

11 files changed

+110
-38
lines changed

11 files changed

+110
-38
lines changed

l10n/bundle.l10n.zh-cn.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"Navigate with ↑↓←→ (append @ to filter symbols or /=? to search)": "通过 ↑↓←→ 进行键盘导航(追加 @ 来过滤符号或 /=? 来搜索)",
3+
"Search for {0}": "搜索 {0}",
4+
"Depth: {0}": "深度: {0}",
5+
"The active editor cannot provide outline information.": "当前编辑器无法提供大纲信息。",
6+
"Contains elements with problems": "包含存在问题的元素",
7+
"{count} problems in this element": "此元素存在 {count} 个问题",
8+
"Navigation mode. Use ↑↓←→ to navigate.": "导航模式,使用 ↑↓←→ 进行导航",
9+
"[=] Regular expression": "[=] 正则搜索",
10+
"[?] Fuzzy search": "[?] 模糊搜索",
11+
"Jump to {anchor} ({line})": "跳转到 {anchor} ({line})",
12+
"start": "开始",
13+
"end": "结束",
14+
"Line {line}": "行 {line}",
15+
"Insert a region pair to fold the content between them": "插入一对区域标记来折叠它们之间的内容",
16+
"Code Region (Outline-Map)": "代码区域 (Outline-Map)",
17+
"Insert a tag to mark a specific point in the source code": "插入一个标记来标记源代码中的特定位置",
18+
"Code Tag (Outline-Map)": "代码标记 (Outline-Map)",
19+
"Re-sorting the outline...": "正在重新排序大纲...",
20+
"No symbols found in document {fileName}.": "在文档 {fileName} 中找不到符号。"
21+
}

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"license": "MIT",
2626
"engines": {
27-
"vscode": "^1.60.0"
27+
"vscode": "^1.73.0"
2828
},
2929
"categories": [
3030
"Other"
@@ -33,6 +33,7 @@
3333
"onView:outline-map-view"
3434
],
3535
"main": "./out/extension/index.js",
36+
"l10n": "./l10n",
3637
"badges": [
3738
{
3839
"href": "https://img.shields.io/",
@@ -880,6 +881,7 @@
880881
"dependencies": {
881882
"@types/vscode-webview": "^1.57.1",
882883
"@vscode/codicons": "^0.0.31",
884+
"@vscode/l10n": "^0.0.18",
883885
"minimatch": "^9.0.3",
884886
"overlayscrollbars": "^2.1.0",
885887
"sass": "^1.77.8"

pnpm-lock.yaml

Lines changed: 27 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ export class SymbolNode {
120120
docSymbol.range.contains(sibling.range) ||
121121
(
122122
docSymbol.range.contains(sibling.range.start) &&
123-
sibling.detail === "__om_Region__"
123+
sibling.detail === '__om_Region__'
124124
)
125125
) {
126126
docSymbol.children.push(sibling);

src/extension/outline.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Msg, UpdateMsg, Op, UpdateOp, DeleteOp, InsertOp, SymbolNode, MoveOp, P
44
import { WorkspaceSymbols } from './workspace';
55
import { RegionProvider } from './region';
66
import { deepClone } from '../utils';
7+
import { $t, injectL10nBundle } from '../l10n/host';
78

89
/**
910
* Initialization options for the outline view.
@@ -61,7 +62,7 @@ export class OutlineView implements WebviewViewProvider {
6162

6263
sort(sortBy: Sortby) {
6364
this.sortby = sortBy;
64-
this.clear('Re-sorting the outline...');
65+
this.clear($t('Re-sorting the outline...'));
6566
this.update(window.activeTextEditor?.document || window.visibleTextEditors[0].document);
6667
}
6768

@@ -394,6 +395,7 @@ export class OutlineView implements WebviewViewProvider {
394395
<style id="color-style"></style>
395396
</head>
396397
<body>
398+
${injectL10nBundle()}
397399
<script type="module" src="${getAssetUri('out/webview/index.js')}"></script>
398400
</body>
399401
</html>`;
@@ -429,10 +431,10 @@ export class OutlineView implements WebviewViewProvider {
429431

430432
update(textDocument: TextDocument | undefined) {
431433
if (!this.view?.visible) { // Outline view isn't visible, updates won't work
432-
return
434+
return;
433435
}
434436
if (textDocument === undefined) { // No active editor
435-
this.clear('The active editor cannot provide outline information.');
437+
this.clear($t('The active editor cannot provide outline information.'));
436438
return;
437439
}
438440
if (!this.isValidDocument(textDocument)) { // Switched to a non-supported document like output
@@ -442,7 +444,8 @@ export class OutlineView implements WebviewViewProvider {
442444
newOutlineTree.updateSymbols().then(() => {
443445
const newNodes = newOutlineTree.getNodes();
444446
if (!newNodes || newNodes.length === 0) {
445-
this.clear(`No symbols found in document '${textDocument.fileName}'.`);
447+
const fileName = textDocument.fileName.split('/').pop();
448+
this.clear($t('No symbols found in document {fileName}.', { fileName: fileName }));
446449
return;
447450
}
448451
this.workspaceSymbols?.updateSymbol(newNodes, textDocument.uri);

src/extension/region/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
import { config } from '../config';
3434
import './parser';
3535
import { RegionParser, RegionEntry, RegionType } from './parser';
36+
import { $t } from '../../l10n/host';
3637

3738
type RegionPair = [RegionEntry, RegionEntry];
3839

@@ -285,9 +286,17 @@ implements DocumentSymbolProvider, FoldingRangeProvider, RenameProvider
285286
);
286287

287288
const command = `command:outline-map.context.goToLocation?${GoToPairArgs}`;
288-
const commandLabel = `Jump to ${
289-
index === 0 ? 'end' : 'start'
290-
} (Line ${region[1 - index].identifier?.range.start.line})`;
289+
290+
291+
const commandLabel = $t(
292+
'Jump to {anchor} ({line})',
293+
{
294+
anchor: index === 0 ? $t('end') : $t('start'),
295+
line: $t('Line {line}', {
296+
line: region[1 - index].identifier?.range.start.line,
297+
}),
298+
}
299+
);
291300

292301
hoverMessage.appendMarkdown(
293302
`[${commandLabel}](${command})`
@@ -462,9 +471,9 @@ export class RegionCompletionProvider implements CompletionItemProvider {
462471
`${this.startRegion} $\{1:name} $\{2:description}\n$0\n${this.endRegion} $\{1:name}`
463472
);
464473
const mdDocument = new MarkdownString(
465-
'Insert a region pair to fold the content between them'
474+
$t('Insert a region pair to fold the content between them')
466475
);
467-
const detail = 'Code Region (Outline-Map)';
476+
const detail = $t('Code Region (Outline-Map)');
468477
mdDocument.appendCodeblock(
469478
`${this.startRegion} name description\n...\n${this.endRegion} name`
470479
);
@@ -480,9 +489,9 @@ export class RegionCompletionProvider implements CompletionItemProvider {
480489
`${this.tag} $\{1:name} $\{2:description}`
481490
);
482491
const mdDocument = new MarkdownString(
483-
'Insert a tag to mark a specific point in the source code'
492+
$t('Insert a tag to mark a specific point in the source code')
484493
);
485-
const detail = 'Code Tag (Outline-Map)';
494+
const detail = $t('Code Tag (Outline-Map)');
486495
mdDocument.appendCodeblock(`${this.tag} name description`);
487496
const item = new CompletionItem(this.tag, CompletionItemKind.Issue);
488497
item.insertText = insertText;

src/l10n/host.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { l10n } from 'vscode';
2+
export function injectL10nBundle() {
3+
const l10nBundle = l10n.bundle;
4+
const str = l10nBundle ? JSON.stringify(l10nBundle) : 'undefined';
5+
return `
6+
<script>
7+
const __l10nBundle = ${str};
8+
</script>
9+
`;
10+
}
11+
12+
export const $t = l10n.t;

src/l10n/webview.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as l10n from '@vscode/l10n';
2+
declare const __l10nBundle: { [key: string]: string } | null;
3+
4+
if (__l10nBundle) {
5+
l10n.config({
6+
contents: __l10nBundle,
7+
});
8+
}
9+
10+
export const $t = l10n.t;

src/webview/components/inputArea.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-non-null-assertion */
2+
import { $t } from '../../l10n/webview';
23
import { SymbolKindList, SymbolKindStr, camelToDash, mapIcon } from '../../utils';
34
import { Mode } from '../input';
45

@@ -37,7 +38,7 @@ export class InputArea extends HTMLElement {
3738
name="input-text"
3839
id="input-text"
3940
style="height: calc(var(--vscode-font-size) + 10px);overflow-y:hidden;"
40-
title="Navigate to symbol by 🢐⬍🢒(append @ to filter symbols or /=? to search)"
41+
title="${$t('Navigate with ↑↓←→ (append @ to filter symbols or /=? to search)')}"
4142
></textarea>
4243
<div class="highlight"></div>
4344
<div id="symbol-list">
@@ -113,7 +114,7 @@ export class InputArea extends HTMLElement {
113114
this.filteredSymbol = symbol;
114115
this._filterEle.querySelector('.icon')!.className =
115116
`icon codicon codicon-${mapIcon(this.filteredSymbol)}`;
116-
this._filterEle.title = `Search for ${this.filteredSymbol}`;
117+
this._filterEle.title = $t('Search for {0}', this.filteredSymbol);
117118
this.filtering = false;
118119
this.filter('');
119120
this._textarea.value = '';

0 commit comments

Comments
 (0)