Skip to content

Commit

Permalink
fix(@schematics/angular): improve i18n baseHref migration support for…
Browse files Browse the repository at this point in the history
… direct localize usage

(cherry picked from commit 97c0d57)
  • Loading branch information
clydin authored and dgp1130 committed Dec 4, 2019
1 parent eab5873 commit af3c3ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,6 @@ function addProjectI18NOptions(
return;
}

const mainOptions = findPropertyInAstObject(browserConfig, 'options');
const mainBaseHref =
mainOptions &&
mainOptions.kind === 'object' &&
findPropertyInAstObject(mainOptions, 'baseHref');
const hasMainBaseHref =
!!mainBaseHref && mainBaseHref.kind === 'string' && mainBaseHref.value !== '/';

// browser builder options
let locales: Record<string, string | { translation: string; baseHref: string }> | undefined;
const options = getAllOptions(browserConfig);
Expand All @@ -97,9 +89,7 @@ function addProjectI18NOptions(

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

// localize base HREF values are controlled by the i18n configuration
// except if the main options has a non-default base href, the i18n configuration
// for the locale baseHref is disabled in that case to more obviously mimic existing behavior
const baseHref = findPropertyInAstObject(option, 'baseHref');
if (localeId && i18nFile && baseHref && !hasMainBaseHref) {
if (localeId && i18nFile && baseHref) {
removePropertyInAstObject(recorder, option, 'baseHref');

// if the main option set has a non-default base href,
// ensure that the augmented base href has the correct base value
if (hasMainBaseHref) {
insertPropertyInAstObjectInOrder(recorder, option, 'baseHref', '/', 12);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe('Migration to version 9', () => {
const tree2 = await schematicRunner.runSchematicAsync('workspace-version-9', {}, tree.branch()).toPromise();
config = getWorkspaceTargets(tree2).build;
expect(config.options.baseHref).toBe('/my-app/');
expect(config.configurations.de.baseHref).toBe('/de/');
expect(config.configurations.de.baseHref).toBe('/');
});

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

0 comments on commit af3c3ff

Please sign in to comment.