Skip to content

Commit

Permalink
feat: add lang attribute to html tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Leksat committed Sep 25, 2023
1 parent 9bb07cc commit 90fe39b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
27 changes: 27 additions & 0 deletions apps/website/gatsby-ssr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import './styles.css';

import { Locale } from '@custom/schema';
import { GatsbySSR } from 'gatsby';

export const onRenderBody: GatsbySSR['onRenderBody'] = ({
setHtmlAttributes,
pathname,
}) => {
const locales = Object.values(Locale);
if (locales.length === 1) {
// Single-language project.
setHtmlAttributes({
lang: locales[0],
});
} else {
// Multi-language project.
const prefix = pathname.split('/')[1];
if (locales.includes(prefix as Locale)) {
setHtmlAttributes({
lang: prefix,
});
} else {
// We don't know the language.
}
}
};
12 changes: 12 additions & 0 deletions tests/e2e/specs/metatags.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,15 @@ test('Metatags on Basic page', async ({ page }) => {
pageUrl,
);
});

test('HTML lang attribute', async ({ page }) => {
await page.goto(websiteUrl('/en'));
await expect(page.locator('html')).toHaveAttribute('lang', 'en');
await page.goto(websiteUrl('/de'));
await expect(page.locator('html')).toHaveAttribute('lang', 'de');

await page.goto(websiteUrl('/en/imprint'));
await expect(page.locator('html')).toHaveAttribute('lang', 'en');
await page.goto(websiteUrl('/de/impressum'));
await expect(page.locator('html')).toHaveAttribute('lang', 'de');
});

0 comments on commit 90fe39b

Please sign in to comment.