Skip to content

Commit 837ef56

Browse files
committed
feat(i18n): add getSupportedLocales function
1 parent b63d4ee commit 837ef56

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/i18n/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export {
102102
getPrimaryLanguageSubtag,
103103
getLocale,
104104
getMessages,
105+
getSupportedLocales,
105106
isRtl,
106107
handleRtl,
107108
mergeMessages,

src/i18n/lib.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,22 @@ export function getMessages(locale = getLocale()) {
184184
return messages[locale];
185185
}
186186

187+
/**
188+
* Returns the list of supported locales based on the configured messages.
189+
* This list is dynamically generated from the translation messages that were
190+
* provided during i18n configuration.
191+
*
192+
* @throws An error if i18n has not yet been configured.
193+
* @returns {string[]} Array of supported locale codes
194+
* @memberof module:Internationalization
195+
*/
196+
export function getSupportedLocales() {
197+
if (messages === null) {
198+
throw new Error('getSupportedLocales called before configuring i18n. Call configure with messages first.');
199+
}
200+
return Object.keys(messages);
201+
}
202+
187203
/**
188204
* Determines if the provided locale is a right-to-left language.
189205
*

src/i18n/lib.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
getPrimaryLanguageSubtag,
55
getLocale,
66
getMessages,
7+
getSupportedLocales,
78
isRtl,
89
handleRtl,
910
getCookies,
@@ -184,6 +185,40 @@ describe('lib', () => {
184185
});
185186
});
186187

188+
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+
},
202+
});
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+
});
210+
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+
);
219+
});
220+
});
221+
187222
describe('isRtl', () => {
188223
it('should be true for RTL languages', () => {
189224
expect(isRtl('ar')).toBe(true);

0 commit comments

Comments
 (0)