Skip to content

Commit

Permalink
feat: allow for tree-shaking unused langauges translations in custom …
Browse files Browse the repository at this point in the history
…apps (#19799)

- add a lot of missing `index.ts` files to re-export JSON translations in TS for new langauges
- update `translations.ts` barrel files, to export each langauge in a separate public API members (to allow for treeshaking unused langauges). Note: it preservers the old feature prefix (e.g. `asmTranslations` -> `asmTranslationsEn`, `asmTranslationsDe`, ...)
- deprecate the the old wrapper consts e.g. `asmTranslations` (and moved those deprecated tokens to `public_api.ts` files from `translations.ts` files)
- remove from the old wrapper consts e.g. `asmTranslations` langauges other than `en` (in other words: revert the change made previously on the branch [epic/tew-translation](https://github.com/SAP/spartacus/tree/epic/tew-translation) which added 3 other langauges into those wrapper objects)
- add a new function `extractTranslationChunksConfig()` in `@spartacus/core` and use it in every library for creating chunks config in their `translation.ts` files. It extracts the chunks config from the translations assets automatically. So no more need to type chunks config manually.
- fix our installation schematics, to use the new recommended syntax in the scaffolded features e.g.:
   ```diff
   - resources: asmTranslations,
   + resources: { en: asmTranslationsEn }
   ```
   
By the way:
  - noticed a broken chunks mapping for the cpqQuote feature. now should be fixed since using function `extractTranslationChunksConfig()`
  - noticed a typo in the existing old chunk name `myAccountV2NotifiationPerference`, HOWEVER I didn't fix it in this PR - it should be done in a separate PR
  - noticed yet another typo in existing file name `my-account-v2-notification-perference.json` (btw. it's a different typo than the above chunk name), HOWEVER I didnt fix it in this PR
  - noticed some i18n JSON files don't use camelCase, but kebab-case, HOWEVER I didn't fix it in this PR - it should be done in a separate PR
  
The reason I didn't want to fix those things by the way is:
- it might affect the workflow of the TEW team, and I'd need to first double check with them
- it's not related to this PR's main purpose (three-shaking)

TODO
- [x] fix unit tests and Jest snapshots in schematics
- [x] use it this way also in our storefrontapp
- [ ] write doc input for the migration docs (to use specific langauges)


related to https://jira.tools.sap/browse/CXSPA-9131

**QA steps**
1. checkout to this branch
2. build libs
3. publish to verdaccio
4. create a new app `npx @angular/cli@17 new test-app --standalone=false --ssr=false`
6. in the new app, install sparatcus from verdaccio `ng add @spartacus/[email protected]. 0-1 --baseUrl="https://40.76.109. 9:9002" --ssr` AND PLEASE SELECT ALL possible features and integrations in the interactive prompt
7. In VsCode search everywhere (Cmd+Shift+F) for the phrase `resources:` and verify that everywhere in every feature module we use the new approach with explicit EN translations
8. In some of those modules try to change the approach to old `translations` wrapper object and verify it's `@deprecated` (crossed out in VSCode)
9. in spartacus-configuration.module.ts add also German translations for the main translations:
   ```ts
   i18n: {
        resources: { en: translationsEn, de: translationsDe },
   ```
10. in spartacus-configuration.module.ts add this config (it's needed for later to run `http-server`)
   ```ts
   provideConfig(<SiteContextConfig>{
      context: {
        baseSite: ['electronics-spa', 'apparel-uk-spa'],
        currency: ['USD', 'GBP'],
        language: ['en', 'de'],
      },
    }),
   ```
11. `ng build` (or for interactive mode run `npm run watch`). Don't try `ng serve --configuration=production` - it doesn't have so well tree shaking as `ng build`
12. `npx http-server dist/test-app/browser` (or for interactive mode run in another terminal `npx nodemon --watch "./dist/test-app/browser" --exec "npx http-server './dist/$test-app/browser'" --ext "*"`
13. In Chrome open http://127.0.0.1:8080/ (8080! not 4200 port!)
14. In devtools Sources tab do the search (Cmd+F)
15. paste this search phrase `ariaLabelSuggestions` and verify that in the loaded `main.js` file there are only 2 langauges (EN and DE which you have enabled in steps above) with translations for this key
    <img width="512" alt="image" src="https://github.com/user-attachments/assets/f6be33e1-7cc7-4e2a-bde3-ecdf4a19db81" />
  • Loading branch information
Platonn authored Jan 9, 2025
1 parent d0a616b commit 21f21c4
Show file tree
Hide file tree
Showing 861 changed files with 8,832 additions and 1,390 deletions.
7 changes: 4 additions & 3 deletions .github/api-extractor-action/src/api-extractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ async function copyApiExtractorConfig(targetDir: string): Promise<void> {
*
* @returns object with name and the newName
*/
function updateNameInPackageJson(
path: string
): { name: string; newName: string } {
function updateNameInPackageJson(path: string): {
name: string;
newName: string;
} {
const packageContent = JSON.parse(fs.readFileSync(path, 'utf-8'));
const name: string = packageContent.name;
const newName = name.replace(/\//g, '_').replace(/\_/, '/');
Expand Down
16 changes: 16 additions & 0 deletions feature-libs/asm/assets/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { asmTranslationsEn } from './translations/translations';

export * from './translations/translations';

/**
* @deprecated Please use **specific language** translations (suffixed with language code) instead,
* like in the following example:
* ```diff
* i18n: {
* - resources: asmTranslations
* + resources: { en: asmTranslationsEn }
* }
* ```
*/
export const asmTranslations = {
en: asmTranslationsEn,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/cs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const cs = {
asm,
};
1 change: 0 additions & 1 deletion feature-libs/asm/assets/translations/de/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import asm from './asm.json';

export const de = {
asm,
};
1 change: 0 additions & 1 deletion feature-libs/asm/assets/translations/en/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import asm from './asm.json';

export const en = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/es/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const es = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/es_CO/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const es_CO = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/fr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const fr = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/hi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const hi = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/hu/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const hu = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/id/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const id = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/it/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const it = {
asm,
};
1 change: 0 additions & 1 deletion feature-libs/asm/assets/translations/ja/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import asm from './asm.json';

export const ja = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/ko/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const ko = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/pl/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const pl = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/pt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const pt = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/ru/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const ru = {
asm,
};
33 changes: 19 additions & 14 deletions feature-libs/asm/assets/translations/translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { TranslationChunksConfig, TranslationResources } from '@spartacus/core';
import { en } from './en/index';
import { ja } from './ja/index';
import { de } from './de/index';
import { zh } from './zh/index';
import { extractTranslationChunksConfig } from '@spartacus/core';
export const asmTranslationChunksConfig = extractTranslationChunksConfig(en);

export const asmTranslations: TranslationResources = {
en,
ja,
de,
zh,
};

export const asmTranslationChunksConfig: TranslationChunksConfig = {
asm: ['asm'],
};
export { cs as asmTranslationsCs } from './cs/index';
export { de as asmTranslationsDe } from './de/index';
export { en as asmTranslationsEn } from './en/index';
export { es as asmTranslationsEs } from './es/index';
export { es_CO as asmTranslationsEs_CO } from './es_CO/index';
export { fr as asmTranslationsFr } from './fr/index';
export { hi as asmTranslationsHi } from './hi/index';
export { hu as asmTranslationsHu } from './hu/index';
export { id as asmTranslationsId } from './id/index';
export { it as asmTranslationsIt } from './it/index';
export { ja as asmTranslationsJa } from './ja/index';
export { ko as asmTranslationsKo } from './ko/index';
export { pl as asmTranslationsPl } from './pl/index';
export { pt as asmTranslationsPt } from './pt/index';
export { ru as asmTranslationsRu } from './ru/index';
export { zh as asmTranslationsZh } from './zh/index';
export { zh_TW as asmTranslationsZh_TW } from './zh_TW/index';
1 change: 0 additions & 1 deletion feature-libs/asm/assets/translations/zh/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import asm from './asm.json';

export const zh = {
asm,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/assets/translations/zh_TW/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asm from './asm.json';
export const zh_TW = {
asm,
};
16 changes: 16 additions & 0 deletions feature-libs/asm/customer-360/assets/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { asmCustomer360TranslationsEn } from './translations/translations';

export * from './translations/translations';

/**
* @deprecated Please use **specific language** translations (suffixed with language code) instead,
* like in the following example:
* ```diff
* i18n: {
* - resources: asmCustomer360Translations
* + resources: { en: asmCustomer360TranslationsEn }
* }
* ```
*/
export const asmCustomer360Translations = {
en: asmCustomer360TranslationsEn,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/cs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const cs = {
asmCustomer360,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import asmCustomer360 from './asmCustomer360.json';

export const de = {
asmCustomer360,
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import asmCustomer360 from './asmCustomer360.json';

export const en = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/es/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const es = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/es_CO/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const es_CO = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/fr/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const fr = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/hi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const hi = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/hu/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const hu = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/id/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const id = {
asmCustomer360,
};
11 changes: 11 additions & 0 deletions feature-libs/asm/customer-360/assets/translations/it/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* SPDX-FileCopyrightText: 2024 SAP Spartacus team <[email protected]>
* SPDX-FileCopyrightText: 2025 SAP Spartacus team <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/

import asmCustomer360 from './asmCustomer360.json';
export const it = {
asmCustomer360,
};
Loading

0 comments on commit 21f21c4

Please sign in to comment.