Skip to content

Commit

Permalink
Fix locale link relying on deprecated initialLanguage prop
Browse files Browse the repository at this point in the history
  • Loading branch information
pwolfert authored and forrestbaer committed Jun 27, 2022
1 parent 037f6fc commit 774ca60
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import defaultMenuLinks, {
defaultMenuLinks as namedExportDefaultMenuLinks,
} from './defaultMenuLinks';
import { setLanguage } from '@cmsgov/design-system';

describe('MenuList', function () {
it('includes a named export', () => {
Expand Down Expand Up @@ -51,7 +52,12 @@ describe('MenuList', function () {

describe('Spanish', () => {
it('returns array of menu list objects', () => {
expect(defaultMenuLinks({ locale: 'es' })).toMatchSnapshot();
// Make sure you can specify the language through the deprecated `locale` prop or by the global setting
const linksA = defaultMenuLinks({ locale: 'es' });
setLanguage('es');
const linksB = defaultMenuLinks();
expect(linksA).toEqual(linksB);
expect(linksB).toMatchSnapshot();
});

it('returns array of menu list objects with subpath', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Link, VARIATION_NAMES } from './Header';
import localeLink from './localeLink';
import loginLink from './loginLink';
import { Language, languageMatches, tWithLanguage } from '../i18n';
import { Language, getLanguage, languageMatches, tWithLanguage } from '../i18n';

export enum LinkIdentifier {
LOGIN = 'login',
Expand Down Expand Up @@ -73,7 +73,7 @@ export function defaultMenuLinks(options: DefaultMenuLinkOptions = {}) {
}

if (!hideLanguageSwitch) {
const locLink = localeLink(t, locale, subpath, switchLocaleLink);
const locLink = localeLink(t, locale ?? getLanguage(), subpath, switchLocaleLink);
loggedOut.push(locLink);
loggedIn.push(locLink);
}
Expand Down
13 changes: 6 additions & 7 deletions packages/ds-healthcare-gov/src/components/Header/localeLink.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Language, TFunction } from '@cmsgov/design-system';
import { Language, TFunction, languageMatches } from '@cmsgov/design-system';

/**
* Returns a link pointing to the opposite locale
Expand All @@ -9,12 +9,11 @@ export default function localeLink(
subpath = '',
switchLocaleLink?: string
) {
const defaultLocaleLink =
locale === 'es'
? `https://www.healthcare.gov/${subpath}`
: `https://www.cuidadodesalud.gov/es/${subpath}`;
const defaultLocaleLink = languageMatches(locale, 'es')
? `https://www.healthcare.gov/${subpath}`
: `https://www.cuidadodesalud.gov/es/${subpath}`;
return {
label: locale === 'es' ? t('header.english') : t('header.español'),
href: switchLocaleLink || defaultLocaleLink,
label: languageMatches(locale, 'es') ? t('header.english') : t('header.español'),
href: switchLocaleLink ?? defaultLocaleLink,
};
}

0 comments on commit 774ca60

Please sign in to comment.