-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Expand file tree
/
Copy pathindex.mjs
More file actions
41 lines (34 loc) · 1.42 KB
/
index.mjs
File metadata and controls
41 lines (34 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
'use strict';
import localeConfig from './config.json' with { type: 'json' };
/**
* Imports a locale when exists from the locales directory
*
* @param {string} locale The locale code to import
* @returns {Promise<import('./types').Locale>} The imported locale
*/
export const importLocale = async locale => {
return import(`./locales/${locale}.json`, { with: { type: 'json' } }).then(
f => f.default
);
};
/**
* A set of available and enabled locales for the website
* This is used for allowing us to redirect the user to any
* of the available locales that we have enabled on the website
*
* @returns {Array<import('./types').LocaleConfig>}
*/
export const getAvailableLocales = () =>
localeConfig.filter(locale => locale.enabled);
// This gives an easy way of accessing all available locale codes
export const getAvailableLocaleCodes = () =>
getAvailableLocales().map(locale => locale.code);
// This provides the default locale information for the Next.js Application
// This is marked by the unique `locale.default` property on the `en` locale
export const getDefaultLocale = () =>
getAvailableLocales().find(locale => locale.default);
// Creates a Map of available locales for easy access
export const getAvailableLocalesMap = () =>
Object.fromEntries(localeConfig.map(locale => [locale.code, locale]));
// Creates all supported locales
export const getAllLocaleCodes = () => localeConfig.map(locale => locale.code);