Skip to content

Commit af3c3ff

Browse files
clydindgp1130
authored andcommitted
fix(@schematics/angular): improve i18n baseHref migration support for direct localize usage
(cherry picked from commit 97c0d57)
1 parent eab5873 commit af3c3ff

File tree

2 files changed

+11
-17
lines changed

2 files changed

+11
-17
lines changed

packages/schematics/angular/migrations/update-9/update-workspace-config.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,6 @@ function addProjectI18NOptions(
7070
return;
7171
}
7272

73-
const mainOptions = findPropertyInAstObject(browserConfig, 'options');
74-
const mainBaseHref =
75-
mainOptions &&
76-
mainOptions.kind === 'object' &&
77-
findPropertyInAstObject(mainOptions, 'baseHref');
78-
const hasMainBaseHref =
79-
!!mainBaseHref && mainBaseHref.kind === 'string' && mainBaseHref.value !== '/';
80-
8173
// browser builder options
8274
let locales: Record<string, string | { translation: string; baseHref: string }> | undefined;
8375
const options = getAllOptions(browserConfig);
@@ -97,9 +89,7 @@ function addProjectI18NOptions(
9789

9890
const baseHref = findPropertyInAstObject(option, 'baseHref');
9991
let baseHrefValue;
100-
// if the main options has a non-default base href, the i18n configuration
101-
// for the locale baseHref is disabled to more obviously mimic existing behavior
102-
if (baseHref && !hasMainBaseHref) {
92+
if (baseHref) {
10393
if (baseHref.kind === 'string' && baseHref.value !== `/${localIdValue}/`) {
10494
baseHrefValue = baseHref.value;
10595
}
@@ -187,11 +177,15 @@ function addBuilderI18NOptions(recorder: UpdateRecorder, builderConfig: JsonAstO
187177
}
188178

189179
// localize base HREF values are controlled by the i18n configuration
190-
// except if the main options has a non-default base href, the i18n configuration
191-
// for the locale baseHref is disabled in that case to more obviously mimic existing behavior
192180
const baseHref = findPropertyInAstObject(option, 'baseHref');
193-
if (localeId && i18nFile && baseHref && !hasMainBaseHref) {
181+
if (localeId && i18nFile && baseHref) {
194182
removePropertyInAstObject(recorder, option, 'baseHref');
183+
184+
// if the main option set has a non-default base href,
185+
// ensure that the augmented base href has the correct base value
186+
if (hasMainBaseHref) {
187+
insertPropertyInAstObjectInOrder(recorder, option, 'baseHref', '/', 12);
188+
}
195189
}
196190
}
197191
}

packages/schematics/angular/migrations/update-9/update-workspace-config_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ describe('Migration to version 9', () => {
383383
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
384384
config = getWorkspaceTargets(tree2).build;
385385
expect(config.options.baseHref).toBe('/my-app/');
386-
expect(config.configurations.de.baseHref).toBe('/de/');
386+
expect(config.configurations.de.baseHref).toBe('/');
387387
});
388388

389389
it('should remove deprecated extract-i18n options', async () => {
@@ -454,8 +454,8 @@ describe('Migration to version 9', () => {
454454
const projectConfig = JSON.parse(tree2.readContent(workspacePath)).projects['migration-test'];
455455
expect(projectConfig.i18n.sourceLocale).toBeUndefined();
456456
expect(projectConfig.i18n.locales).toEqual({
457-
de: { translation: 'src/locale/messages.de.xlf', baseHref: '' },
458-
fr: { translation: 'src/locale/messages.fr.xlf', baseHref: '' },
457+
de: { translation: 'src/locale/messages.de.xlf', baseHref: '/abc/' },
458+
fr: 'src/locale/messages.fr.xlf',
459459
});
460460
});
461461
});

0 commit comments

Comments
 (0)