Skip to content

Commit 04e46d2

Browse files
committed
refactor: Use localizable messages.
1 parent 8a9e9d9 commit 04e46d2

File tree

11 files changed

+88
-61
lines changed

11 files changed

+88
-61
lines changed

src/actions/clipboard.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Gesture,
1010
ShortcutRegistry,
1111
Events,
12+
Msg,
1213
utils as blocklyUtils,
1314
clipboard,
1415
ICopyData,
@@ -104,7 +105,8 @@ export class Clipboard {
104105
*/
105106
private registerCutContextMenuAction() {
106107
const cutAction: ContextMenuRegistry.RegistryItem = {
107-
displayText: (scope) => `Cut (${getShortActionShortcut('cut')})`,
108+
displayText: (scope) =>
109+
Msg['CUT_SHORTCUT'].replace('%1', getShortActionShortcut('cut')),
108110
preconditionFn: (scope) => {
109111
const ws = scope.block?.workspace;
110112
if (!ws) return 'hidden';
@@ -199,7 +201,8 @@ export class Clipboard {
199201
*/
200202
private registerCopyContextMenuAction() {
201203
const copyAction: ContextMenuRegistry.RegistryItem = {
202-
displayText: (scope) => `Copy (${getShortActionShortcut('copy')})`,
204+
displayText: (scope) =>
205+
Msg['COPY_SHORTCUT'].replace('%1', getShortActionShortcut('copy')),
203206
preconditionFn: (scope) => {
204207
const ws = scope.block?.workspace;
205208
if (!ws) return 'hidden';
@@ -304,7 +307,8 @@ export class Clipboard {
304307
*/
305308
private registerPasteContextMenuAction() {
306309
const pasteAction: ContextMenuRegistry.RegistryItem = {
307-
displayText: (scope) => `Paste (${getShortActionShortcut('paste')})`,
310+
displayText: (scope) =>
311+
Msg['PASTE_SHORTCUT'].replace('%1', getShortActionShortcut('paste')),
308312
preconditionFn: (scope: ContextMenuRegistry.Scope) => {
309313
let block;
310314
if (scope.focusedNode instanceof Blockly.Block) {

src/actions/delete.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
import {
88
ContextMenuRegistry,
99
Gesture,
10+
Msg,
1011
ShortcutRegistry,
1112
utils as BlocklyUtils,
1213
LineCursor,
1314
} from 'blockly';
15+
import {getShortActionShortcut} from '../shortcut_formatting';
1416
import * as Constants from '../constants';
1517
import type {WorkspaceSvg} from 'blockly';
1618
import {Navigation} from '../navigation';
@@ -102,16 +104,17 @@ export class DeleteAction {
102104

103105
const deleteItem: ContextMenuRegistry.RegistryItem = {
104106
displayText: (scope) => {
107+
const shortcut = getShortActionShortcut(this.deleteShortcutName);
105108
if (!this.oldContextMenuItem) {
106-
return 'Delete block (Del)';
109+
return Msg['DELETE_BLOCK'].replace('%1', shortcut);
107110
}
108111

109112
type DisplayTextFn = (p1: ContextMenuRegistry.Scope) => string;
110113
// Use the original item's text, which is dynamic based on the number
111114
// of blocks that will be deleted.
112115
const oldDisplayText = this.oldContextMenuItem
113116
.displayText as DisplayTextFn;
114-
return oldDisplayText(scope) + ' (Del)';
117+
return oldDisplayText(scope) + ` (${shortcut})`;
115118
},
116119
preconditionFn: (scope, menuOpenEvent: Event) => {
117120
const ws = scope.block?.workspace;

src/actions/edit.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
import {ContextMenuRegistry, LineCursor} from 'blockly';
7+
import {ContextMenuRegistry, LineCursor, Msg} from 'blockly';
88
import {Navigation} from 'src/navigation';
9+
import {getShortActionShortcut} from '../shortcut_formatting';
10+
import * as Constants from '../constants';
911

1012
/**
1113
* Action to edit a block. This just moves the cursor to the first
@@ -50,7 +52,10 @@ export class EditAction {
5052
*/
5153
private registerContextMenuAction() {
5254
const editAboveItem: ContextMenuRegistry.RegistryItem = {
53-
displayText: 'Edit Block contents (→︎)',
55+
displayText: Msg['EDIT_BLOCK_CONTENTS'].replace(
56+
'%1',
57+
getShortActionShortcut(Constants.SHORTCUT_NAMES.RIGHT),
58+
),
5459
preconditionFn: (scope: ContextMenuRegistry.Scope, menuOpenEvent) => {
5560
if (menuOpenEvent instanceof PointerEvent) return 'hidden';
5661
const workspace = scope.block?.workspace;

src/actions/enter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import {
88
Events,
9+
Msg,
910
ShortcutRegistry,
1011
utils as BlocklyUtils,
1112
getFocusManager,

src/actions/insert.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
import {
88
ContextMenuRegistry,
9+
Msg,
910
ShortcutRegistry,
1011
utils as BlocklyUtils,
1112
} from 'blockly';
13+
import {getShortActionShortcut} from '../shortcut_formatting';
1214
import * as Constants from '../constants';
1315
import type {WorkspaceSvg} from 'blockly';
1416
import {Navigation} from '../navigation';
@@ -69,7 +71,10 @@ export class InsertAction {
6971
private registerContextMenuAction() {
7072
const insertAboveItem: ContextMenuRegistry.RegistryItem = {
7173
displayText: () => {
72-
return 'Insert Block (I)';
74+
return Msg['INSERT_BLOCK'].replace(
75+
'%1',
76+
getShortActionShortcut(this.insertShortcutName),
77+
);
7378
},
7479
preconditionFn: (
7580
scope: ContextMenuRegistry.Scope,

src/actions/move.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import {
88
BlockSvg,
99
ContextMenuRegistry,
10+
Msg,
1011
ShortcutRegistry,
1112
utils,
1213
WorkspaceSvg,
1314
} from 'blockly';
1415
import {Direction} from '../drag_direction';
1516
import {Mover} from './mover';
17+
import {getShortActionShortcut} from '../shortcut_formatting';
1618

1719
const KeyCodes = utils.KeyCodes;
1820
const createSerializedKey = ShortcutRegistry.registry.createSerializedKey.bind(
@@ -28,7 +30,7 @@ export class MoveActions {
2830
private shortcuts: ShortcutRegistry.KeyboardShortcut[] = [
2931
// Begin and end move.
3032
{
31-
name: 'Start move',
33+
name: Msg['START_MOVE'],
3234
preconditionFn: (workspace) => {
3335
const startBlock = this.getCurrentBlock(workspace);
3436
return !!startBlock && this.mover.canMove(workspace, startBlock);
@@ -42,14 +44,14 @@ export class MoveActions {
4244
keyCodes: [KeyCodes.M],
4345
},
4446
{
45-
name: 'Finish move',
47+
name: Msg['FINISH_MOVE'],
4648
preconditionFn: (workspace) => this.mover.isMoving(workspace),
4749
callback: (workspace) => this.mover.finishMove(workspace),
4850
keyCodes: [KeyCodes.ENTER, KeyCodes.SPACE],
4951
allowCollision: true,
5052
},
5153
{
52-
name: 'Abort move',
54+
name: Msg['ABORT_MOVE'],
5355
preconditionFn: (workspace) => this.mover.isMoving(workspace),
5456
callback: (workspace) => this.mover.abortMove(workspace),
5557
keyCodes: [KeyCodes.ESC],
@@ -58,31 +60,31 @@ export class MoveActions {
5860

5961
// Constrained moves.
6062
{
61-
name: 'Move left, constrained',
63+
name: Msg['MOVE_LEFT_CONSTRAINED'],
6264
preconditionFn: (workspace) => this.mover.isMoving(workspace),
6365
callback: (workspace) =>
6466
this.mover.moveConstrained(workspace, Direction.Left),
6567
keyCodes: [KeyCodes.LEFT],
6668
allowCollision: true,
6769
},
6870
{
69-
name: 'Move right constrained',
71+
name: Msg['MOVE_RIGHT_CONSTRAINED'],
7072
preconditionFn: (workspace) => this.mover.isMoving(workspace),
7173
callback: (workspace) =>
7274
this.mover.moveConstrained(workspace, Direction.Right),
7375
keyCodes: [KeyCodes.RIGHT],
7476
allowCollision: true,
7577
},
7678
{
77-
name: 'Move up, constrained',
79+
name: Msg['MOVE_UP_CONSTRAINED'],
7880
preconditionFn: (workspace) => this.mover.isMoving(workspace),
7981
callback: (workspace) =>
8082
this.mover.moveConstrained(workspace, Direction.Up),
8183
keyCodes: [KeyCodes.UP],
8284
allowCollision: true,
8385
},
8486
{
85-
name: 'Move down constrained',
87+
name: Msg['MOVE_DOWN_CONSTRAINED'],
8688
preconditionFn: (workspace) => this.mover.isMoving(workspace),
8789
callback: (workspace) =>
8890
this.mover.moveConstrained(workspace, Direction.Down),
@@ -92,7 +94,7 @@ export class MoveActions {
9294

9395
// Unconstrained moves.
9496
{
95-
name: 'Move left, unconstrained',
97+
name: Msg['MOVE_LEFT_UNCONSTRAINED'],
9698
preconditionFn: (workspace) => this.mover.isMoving(workspace),
9799
callback: (workspace) =>
98100
this.mover.moveUnconstrained(workspace, Direction.Left),
@@ -102,7 +104,7 @@ export class MoveActions {
102104
],
103105
},
104106
{
105-
name: 'Move right, unconstrained',
107+
name: Msg['MOVE_RIGHT_UNCONSTRAINED'],
106108
preconditionFn: (workspace) => this.mover.isMoving(workspace),
107109
callback: (workspace) =>
108110
this.mover.moveUnconstrained(workspace, Direction.Right),
@@ -112,7 +114,7 @@ export class MoveActions {
112114
],
113115
},
114116
{
115-
name: 'Move up unconstrained',
117+
name: Msg['MOVE_UP_UNCONSTRAINED'],
116118
preconditionFn: (workspace) => this.mover.isMoving(workspace),
117119
callback: (workspace) =>
118120
this.mover.moveUnconstrained(workspace, Direction.Up),
@@ -122,7 +124,7 @@ export class MoveActions {
122124
],
123125
},
124126
{
125-
name: 'Move down, unconstrained',
127+
name: Msg['MOVE_DOWN_UNCONSTRAINED'],
126128
preconditionFn: (workspace) => this.mover.isMoving(workspace),
127129
callback: (workspace) =>
128130
this.mover.moveUnconstrained(workspace, Direction.Down),
@@ -135,7 +137,10 @@ export class MoveActions {
135137

136138
menuItems: ContextMenuRegistry.RegistryItem[] = [
137139
{
138-
displayText: 'Move Block (M)',
140+
displayText: Msg['MOVE_BLOCK'].replace(
141+
'%1',
142+
getShortActionShortcut(Msg['START_MOVE']),
143+
),
139144
preconditionFn: (scope, menuOpenEvent) => {
140145
const workspace = scope.block?.workspace as WorkspaceSvg | null;
141146
if (!workspace || menuOpenEvent instanceof PointerEvent)

src/constants.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @author [email protected] (Abby Schmiedt)
1313
*/
1414

15+
import {Msg} from 'blockly/core';
16+
1517
/**
1618
* Keyboard navigation states.
1719
* The different parts of Blockly that the user navigates between.
@@ -70,29 +72,31 @@ export const SHORTCUT_CATEGORIES: Record<
7072
// better text because temporarily the name in the table is derived from
7173
// these id-like names.
7274
Array<SHORTCUT_NAMES | 'undo' | 'redo' | 'cut' | 'copy' | 'paste' | 'delete'>
73-
> = {
74-
'General': [
75-
SHORTCUT_NAMES.MENU,
76-
SHORTCUT_NAMES.EDIT_OR_CONFIRM,
77-
SHORTCUT_NAMES.EXIT,
78-
SHORTCUT_NAMES.TOOLBOX,
79-
SHORTCUT_NAMES.CLEAN_UP,
80-
SHORTCUT_NAMES.LIST_SHORTCUTS,
81-
],
82-
'Editing': [
83-
SHORTCUT_NAMES.INSERT,
84-
'delete',
85-
SHORTCUT_NAMES.DISCONNECT,
86-
'cut',
87-
'copy',
88-
'paste',
89-
'undo',
90-
'redo',
91-
],
92-
'Code navigation': [
93-
SHORTCUT_NAMES.UP,
94-
SHORTCUT_NAMES.DOWN,
95-
SHORTCUT_NAMES.RIGHT,
96-
SHORTCUT_NAMES.LEFT,
97-
],
98-
};
75+
> = {};
76+
77+
SHORTCUT_CATEGORIES[Msg['SHORTCUTS_GENERAL']] = [
78+
SHORTCUT_NAMES.MENU,
79+
SHORTCUT_NAMES.EDIT_OR_CONFIRM,
80+
SHORTCUT_NAMES.EXIT,
81+
SHORTCUT_NAMES.TOOLBOX,
82+
SHORTCUT_NAMES.CLEAN_UP,
83+
SHORTCUT_NAMES.LIST_SHORTCUTS,
84+
];
85+
86+
SHORTCUT_CATEGORIES[Msg['SHORTCUTS_EDITING']] = [
87+
SHORTCUT_NAMES.INSERT,
88+
'delete',
89+
SHORTCUT_NAMES.DISCONNECT,
90+
'cut',
91+
'copy',
92+
'paste',
93+
'undo',
94+
'redo',
95+
];
96+
97+
SHORTCUT_CATEGORIES[Msg['SHORTCUTS_CODE_NAVIGATION']] = [
98+
SHORTCUT_NAMES.UP,
99+
SHORTCUT_NAMES.DOWN,
100+
SHORTCUT_NAMES.RIGHT,
101+
SHORTCUT_NAMES.LEFT,
102+
];

src/hints.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* SPDX-License-Identifier: Apache-2.0
77
*/
88

9-
import {WorkspaceSvg, Toast} from 'blockly';
9+
import {Msg, WorkspaceSvg, Toast} from 'blockly';
1010
import {SHORTCUT_NAMES} from './constants';
1111
import {getShortActionShortcut} from './shortcut_formatting';
1212

@@ -104,7 +104,7 @@ export function clearPasteHints(workspace: WorkspaceSvg) {
104104
*/
105105
export function showHelpHint(workspace: WorkspaceSvg) {
106106
const shortcut = getShortActionShortcut('list_shortcuts');
107-
const message = `Press ${shortcut} for help on keyboard controls`;
107+
const message = Msg['HELP_PROMPT'].replace('%1', shortcut);
108108
const id = helpHintId;
109109
Toast.show(workspace, {message, id});
110110
}

src/navigation_controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ export class NavigationController {
239239
ShortcutRegistry.registry.register(shortcut);
240240
}
241241
this.deleteAction.install();
242-
this.editAction.install();
243242
this.insertAction.install();
244243
this.workspaceMovement.install();
245244
this.arrowNavigation.install();
245+
this.editAction.install();
246246
this.exitAction.install();
247247
this.enterAction.install();
248248
this.disconnectAction.install();

src/shortcut_dialog.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import * as Blockly from 'blockly/core';
88
import * as Constants from './constants';
9-
import {ShortcutRegistry} from 'blockly/core';
9+
import {Msg, ShortcutRegistry} from 'blockly/core';
1010
import {
1111
getLongActionShortcutsAsKeys,
1212
upperCaseFirst,
@@ -39,16 +39,16 @@ export class ShortcutDialog {
3939
getPlatform() {
4040
const {platform, userAgent} = navigator;
4141
if (platform.startsWith('Win')) {
42-
return 'Windows';
42+
return Msg['WINDOWS'];
4343
} else if (platform.startsWith('Mac')) {
44-
return 'macOS';
44+
return Msg['MAC_OS'];
4545
} else if (/\bCrOS\b/.test(userAgent)) {
4646
// Order is important because platform matches the Linux case below.
47-
return 'ChromeOS';
47+
return Msg['CHROME_OS'];
4848
} else if (platform.includes('Linux')) {
49-
return 'Linux';
49+
return Msg['LINUX'];
5050
} else {
51-
return 'Unknown';
51+
return Msg['UNKNOWN'];
5252
}
5353
}
5454

0 commit comments

Comments
 (0)