Skip to content

Commit 6e79d08

Browse files
M0nicalex111
authored andcommitted
Add lang and hreflang to language pages for screen reader UX (#2033)
* Add lang and hreflang for screen reader UX I wanted to add `lang` and `hreflang` to the link element for translated languages. `Lang` is used by screen readers to switch language profiles to provide the correct accent and pronunciation. >If you have multiple versions of a page for different languages or regions, tell Google about these different variations. Doing so will help Google Search point users to the most appropriate version of your page by language or region. > Note that even without taking action, Google might still find alternate language versions of your page, but it is usually best for you to explicitly indicate your language- or region-specific pages. Source: https://support.google.com/webmasters/answer/189077?hl=en * add hrefLang and Lang attributes * add alternate pages to head * Apply suggestions from code review Co-Authored-By: Alexey Pyltsyn <[email protected]> * mark english hreflang as x-default * make english the default on the languages page * rethink default-x
1 parent 23b242e commit 6e79d08

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/components/TitleAndMetaTags/TitleAndMetaTags.js

+37
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import Helmet from 'react-helmet';
99
import React from 'react';
10+
import {urlRoot} from 'site-constants';
11+
// $FlowFixMe This is a valid path
12+
import languages from '../../../content/languages.yml';
1013

1114
const defaultDescription = 'A JavaScript library for building user interfaces';
1215

@@ -16,6 +19,32 @@ type Props = {
1619
canonicalUrl: string,
1720
};
1821

22+
// only provide alternate links to pages in languages where 95-100% of core content has been translated
23+
// which is determined by status enum of 2
24+
const completeLanguages = languages.filter(language => {
25+
return language.status == 2;
26+
});
27+
28+
const alternatePages = canonicalUrl => {
29+
return completeLanguages.map(language => (
30+
<link
31+
key={('alt-', language.code)}
32+
rel="alternate"
33+
hreflang={language.code}
34+
href={canonicalUrl.replace(
35+
urlRoot,
36+
`https://${
37+
language.code === 'en' ? '' : `${language.code}.`
38+
}reactjs.org`,
39+
)}
40+
/>
41+
));
42+
};
43+
44+
const defaultPage = canonicalUrl => {
45+
return canonicalUrl.replace(urlRoot, 'https://reactjs.org');
46+
};
47+
1948
const TitleAndMetaTags = ({title, ogDescription, canonicalUrl}: Props) => {
2049
return (
2150
<Helmet title={title}>
@@ -29,6 +58,14 @@ const TitleAndMetaTags = ({title, ogDescription, canonicalUrl}: Props) => {
2958
/>
3059
<meta property="fb:app_id" content="623268441017527" />
3160
{canonicalUrl && <link rel="canonical" href={canonicalUrl} />}
61+
{canonicalUrl && (
62+
<link
63+
rel="alternate"
64+
href={defaultPage(canonicalUrl)}
65+
hreflang="x-default"
66+
/>
67+
)}
68+
{canonicalUrl && alternatePages(canonicalUrl)}
3269
</Helmet>
3370
);
3471
};

src/pages/languages.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,11 @@ const Language = ({code, name, status, translatedName}) => {
140140
}}>
141141
{status === 0 && translatedName}
142142
{status > 0 && (
143-
<a href={`https://${prefix}reactjs.org/`} rel="nofollow">
143+
<a
144+
href={`https://${prefix}reactjs.org/`}
145+
rel="nofollow"
146+
lang={code}
147+
hrefLang={code}>
144148
{translatedName}
145149
</a>
146150
)}

0 commit comments

Comments
 (0)