fix(portal): emit valid BCP 47 html lang attribute (#14680)
Locales are stored with underscores (e.g. pt_BR), but the HTML lang attribute requires hyphens (pt-BR). Convert via a helper in the public portal layouts. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
This commit is contained in:
@@ -41,6 +41,10 @@ module PortalHelper
|
|||||||
language_map[locale] || locale
|
language_map[locale] || locale
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def html_lang_attribute(locale)
|
||||||
|
locale.to_s.tr('_', '-')
|
||||||
|
end
|
||||||
|
|
||||||
def theme_query_string(theme)
|
def theme_query_string(theme)
|
||||||
theme.present? && theme != 'system' ? "?theme=#{theme}" : ''
|
theme.present? && theme != 'system' ? "?theme=#{theme}" : ''
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="<%= I18n.locale %>">
|
<html lang="<%= html_lang_attribute(I18n.locale) %>">
|
||||||
<head>
|
<head>
|
||||||
<%= render 'layouts/portal_head' %>
|
<%= render 'layouts/portal_head' %>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="<%= I18n.locale %>">
|
<html lang="<%= html_lang_attribute(I18n.locale) %>">
|
||||||
<head>
|
<head>
|
||||||
<%= render 'layouts/portal_head' %>
|
<%= render 'layouts/portal_head' %>
|
||||||
</head>
|
</head>
|
||||||
|
|||||||
@@ -1,6 +1,28 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe PortalHelper do
|
describe PortalHelper do
|
||||||
|
describe '#html_lang_attribute' do
|
||||||
|
it 'returns the locale unchanged when it has no region suffix' do
|
||||||
|
expect(helper.html_lang_attribute('en')).to eq('en')
|
||||||
|
expect(helper.html_lang_attribute(:fr)).to eq('fr')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'converts underscores to hyphens for BCP 47 compliance' do
|
||||||
|
expect(helper.html_lang_attribute('pt_BR')).to eq('pt-BR')
|
||||||
|
expect(helper.html_lang_attribute(:zh_CN)).to eq('zh-CN')
|
||||||
|
expect(helper.html_lang_attribute('zh_TW')).to eq('zh-TW')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'produces a valid BCP 47 lang attribute for every language shown in the UI' do
|
||||||
|
bcp47 = /\A[a-z]{2,3}(-[A-Z]{2})?\z/
|
||||||
|
|
||||||
|
LANGUAGES_CONFIG.each_value do |lang|
|
||||||
|
locale = lang[:iso_639_1_code]
|
||||||
|
expect(helper.html_lang_attribute(locale)).to match(bcp47)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe '#generate_portal_bg_color' do
|
describe '#generate_portal_bg_color' do
|
||||||
context 'when theme is dark' do
|
context 'when theme is dark' do
|
||||||
it 'returns the correct color mix with black' do
|
it 'returns the correct color mix with black' do
|
||||||
|
|||||||
Reference in New Issue
Block a user