Skip to content

Commit d0f8319

Browse files
Andrea GriffithsCopilot
andcommitted
Emit BCP 47 hreflang tags for new locales
The hreflang attribute requires BCP 47 language tags, but the existing template was emitting the raw locale key (e.g. 'zh_hans') as the hreflang value. Underscores aren't valid in BCP 47, so the new locales would have produced non-conformant alternate links that search engines ignore. Adds an optional 'hreflang' key to each locale's data file, with the template falling back to the locale key when the key is absent. That keeps every existing locale's output unchanged (single-subtag codes like 'es', 'pt', 'de' are already valid BCP 47) and lets zh_hans and zh_hant emit zh-Hans and zh-Hant. Addresses Copilot review feedback on PR #1333. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 4a6c6f2 commit d0f8319

3 files changed

Lines changed: 5 additions & 1 deletion

File tree

_data/locales/zh_hans.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
zh_hans:
22
locale_name: 简体中文
3+
hreflang: zh-Hans
34
OpenSourceFriday: "开源星期五"
45
full_description: "众人拾柴火焰高。这个星期五,为你在用的、喜欢的项目做出贡献。"
56
Contributors: "贡献者"

_data/locales/zh_hant.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
zh_hant:
22
locale_name: 繁體中文
3+
hreflang: zh-Hant
34
OpenSourceFriday: "開源星期五"
45
full_description: "眾人拾柴火焰高。這個星期五,為你在用的、喜歡的項目做出貢獻。"
56
Contributors: "貢獻者"

_includes/head.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
{% assign locales = site.data.locales | sort %}
1212
{% for locale in locales %}
1313
{% assign lang = locale[0] %}
14+
{% assign lang_data = locale[1][lang] %}
15+
{% assign hreflang_tag = lang_data.hreflang | default: lang %}
1416
{% assign page_lang_slash = page.lang | append: '/' | prepend: '/' %}
1517
{% assign default_url = page.url | replace: page_lang_slash, '/' %}
1618
{% if lang == "en" %}
1719
<link rel="alternate" hreflang="en" href="{{ site.url }}{{ default_url }}" />
1820
<link rel="alternate" hreflang="x-default" href="{{ site.url }}{{ default_url }}" />
1921
{% else %}
20-
<link rel="alternate" hreflang="{{ lang }}" href="{{ site.url }}/{{ lang }}{{ default_url }}" />
22+
<link rel="alternate" hreflang="{{ hreflang_tag }}" href="{{ site.url }}/{{ lang }}{{ default_url }}" />
2123
{% endif %}
2224
{% endfor %}
2325
{% endif %}

0 commit comments

Comments
 (0)