Skip to content

Commit

Permalink
fix: add curly brace pattern validation for shared strings
Browse files Browse the repository at this point in the history
See #350.
  • Loading branch information
liammulh committed Feb 8, 2023
1 parent cac9496 commit 9d68c31
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
50 changes: 26 additions & 24 deletions src/client/components/InputErrorMessage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import { useContext } from 'react';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';
import { ErrorContext } from './TranslationForm.jsx';
import questionOctagon from '../img/question-octagon.svg';

Expand All @@ -21,15 +22,15 @@ const InputErrorMessage = ( { fieldKey } ) => {
marginLeft: '6px'
};
const tooltip = 'Curly brace patterns must match the English version. ' +
'The values inside of the curly braces must also match the English version. ' +
'These curly brace patterns are values that dynamically change in the sim.\n\n' +
'For example, {0}: {1} cannot be translated to {x}: {y}. ' +
'However, the order of the {0} and the {1} can change. ' +
'So {0}: {1} could be translated to {1}: {0}.\n\n' +
'The text outside the curly braces can be changed. ' +
'For example, Software Development: {0} could be translated to Software Dev: {0}.\n\n' +
'The rules above apply to string keys with double curly braces as well.\n\n' +
'For further guidance, please read the user guide.';
'The values inside of the curly braces must also match the English version. ' +
'These curly brace patterns are values that dynamically change in the sim.\n\n' +
'For example, {0}: {1} cannot be translated to {x}: {y}. ' +
'However, the order of the {0} and the {1} can change. ' +
'So {0}: {1} could be translated to {1}: {0}.\n\n' +
'The text outside the curly braces can be changed. ' +
'For example, Software Development: {0} could be translated to Software Dev: {0}.\n\n' +
'The rules above apply to string keys with double curly braces as well.\n\n' +
'For further guidance, please read the user guide.';
const errorTooltipJsx = (
<button
onClick={() => window.alert( tooltip )}
Expand All @@ -48,21 +49,22 @@ const InputErrorMessage = ( { fieldKey } ) => {
const error = useContext( ErrorContext );
let jsx = null;
if ( Object.keys( error ).length > 0 ) {
if (
error.simSpecific &&
error.simSpecific[ fieldKey ] &&
error.simSpecific[ fieldKey ].translated
) {
const errorMessage = error.simSpecific[ fieldKey ].translated;
jsx = <>{errorTooltipJsx}<div style={divStyle}>{errorMessage}</div></>;
}
else if (
error.common &&
error.common[ fieldKey ] &&
error.common[ fieldKey ].translated
) {
const errorMessage = error.common[ fieldKey ].translated;
jsx = <>{errorTooltipJsx}<div style={divStyle}>{errorMessage}</div></>;
for ( const keyType of Object.values( KeyTypesEnum ) ) {
if (
error[ keyType ] &&
error[ keyType ][ fieldKey ] &&
error[ keyType ][ fieldKey ].translated
) {
const errorMessage = error[ keyType ][ fieldKey ].translated;
jsx = (
<>
{errorTooltipJsx}
<div style={divStyle}>
{errorMessage}
</div>
</>
);
}
}
}
return jsx;
Expand Down
2 changes: 1 addition & 1 deletion src/client/components/TranslationFormTables.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import React, { useEffect, useState } from 'react';
import TranslationFormRow from './TranslationFormRow.jsx';
import KeyTypesEnum from '../js/KeyTypesEnum.js';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';

/**
* This component has some headers, some info, and a table for sim-specific strings and common strings. The header
Expand Down
2 changes: 1 addition & 1 deletion src/client/js/computeTranslatedStringsData.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2022, University of Colorado Boulder

import KeyTypesEnum from './KeyTypesEnum.js';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';

/**
* Compute the number of strings translated in the translation form. To be presented to user
Expand Down
5 changes: 3 additions & 2 deletions src/client/js/makeValidationSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as Yup from 'yup';
import alertErrorMessage from './alertErrorMessage.js';
import isValidBracePattern from './isValidBracePattern.js';
import publicConfig from '../../common/publicConfig.js';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';

/**
* Return a Yup schema for validating our Formik translation form.
Expand All @@ -24,9 +25,8 @@ const makeValidationSchema = translationFormData => {
let validationSchema;

// We need to iterate over both types of keys.
const typesOfKeys = [ 'simSpecific', 'common' ];
try {
for ( const keyType of typesOfKeys ) {
for ( const keyType of Object.values( KeyTypesEnum ) ) {

// We have to replace dots in string keys with _DOT_. Otherwise,
// we might have errors related to the code think there are more
Expand Down Expand Up @@ -55,6 +55,7 @@ const makeValidationSchema = translationFormData => {
}
const validationObject = {
simSpecific: Yup.object( subObjects.simSpecific ),
shared: Yup.object( subObjects.shared ),
common: Yup.object( subObjects.common )
};
validationSchema = Yup.object().shape( validationObject );
Expand Down
2 changes: 1 addition & 1 deletion src/client/js/submitTranslation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import axios from 'axios';
import alertErrorMessage from './alertErrorMessage.js';
import publicConfig from '../../common/publicConfig.js';
import computeTranslatedStringsData from './computeTranslatedStringsData.js';
import KeyTypesEnum from './KeyTypesEnum.js';
import KeyTypesEnum from '../../common/KeyTypesEnum.js';
import makeTranslationObject from './makeTranslationObject.js';

/**
Expand Down
File renamed without changes.

0 comments on commit 9d68c31

Please sign in to comment.