Skip to content

Commit 578b646

Browse files
committed
Go away from %1 %2
1 parent b635c4e commit 578b646

File tree

7 files changed

+65
-43
lines changed

7 files changed

+65
-43
lines changed

src/blocks/mrc_get_python_enum_value.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { createFieldDropdown } from '../fields/FieldDropdown';
3030
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
3131
import { MRC_STYLE_ENUM } from '../themes/styles'
3232
import * as toolboxItems from '../toolbox/items';
33+
import { replaceTokens } from './tokens';
3334

3435

3536
// A block to access a python enum.
@@ -75,9 +76,10 @@ const GET_PYTHON_ENUM_VALUE = {
7576
this.setTooltip(() => {
7677
const enumClassName = this.getFieldValue(FIELD_ENUM_CLASS_NAME);
7778
const enumValue = this.getFieldValue(FIELD_ENUM_VALUE);
78-
let tooltip = Blockly.Msg['GET_ENUM_VALUE_TOOLTIP']
79-
.replace('%1', enumClassName)
80-
.replace('%2', enumValue);
79+
let tooltip = replaceTokens(Blockly.Msg['GET_ENUM_VALUE_TOOLTIP'], {
80+
enumName: enumClassName,
81+
valueName: enumValue
82+
});
8183
const enumTooltip = PythonEnumTooltips[enumClassName]
8284
if (enumTooltip) {
8385
if (typeof enumTooltip === 'string') {

src/blocks/mrc_get_python_variable.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { createFieldDropdown } from '../fields/FieldDropdown';
3434
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
3535
import { MRC_STYLE_VARIABLES } from '../themes/styles';
3636
import * as toolboxItems from '../toolbox/items';
37+
import { replaceTokens } from './tokens';
3738

3839

3940
// A block to get a python variable.
@@ -142,23 +143,26 @@ const GET_PYTHON_VARIABLE = {
142143
switch (this.mrcVarKind) {
143144
case VariableKind.MODULE: {
144145
const moduleName = this.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
145-
tooltip = Blockly.Msg['GET_MODULE_VARIABLE_TOOLTIP']
146-
.replace('%1', moduleName)
147-
.replace('%2', varName);
146+
tooltip = replaceTokens(Blockly.Msg['GET_MODULE_VARIABLE_TOOLTIP'], {
147+
moduleName: moduleName,
148+
varName: varName
149+
});
148150
break;
149151
}
150152
case VariableKind.CLASS: {
151153
const className = this.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
152-
tooltip = Blockly.Msg['GET_CLASS_VARIABLE_TOOLTIP']
153-
.replace('%1', className)
154-
.replace('%2', varName);
154+
tooltip = replaceTokens(Blockly.Msg['GET_CLASS_VARIABLE_TOOLTIP'], {
155+
className: className,
156+
varName: varName
157+
});
155158
break;
156159
}
157160
case VariableKind.INSTANCE: {
158161
const className = this.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
159-
tooltip = Blockly.Msg['GET_INSTANCE_VARIABLE_TOOLTIP']
160-
.replace('%1', varName)
161-
.replace('%2', className);
162+
tooltip = replaceTokens(Blockly.Msg['GET_INSTANCE_VARIABLE_TOOLTIP'], {
163+
varName: varName,
164+
className: className
165+
});
162166
break;
163167
}
164168
default:

src/blocks/mrc_set_python_variable.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { createFieldDropdown } from '../fields/FieldDropdown';
3131
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
3232
import { MRC_STYLE_VARIABLES } from '../themes/styles';
3333
import * as toolboxItems from '../toolbox/items';
34+
import { replaceTokens } from './tokens';
3435

3536

3637
// A block to set a python variable.
@@ -133,23 +134,26 @@ const SET_PYTHON_VARIABLE = {
133134
switch (this.mrcVarKind) {
134135
case VariableKind.MODULE: {
135136
const moduleName = this.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
136-
tooltip = Blockly.Msg['SET_MODULE_VARIABLE_TOOLTIP']
137-
.replace('%1', moduleName)
138-
.replace('%2', varName);
137+
tooltip = replaceTokens(Blockly.Msg['SET_MODULE_VARIABLE_TOOLTIP'], {
138+
moduleName: moduleName,
139+
varName: varName
140+
});
139141
break;
140142
}
141143
case VariableKind.CLASS: {
142144
const className = this.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
143-
tooltip = Blockly.Msg['SET_CLASS_VARIABLE_TOOLTIP']
144-
.replace('%1', className)
145-
.replace('%2', varName);
145+
tooltip = replaceTokens(Blockly.Msg['SET_CLASS_VARIABLE_TOOLTIP'], {
146+
className: className,
147+
varName: varName
148+
});
146149
break;
147150
}
148151
case VariableKind.INSTANCE: {
149152
const className = this.getFieldValue(FIELD_MODULE_OR_CLASS_NAME);
150-
tooltip = Blockly.Msg['SET_INSTANCE_VARIABLE_TOOLTIP']
151-
.replace('%1', varName)
152-
.replace('%2', className);
153+
tooltip = replaceTokens(Blockly.Msg['SET_INSTANCE_VARIABLE_TOOLTIP'], {
154+
varName: varName,
155+
className: className
156+
});
153157
break;
154158
}
155159
default:

src/blocks/tokens.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,16 @@ export function customTokens(t: (key: string) => string): typeof Blockly.Msg {
121121
MRC_CATEGORY_TEST: t('BLOCKLY.CATEGORY.TEST'),
122122
MRC_PRINT: t('BLOCKLY.PRINT'),
123123
}
124-
};
124+
};
125+
126+
/**
127+
* Replaces tokens in a string with values from a dictionary.
128+
* @param template String containing tokens in the format {{token}}
129+
* @param values Dictionary mapping token names to their replacement values
130+
* @return String with all tokens replaced by their corresponding values
131+
*/
132+
export function replaceTokens(template: string, values: Record<string, string>): string {
133+
return template.replace(/\{\{(\w+)\}\}/g, (match, token) => {
134+
return values[token] !== undefined ? values[token] : match;
135+
});
136+
}

src/i18n/locales/en/translation.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@
119119
"CALL_COMPONENT_INSTANCE_METHOD": "Calls the instance method {{className}}.{{functionName}} on the component named {{componentName}}.",
120120
"CALL_ROBOT_INSTANCE_METHOD": "Calls the robot method {{functionName}}.",
121121
"CALL_MECHANISM_INSTANCE_METHOD": "Calls the instance method {{className}}.{{functionName}} on the mechanism named {{mechanismName}}.",
122-
"GET_MODULE_VARIABLE": "Gets the variable %1.%2.",
123-
"GET_CLASS_VARIABLE": "Gets the variable %1.%2.",
124-
"GET_INSTANCE_VARIABLE": "Gets the variable %1 for the given %2 object.",
125-
"GET_ENUM_VALUE": "Gets the enum value %1.%2.",
126-
"SET_MODULE_VARIABLE": "Sets the variable %1.%2.",
127-
"SET_CLASS_VARIABLE": "Sets the variable %1.%2.",
128-
"SET_INSTANCE_VARIABLE": "Sets the variable %1 for the given %2 object."
122+
"GET_MODULE_VARIABLE": "Gets the variable {{moduleName}}.{{varName}}.",
123+
"GET_CLASS_VARIABLE": "Gets the variable {{className}}.{{varName}}.",
124+
"GET_INSTANCE_VARIABLE": "Gets the variable {{varName}} for the given {{className}} object.",
125+
"GET_ENUM_VALUE": "Gets the enum value {{enumName}}.{{valueName}}.",
126+
"SET_MODULE_VARIABLE": "Sets the variable {{moduleName}}.{{varName}}.",
127+
"SET_CLASS_VARIABLE": "Sets the variable {{className}}.{{varName}}.",
128+
"SET_INSTANCE_VARIABLE": "Sets the variable {{varName}} for the given {{className}} object."
129129
},
130130
"CATEGORY":{
131131
"LISTS": "Lists",

src/i18n/locales/es/translation.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,13 +120,13 @@
120120
"CALL_COMPONENT_INSTANCE_METHOD": "Llama al método de instancia {{className}}.{{functionName}} en el componente llamado {{componentName}}.",
121121
"CALL_ROBOT_INSTANCE_METHOD": "Llama al método robot {{functionName}}.",
122122
"CALL_MECHANISM_INSTANCE_METHOD": "Llama al método de instancia {{className}}.{{functionName}} en el mecanismo llamado {{mechanismName}}.",
123-
"GET_MODULE_VARIABLE": "Obtiene la variable %1.%2.",
124-
"GET_CLASS_VARIABLE": "Obtiene la variable %1.%2.",
125-
"GET_INSTANCE_VARIABLE": "Obtiene la variable %1 para el objeto %2 dado.",
126-
"GET_ENUM_VALUE": "Obtiene el valor de enumeración %1.%2.",
127-
"SET_MODULE_VARIABLE": "Establece la variable %1.%2.",
128-
"SET_CLASS_VARIABLE": "Establece la variable %1.%2.",
129-
"SET_INSTANCE_VARIABLE": "Establece la variable %1 para el objeto %2 dado."
123+
"GET_MODULE_VARIABLE": "Obtiene la variable {{moduleName}}.{{varName}}.",
124+
"GET_CLASS_VARIABLE": "Obtiene la variable {{className}}.{{varName}}.",
125+
"GET_INSTANCE_VARIABLE": "Obtiene la variable {{varName}} para el objeto {{className}} dado.",
126+
"GET_ENUM_VALUE": "Obtiene el valor de enumeración {{enumName}}.{{valueName}}.",
127+
"SET_MODULE_VARIABLE": "Establece la variable {{moduleName}}.{{varName}}.",
128+
"SET_CLASS_VARIABLE": "Establece la variable {{className}}.{{varName}}.",
129+
"SET_INSTANCE_VARIABLE": "Establece la variable {{varName}} para el objeto {{className}} dado."
130130
},
131131
"CATEGORY": {
132132
"LISTS": "Listas",

src/i18n/locales/he/translation.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@
119119
"CALL_COMPONENT_INSTANCE_METHOD": "קורא למתודה {{className}}.{{functionName}} על הרכיב {{componentName}}.",
120120
"CALL_ROBOT_INSTANCE_METHOD": "קורא למתודת הרובוט {{functionName}}.",
121121
"CALL_MECHANISM_INSTANCE_METHOD": "קורא למתודה {{className}}.{{functionName}} במנגנון {{mechanismName}}.",
122-
"GET_MODULE_VARIABLE": "מקבל את המשתנה %1.%2.",
123-
"GET_CLASS_VARIABLE": "מקבל את המשתנה %1.%2.",
124-
"GET_INSTANCE_VARIABLE": "מקבל את המשתנה %1 עבור אובייקט %2 נתון.",
125-
"GET_ENUM_VALUE": "מקבל את ערך ההספירה %1.%2.",
126-
"SET_MODULE_VARIABLE": "מגדיר את המשתנה %1.%2.",
127-
"SET_CLASS_VARIABLE": "מגדיר את המשתנה %1.%2.",
128-
"SET_INSTANCE_VARIABLE": "מגדיר את המשתנה %1 עבור אובייקט %2 נתון."
122+
"GET_MODULE_VARIABLE": "מקבל את המשתנה {{moduleName}}.{{varName}}.",
123+
"GET_CLASS_VARIABLE": "מקבל את המשתנה {{className}}.{{varName}}.",
124+
"GET_INSTANCE_VARIABLE": "מקבל את המשתנה {{varName}} עבור אובייקט {{className}} נתון.",
125+
"GET_ENUM_VALUE": "מקבל את ערך ההספירה {{enumName}}.{{valueName}}.",
126+
"SET_MODULE_VARIABLE": "מגדיר את המשתנה {{moduleName}}.{{varName}}.",
127+
"SET_CLASS_VARIABLE": "מגדיר את המשתנה {{className}}.{{varName}}.",
128+
"SET_INSTANCE_VARIABLE": "מגדיר את המשתנה {{varName}} עבור אובייקט {{className}} נתון."
129129
},
130130
"CATEGORY": {
131131
"LISTS": "רשימות",

0 commit comments

Comments
 (0)