Skip to content

Commit af95340

Browse files
Fix/LanguageSwitcher uses wrong language for checks (#942)
This fixes a bug where the LanguageSwitcher was using the fallback language in the config, when it should have used the default language. We did not find this bug sooner, since we have always ran the web app with the same fallback and default languages.
1 parent 628fe73 commit af95340

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

packages/pxweb2/src/app/components/Footer/Footer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export const Footer: React.FC<FooterProps> = ({
115115
location.pathname,
116116
lang.shorthand,
117117
config.language.supportedLanguages,
118-
config.language.fallbackLanguage,
118+
config.language.defaultLanguage,
119119
config.language.showDefaultLanguageInPath,
120120
);
121121
const isCurrent = i18n.language?.startsWith(

packages/pxweb2/src/app/components/Header/Header.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const Header = ({ stroke = false }: HeaderProps) => {
3636
'/',
3737
i18n.language,
3838
config.language.supportedLanguages,
39-
config.language.fallbackLanguage,
39+
config.language.defaultLanguage,
4040
config.language.showDefaultLanguageInPath,
4141
)}
4242
>

packages/pxweb2/src/app/components/LanguageSwitcher/LanguageSwitcher.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const LanguageSwitcher = () => {
3535
location.pathname,
3636
event.target.value,
3737
config.language.supportedLanguages,
38-
config.language.fallbackLanguage,
38+
config.language.defaultLanguage,
3939
config.language.showDefaultLanguageInPath,
4040
),
4141
);

packages/pxweb2/src/app/util/language/getLanguagePath.spec.tsx

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,63 @@ describe('getLanguagePath', () => {
66
{ languageName: 'English', shorthand: 'en' },
77
{ languageName: 'Polish', shorthand: 'pl' },
88
];
9-
const fallbackLanguage = 'en';
9+
const defaultLanguage = 'en';
1010

1111
describe('when showDefaultLanguageInPath is false', () => {
12-
it('should return the correct path when pathname starts with a language code and target is fallback', () => {
12+
it('should return the correct path when pathname starts with a language code and target is default', () => {
1313
const pathname = '/no/some/path';
1414
const targetLanguage = 'en';
1515

1616
const result = getLanguagePath(
1717
pathname,
1818
targetLanguage,
1919
supportedLanguages,
20-
fallbackLanguage,
20+
defaultLanguage,
2121
false,
2222
);
2323

2424
expect(result).toBe('/some/path');
2525
});
2626

27-
it('should return the correct path when pathname starts with a language code and target is not fallback', () => {
27+
it('should return the correct path when pathname starts with a language code and target is not default', () => {
2828
const pathname = '/en/some/path';
2929
const targetLanguage = 'no';
3030

3131
const result = getLanguagePath(
3232
pathname,
3333
targetLanguage,
3434
supportedLanguages,
35-
fallbackLanguage,
35+
defaultLanguage,
3636
false,
3737
);
3838

3939
expect(result).toBe('/no/some/path');
4040
});
4141

42-
it('should return the correct path when pathname does not start with a language code and target is fallback', () => {
42+
it('should return the correct path when pathname does not start with a language code and target is default', () => {
4343
const pathname = '/some/path';
4444
const targetLanguage = 'en';
4545

4646
const result = getLanguagePath(
4747
pathname,
4848
targetLanguage,
4949
supportedLanguages,
50-
fallbackLanguage,
50+
defaultLanguage,
5151
false,
5252
);
5353

5454
expect(result).toBe('/some/path');
5555
});
5656

57-
it('should return the correct path when pathname does not start with a language code and target is not fallback', () => {
57+
it('should return the correct path when pathname does not start with a language code and target is not default', () => {
5858
const pathname = '/some/path';
5959
const targetLanguage = 'no';
6060

6161
const result = getLanguagePath(
6262
pathname,
6363
targetLanguage,
6464
supportedLanguages,
65-
fallbackLanguage,
65+
defaultLanguage,
6666
false,
6767
);
6868

@@ -77,7 +77,7 @@ describe('getLanguagePath', () => {
7777
pathname,
7878
targetLanguage,
7979
supportedLanguages,
80-
fallbackLanguage,
80+
defaultLanguage,
8181
false,
8282
);
8383

@@ -92,7 +92,7 @@ describe('getLanguagePath', () => {
9292
pathname,
9393
targetLanguage,
9494
supportedLanguages,
95-
fallbackLanguage,
95+
defaultLanguage,
9696
false,
9797
);
9898

@@ -101,60 +101,60 @@ describe('getLanguagePath', () => {
101101
});
102102

103103
describe('when showDefaultLanguageInPath is true', () => {
104-
it('should include fallback language in path when target is fallback', () => {
104+
it('should include default language in path when target is default', () => {
105105
const pathname = '/no/some/path';
106106
const targetLanguage = 'en';
107107

108108
const result = getLanguagePath(
109109
pathname,
110110
targetLanguage,
111111
supportedLanguages,
112-
fallbackLanguage,
112+
defaultLanguage,
113113
true,
114114
);
115115

116116
expect(result).toBe('/en/some/path');
117117
});
118118

119-
it('should include fallback language when pathname does not start with language code and target is fallback', () => {
119+
it('should include default language when pathname does not start with language code and target is default', () => {
120120
const pathname = '/some/path';
121121
const targetLanguage = 'en';
122122

123123
const result = getLanguagePath(
124124
pathname,
125125
targetLanguage,
126126
supportedLanguages,
127-
fallbackLanguage,
127+
defaultLanguage,
128128
true,
129129
);
130130

131131
expect(result).toBe('/en/some/path');
132132
});
133133

134-
it('should handle empty path correctly with fallback language', () => {
134+
it('should handle empty path correctly with default language', () => {
135135
const pathname = '/';
136136
const targetLanguage = 'en';
137137

138138
const result = getLanguagePath(
139139
pathname,
140140
targetLanguage,
141141
supportedLanguages,
142-
fallbackLanguage,
142+
defaultLanguage,
143143
true,
144144
);
145145

146146
expect(result).toBe('/en/');
147147
});
148148

149-
it('should handle non-fallback language the same regardless of showDefaultLanguageInPath setting', () => {
149+
it('should handle non-default language the same regardless of showDefaultLanguageInPath setting', () => {
150150
const pathname = '/some/path';
151151
const targetLanguage = 'no';
152152

153153
const result = getLanguagePath(
154154
pathname,
155155
targetLanguage,
156156
supportedLanguages,
157-
fallbackLanguage,
157+
defaultLanguage,
158158
true,
159159
);
160160

packages/pxweb2/src/app/util/language/getLanguagePath.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { Config as LanguageConfig } from '../../util/config/configType';
55
* @param pathname Current pathname from location
66
* @param targetLanguage The language to switch to
77
* @param supportedLanguages List of supported languages
8-
* @param fallbackLanguage The fallback language code
8+
* @param defaultLanguage The fallback language code
99
* @param showDefaultLanguageInPath Whether to show the default language in the path
1010
* @returns The correct path for the target language
1111
*/
1212
export const getLanguagePath = (
1313
pathname: string,
1414
targetLanguage: string,
1515
supportedLanguages: LanguageConfig['language']['supportedLanguages'],
16-
fallbackLanguage: string,
16+
defaultLanguage: string,
1717
showDefaultLanguageInPath: boolean,
1818
): string => {
1919
const [firstURLElement, ...pathParts] = pathname.slice(1).split('/');
@@ -25,7 +25,7 @@ export const getLanguagePath = (
2525
? pathParts.join('/')
2626
: [firstURLElement, ...pathParts].join('/');
2727

28-
if (!showDefaultLanguageInPath && targetLanguage === fallbackLanguage) {
28+
if (!showDefaultLanguageInPath && targetLanguage === defaultLanguage) {
2929
return `/${actualPath}`;
3030
}
3131

0 commit comments

Comments
 (0)