Skip to content

Commit 133b230

Browse files
committed
feat(i18n): update getSupportedLocaleList function
1 parent d6d1cd7 commit 133b230

File tree

4 files changed

+33
-35
lines changed

4 files changed

+33
-35
lines changed

src/i18n/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export {
102102
getPrimaryLanguageSubtag,
103103
getLocale,
104104
getMessages,
105-
getSupportedLocales,
105+
getSupportedLocaleList as getSupportedLocalesList,
106106
isRtl,
107107
handleRtl,
108108
mergeMessages,

src/i18n/languageApi.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { convertKeyNames, snakeCaseObject } from '../utils';
55
/**
66
* Updates user language preferences via the preferences API.
77
*
8-
* This function gets the authenticated user, converts preference data to snake_case
8+
* This function gets the authenticated user, converts preference data to snake_case
99
* and formats specific keys according to backend requirements before sending the PATCH request.
1010
* If no user is authenticated, the function returns early without making the API call.
1111
*

src/i18n/lib.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,23 @@ export function getMessages(locale = getLocale()) {
187187
/**
188188
* Returns the list of supported locales based on the configured messages.
189189
* This list is dynamically generated from the translation messages that were
190-
* provided during i18n configuration.
190+
* provided during i18n configuration. Always includes the current locale.
191191
*
192192
* @throws An error if i18n has not yet been configured.
193193
* @returns {string[]} Array of supported locale codes
194194
* @memberof module:Internationalization
195195
*/
196-
export function getSupportedLocales() {
196+
export function getSupportedLocaleList() {
197197
if (messages === null) {
198-
throw new Error('getSupportedLocales called before configuring i18n. Call configure with messages first.');
198+
throw new Error('getSupportedLocaleList called before configuring i18n. Call configure with messages first.');
199199
}
200-
return Object.keys(messages);
200+
201+
const locales = Object.keys(messages);
202+
if (!locales.includes('en')) {
203+
locales.push('en');
204+
}
205+
206+
return locales;
201207
}
202208

203209
/**

src/i18n/lib.test.js

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
getPrimaryLanguageSubtag,
55
getLocale,
66
getMessages,
7-
getSupportedLocales,
7+
getSupportedLocaleList,
88
isRtl,
99
handleRtl,
1010
getCookies,
@@ -186,36 +186,28 @@ describe('lib', () => {
186186
});
187187

188188
describe('getSupportedLocales', () => {
189-
beforeEach(() => {
190-
configure({
191-
loggingService: { logError: jest.fn() },
192-
config: {
193-
ENVIRONMENT: 'production',
194-
LANGUAGE_PREFERENCE_COOKIE_NAME: 'yum',
195-
},
196-
messages: {
197-
'es-419': { message: 'es-hah' },
198-
de: { message: 'de-hah' },
199-
'en-us': { message: 'en-us-hah' },
200-
fr: { message: 'fr-hah' },
201-
},
189+
describe('when configured', () => {
190+
beforeEach(() => {
191+
configure({
192+
loggingService: { logError: jest.fn() },
193+
config: {
194+
ENVIRONMENT: 'production',
195+
LANGUAGE_PREFERENCE_COOKIE_NAME: 'yum',
196+
},
197+
messages: {
198+
'es-419': { message: 'es-hah' },
199+
de: { message: 'de-hah' },
200+
'en-us': { message: 'en-us-hah' },
201+
fr: { message: 'fr-hah' },
202+
},
203+
});
202204
});
203-
});
204-
205-
it('should return an array of supported locale codes', () => {
206-
const supportedLocales = getSupportedLocales();
207-
expect(Array.isArray(supportedLocales)).toBe(true);
208-
expect(supportedLocales).toEqual(['es-419', 'de', 'en-us', 'fr']);
209-
});
210205

211-
it('should throw an error if i18n is not configured', () => {
212-
// Reset the configuration to null
213-
jest.resetModules();
214-
const { getSupportedLocales: freshGetSupportedLocales } = require('./lib');
215-
216-
expect(() => freshGetSupportedLocales()).toThrow(
217-
'getSupportedLocales called before configuring i18n. Call configure with messages first.'
218-
);
206+
it('should return an array of supported locale codes', () => {
207+
const supportedLocales = getSupportedLocaleList();
208+
expect(Array.isArray(supportedLocales)).toBe(true);
209+
expect(supportedLocales).toEqual(['es-419', 'de', 'en-us', 'fr', 'en']);
210+
});
219211
});
220212
});
221213

0 commit comments

Comments
 (0)