Define Translations
Out of the box, spartacus-mock uses the default english translations provided by spartacus itself. You can override these translations, enhance them with additional translations or add your own custom translations:
Override default translations
Open the file
src/mock-server/mock-data/translations/translations.ts
(created by the setup schematics)Out of the box, the default translations from Spartacus Assets are imported in this file
Create a folder
en
within thetranslations
folderCreate a file
src/mock-server/mock-data/translations/en/product.ts
with the following content
// src/mock-server/mock-data/translations/en/product.ts
// your custom translations for the chunk product
export const product = {
// your custom translations for the chunk product
// you can add any nested objects here, they get deep merged with the existing default translations
productSummary: {
id: 'ID (custom)',
},
};
Create a file
src/mock-server/mock-data/translations/en/index.ts
with the following content
// src/mock-server/mock-data/translations/en/index.ts
import { mergeDeep } from '@valantic/spartacus-mock';
import { translations } from '@spartacus/assets';
import { product } from './product';
export const en = {
...mergeDeep(translations['en'], { product }),
};
Add the enhanced english chunks to the translations file
import { en } from './en';
export const translationResources = {
en,
};
Add custom chunks for language EN
Open the file
src/mock-server/mock-data/translations/translations.ts
Create a folder
en
within thetranslations
folderCreate a file
src/mock-server/mock-data/translations/en/my-custom-chunk.ts
with the following content
// src/mock-server/mock-data/translations/en/product.ts
// your custom translations for the chunk myCustomChunk
export const myCustomChunk = {
myCustomChunk: {
foo: 'bar',
},
};
Create a file
src/mock-server/mock-data/translations/en/index.ts
with the following content
// src/mock-server/mock-data/translations/en/index.ts
import { translations } from '@spartacus/assets';
import { myCustomChunk } from './my-custom-chunk';
export const en = {
...translations['en'],
myCustomChunk,
};
Add the enhanced english chunks to the translations file
import { en } from './en';
export const translationResources = {
en,
};
Use your new translation in the template:
{{'myCustomChunk.foo' | cxTranslate}}
Pro tip: Do not forget to add the new chunk to the translation chunk config of the spartacus configuration module.
Add translations for other languages
Open the file
src/mock-server/mock-data/translations/translations.ts
and add ade
property
// src/mock-server/mock-data/translations/translations.ts
import { TranslationResources } from '@spartacus/core';
import { de } from './de';
export const translationResources: TranslationResources = {
en: translations['en'],
de,
};
Create a folder
de
within thetranslations
folderCreate a file
src/mock-server/mock-data/translations/de/index.ts
with the following content
// src/mock-server/mock-data/translations/de/index.ts
// your custom chunks that you want to override
import { product } from './product';
export const de = {
product,
};
Create a file
src/mock-server/mock-data/translations/de/product.ts
with the following content
// src/mock-server/mock-data/translations/de/product.ts
// your german translations for the chunk product
export const product = {
// your german translations for the chunk product
};
Append your translations to your mockConfig in your
main.ts
file
// src/main.ts
// add your translation overrides
import { translationResources } from './mock-server/mock-data/translations/translations';
const mockConfig: MockConfig = {
// ...
translations: translationResources,
// ...
};
Pro tip: If you want to have full control over the translations, you can also provide a custom handler for the translation endpoint as described in "Add Handlers for Endpoints"
Last updated