Skip to content

Commit

Permalink
(EPROC-20881) Fixed translations prefix usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
azinchenko-sc authored Nov 15, 2021
1 parent 137589e commit c131cf3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/components/EditHeading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Row from 'react-bootstrap/lib/Row';
import ButtonGroup from 'react-bootstrap/lib/ButtonGroup';
import Button from 'react-bootstrap/lib/Button';
import Glyphicon from 'react-bootstrap/lib/Glyphicon';
import { getModelMessage, getTabLabel } from '../lib';
import { getKeyWithPrefix, getModelMessage, getTabLabel } from '../lib';
import { VIEW_CREATE } from '../../crudeditor-lib/common/constants';
import ConfirmUnsavedChanges from '../ConfirmDialog/ConfirmUnsavedChanges';

Expand Down Expand Up @@ -58,7 +58,7 @@ export default class EditHeading extends PureComponent {

const { i18n } = this.context;

const modelName = getModelMessage({ i18n, key: 'model.name' });
const modelName = getModelMessage({ i18n, key: getKeyWithPrefix(i18n, 'model.name') });

const title = exitView ?
(
Expand Down
4 changes: 2 additions & 2 deletions src/components/SearchMain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col';
import Form from '../SearchForm';
import Result from '../SearchResult';
import { getModelMessage } from '../lib';
import { getModelMessage, getKeyWithPrefix } from '../lib';
import './SearchMain.less';

export default class SearchMain extends PureComponent {
Expand Down Expand Up @@ -51,7 +51,7 @@ export default class SearchMain extends PureComponent {
<Row>
<Col xs={8}>

{getModelMessage({ i18n, key: 'model.name' })}
{getModelMessage({ i18n, key: getKeyWithPrefix(i18n, 'model.name') })}

<Button
bsStyle="link"
Expand Down
11 changes: 7 additions & 4 deletions src/components/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@ export const titleCase = str => typeof str === 'string' ?
str.charAt(0).toUpperCase() + str.slice(1).replace(/[^A-Z](?=[A-Z])/g, '$& ') :
str;

export const getKeyWithPrefix = (i18n, key) =>
i18n.hasOwnProperty('getKeyWithPrefix') ? i18n.getKeyWithPrefix(key) : key;

export const getModelMessage = request => {
const { i18n, key, args, defaultMessage } = request;

// If i18n doesn't find a message by key, it returns the key itself.
// Optinal 'defaultMessage' defines default message for such a case.
// Optional 'defaultMessage' defines default message for such a case.
const message = i18n.getMessage(key, args);

return message.slice(-key.length) === key && request.hasOwnProperty('defaultMessage') ?
Expand Down Expand Up @@ -57,20 +60,20 @@ export const getFieldErrorMessage = ({ error, i18n, fieldName }) => {
// try to find a translation defined by model
return getModelMessage({
i18n,
key: `model.field.${fieldName}.error.${id}`,
key: getKeyWithPrefix(i18n, `model.field.${fieldName}.error.${id}`),
args,
defaultMessage: message || id
})
}

export const getTabLabel = ({ i18n, name }) => getModelMessage({
i18n,
key: `model.tab.${name}.label`,
key: getKeyWithPrefix(i18n, `model.tab.${name}.label`),
defaultMessage: titleCase(name)
});

export const getFieldLabel = ({ i18n, name }) => getModelMessage({
i18n,
key: `model.field.${name}.label`,
key: getKeyWithPrefix(i18n, `model.field.${name}.label`),
defaultMessage: titleCase(name)
})
22 changes: 19 additions & 3 deletions src/crudeditor-lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,22 @@ export default baseModelDefinition => {
// modelDefinition also needs to be accessed outside of constructor scope, therefore pin it to 'this'
this.modelDefinition = modelDefinition;

let prefix = modelDefinition.model.translationsKeyPrefix
let translations = modelDefinition.model.translations
const configuredPrefix = modelDefinition.model.translationsKeyPrefix;
let translations = modelDefinition.model.translations;
let prefix = null;

if (!prefix) {
if (!configuredPrefix) {
// Since "object-hash" package does not support async functions, remove them from modelDefinition.
// For more details see
// https://github.com/puleos/object-hash/issues/67
prefix = `${appName}.${hash(JSON.parse(JSON.stringify(modelDefinition)))}`;

// model translations
// add generated prefix manually to translations
const prefixedTranslations = getPrefixedTranslations(translations, prefix);
this.context.i18n.register(prefix, prefixedTranslations);
} else {
prefix = configuredPrefix;
this.context.i18n.register(prefix, translations);
}

Expand All @@ -172,7 +175,20 @@ export default baseModelDefinition => {
args
);
}
},

getKeyWithPrefix: {
get() {
return (key) => {
if (configuredPrefix) {
return `${configuredPrefix}.${key}`;
} else {
return key;
}
}
}
}

});

this.adjustedContext = {
Expand Down

0 comments on commit c131cf3

Please sign in to comment.