Skip to content

Commit 791f574

Browse files
clydinalan-agius4
authored andcommitted
fix(@angular-devkit/build-angular): set HTML lang attribute when serving
When using the non-deprecated localization options, the development server was not properly setting the HTML `lang` attribute for the application. This change ensures that the active locale is used within the application's index HTML file. Closes #18094 (cherry picked from commit 58a7dea)
1 parent 970c3cc commit 791f574

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

packages/angular_devkit/build_angular/src/dev-server/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ export function serveWebpackBrowser(
9595
webpackDevServerConfig: WebpackDevServer.Configuration;
9696
port: number;
9797
projectRoot: string;
98+
locale: string | undefined;
9899
}> {
99100
// Get the browser configuration from the target name.
100101
const rawBrowserOptions = await context.getTargetOptions(browserTarget);
@@ -156,11 +157,13 @@ export function serveWebpackBrowser(
156157
webpackDevServerConfig,
157158
port,
158159
projectRoot,
160+
locale:
161+
browserOptions.i18nLocale || (i18n.shouldInline ? [...i18n.inlineLocales][0] : undefined),
159162
};
160163
}
161164

162165
return from(setup()).pipe(
163-
switchMap(({ browserOptions, webpackConfig, webpackDevServerConfig, port, projectRoot }) => {
166+
switchMap(({ browserOptions, webpackConfig, webpackDevServerConfig, port, projectRoot, locale }) => {
164167
options.port = port;
165168

166169
// Resolve public host and client address.
@@ -219,7 +222,7 @@ export function serveWebpackBrowser(
219222
noModuleEntrypoints: ['polyfills-es5'],
220223
postTransform: transforms.indexHtml,
221224
crossOrigin: browserOptions.crossOrigin,
222-
lang: browserOptions.i18nLocale,
225+
lang: locale,
223226
}),
224227
);
225228
}

packages/angular_devkit/build_angular/src/dev-server/index_spec.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,42 @@ describe('Dev Server Builder index', () => {
104104
);
105105
await run.stop();
106106
});
107+
108+
it('sets HTML lang attribute with the active locale', async () => {
109+
const locale = 'fr';
110+
const { workspace } = await workspaces.readWorkspace(host.root(), workspaces.createWorkspaceHost(host));
111+
const app = workspace.projects.get('app');
112+
if (!app) {
113+
fail('Test application "app" not found.');
114+
115+
return;
116+
}
117+
118+
app.extensions['i18n'] = {
119+
locales: {
120+
[locale]: [],
121+
},
122+
};
123+
124+
const target = app.targets.get('build');
125+
if (!target) {
126+
fail('Test application "app" target "build" not found.');
127+
128+
return;
129+
}
130+
if (!target.options) {
131+
target.options = {};
132+
}
133+
target.options.localize = [locale];
134+
135+
await workspaces.writeWorkspace(workspace, workspaces.createWorkspaceHost(host));
136+
137+
const architect = (await createArchitect(host.root())).architect;
138+
const run = await architect.scheduleTarget(targetSpec);
139+
const output = (await run.result) as DevServerBuilderOutput;
140+
expect(output.success).toBe(true);
141+
const response = await fetch('http://localhost:4200/index.html');
142+
expect(await response.text()).toContain(`lang="${locale}"`);
143+
await run.stop();
144+
});
107145
});

0 commit comments

Comments
 (0)