Skip to content

Commit 5ee4bec

Browse files
committed
First combining of Blockly localization along with react localization
1 parent 6cc482b commit 5ee4bec

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

public/locales/en/translation.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
"title": "Add Tab",
2020
"newItemPlaceholder": "Add Module",
2121
"search": "Search..."
22-
23-
}
22+
},
23+
"BLOCKLY":{
24+
"OF_TYPE": "of type"
25+
}
2426
}

src/App.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import * as ChangeFramework from './blocks/utils/change_framework'
4747
import { mutatorOpenListener } from './blocks/mrc_param_container'
4848
import { TOOLBOX_UPDATE_EVENT } from './blocks/mrc_mechanism_component_holder';
4949
import { antdThemeFromString } from './reactComponents/ThemeModal';
50+
import i18n from './i18n/config';
5051

5152
/** Storage key for shown toolbox categories. */
5253
const SHOWN_TOOLBOX_CATEGORIES_KEY = 'shownPythonToolboxCategories';

src/blocks/mrc_component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const COMPONENT = {
6969
this.setStyle(MRC_STYLE_COMPONENTS);
7070
this.appendDummyInput()
7171
.appendField(new Blockly.FieldTextInput(''), FIELD_NAME)
72-
.appendField('of type')
72+
.appendField(Blockly.Msg.OF_TYPE)
7373
.appendField(createFieldNonEditableText(''), FIELD_TYPE);
7474
this.setPreviousStatement(true, OUTPUT_NAME);
7575
this.setNextStatement(true, OUTPUT_NAME);

src/blocks/tokens.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Blockly from 'blockly/core';
2+
3+
export const customTokens = (t: (key: string) => string): typeof Blockly.Msg => {
4+
return {
5+
ADD_COMMENT: t('BLOCKLY.ADD_COMMENT'),
6+
REMOVE_COMMENT: t('BLOCKLY.REMOVE_COMMENT'),
7+
DUPLICATE_COMMENT: t('BLOCKLY.DUPLICATE_COMMENT'),
8+
OF_TYPE: t('BLOCKLY.OF_TYPE'),
9+
};
10+
};

src/reactComponents/BlocklyComponent.tsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@
2020
*/
2121
import * as React from 'react';
2222
import * as Blockly from 'blockly/core';
23-
import * as locale from 'blockly/msg/en';
23+
import * as En from 'blockly/msg/en';
24+
import * as Es from 'blockly/msg/es';
25+
import { customTokens } from '../blocks/tokens';
26+
2427
import { themes } from '../themes/mrc_themes';
2528
import {pluginInfo as HardwareConnectionsPluginInfo} from '../blocks/utils/connection_checker';
2629

2730
import 'blockly/blocks'; // Includes standard blocks like controls_if, logic_compare, etc.
31+
import { useTranslation } from 'react-i18next';
32+
2833

2934
/** Interface for methods exposed by the BlocklyComponent. */
3035
export interface BlocklyComponentType {
@@ -78,6 +83,10 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
7883
const blocklyDiv = React.useRef<HTMLDivElement | null>(null);
7984
const workspaceRef = React.useRef<Blockly.WorkspaceSvg | null>(null);
8085

86+
const { t, i18n } = useTranslation();
87+
// const { t } = useTranslation();
88+
89+
8190
const getBlocklyTheme = (): Blockly.Theme => {
8291
const blocklyTheme = 'mrc_theme_' + theme.replace(/-/g, '_');
8392
// Find the theme by key
@@ -133,8 +142,18 @@ const BlocklyComponent = React.forwardRef<BlocklyComponentType | null, BlocklyCo
133142
}
134143

135144
// Set Blockly locale
136-
Blockly.setLocale(locale as any);
137-
145+
switch (i18n.language) {
146+
case 'es':
147+
Blockly.setLocale(Es as any);
148+
break;
149+
case 'en':
150+
Blockly.setLocale(En as any);
151+
break;
152+
default:
153+
Blockly.setLocale(En as any);
154+
break;
155+
}
156+
Blockly.setLocale(customTokens(t));
138157
// Create workspace
139158
const workspaceConfig = createWorkspaceConfig();
140159
const workspace = Blockly.inject(blocklyDiv.current, workspaceConfig);

0 commit comments

Comments
 (0)