Skip to content

Commit 6d7c9d2

Browse files
I18next should use default lang (#943)
This fixes the logic for the language system, so it uses the default language when no language match is found in the URL. It is needed when the default language should not be in the URL. It does this by adding a new, custom detector that always returns the default language, from the config file. This is put behind the URL matching, so it will only be used when no language matches.
1 parent af95340 commit 6d7c9d2

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

packages/pxweb2/src/i18n/config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,14 @@ import HttpApi from 'i18next-http-backend';
44
import LanguageDetector from 'i18next-browser-languagedetector';
55

66
import { pxNumber } from './formatters';
7+
import pxDetector from './pxDetector';
78
import { getConfig } from '../app/util/config/getConfig';
89

10+
// Add custom language detector to handle default language from config,
11+
// so we can handle no language in path as default language.
12+
const languageDetector = new LanguageDetector();
13+
languageDetector.addDetector(pxDetector);
14+
915
export const defaultNS = 'translation';
1016
const config = getConfig();
1117

@@ -19,7 +25,7 @@ const lookingForLanguagePos =
1925
const initPromise = i18n
2026
.use(HttpApi)
2127
.use(initReactI18next)
22-
.use(LanguageDetector)
28+
.use(languageDetector)
2329
.init({
2430
backend: {
2531
loadPath: `${config.baseApplicationPath}locales/{{lng}}/translation.json`,
@@ -34,7 +40,7 @@ const initPromise = i18n
3440
escapeValue: false,
3541
},
3642
detection: {
37-
order: ['path'],
43+
order: ['path', 'pxDetector'],
3844
lookupFromPathIndex: lookingForLanguagePos,
3945
caches: [], // Do not cache the language in local storage or cookies.
4046
},
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pxDetector from './pxDetector';
2+
import { getConfig } from '../app/util/config/getConfig';
3+
4+
const config = getConfig();
5+
6+
describe('pxDetector', () => {
7+
it('should return the default language from config', () => {
8+
const result = pxDetector.lookup();
9+
10+
expect(result).toBe(config.language.defaultLanguage);
11+
});
12+
13+
it('should have the correct name', () => {
14+
expect(pxDetector.name).toBe('pxDetector');
15+
});
16+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { getConfig } from '../app/util/config/getConfig';
2+
3+
const config = getConfig();
4+
5+
/**
6+
* Custom i18next language detector to always return the default language from config.
7+
* This is used to handle the case where there is no language in the path,
8+
* and we want to use the default language from config in that case.
9+
*/
10+
export default {
11+
name: 'pxDetector',
12+
13+
lookup() {
14+
return config.language.defaultLanguage;
15+
},
16+
};

0 commit comments

Comments
 (0)