mirror of
https://github.com/strapi/strapi.git
synced 2025-09-06 23:28:02 +00:00
Merge pull request #10081 from strapi/i18n/allow-env-var-default-locale
Add an env var to set the init locale code on startup
This commit is contained in:
commit
dbcaad36cc
@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
const { getInitLocale } = require('../');
|
||||
|
||||
describe('I18N default locale', () => {
|
||||
describe('getInitLocale', () => {
|
||||
test('The init locale is english by default', () => {
|
||||
expect(getInitLocale()).toStrictEqual({
|
||||
code: 'en',
|
||||
name: 'English (en)',
|
||||
});
|
||||
});
|
||||
|
||||
test('The init locale can be configured by an env var', () => {
|
||||
process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE = 'fr';
|
||||
expect(getInitLocale()).toStrictEqual({
|
||||
code: 'fr',
|
||||
name: 'French (fr)',
|
||||
});
|
||||
});
|
||||
|
||||
test('Throws if env var code is unknown in iso list', () => {
|
||||
process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE = 'zzzzz';
|
||||
expect(() => getInitLocale()).toThrow();
|
||||
});
|
||||
});
|
||||
});
|
@ -2,12 +2,35 @@
|
||||
|
||||
const isoLocales = require('./iso-locales');
|
||||
|
||||
const DEFAULT_LOCALE = {
|
||||
name: 'English',
|
||||
code: 'en',
|
||||
/**
|
||||
* Returns the default locale based either on env var or english
|
||||
* @returns {string}
|
||||
*/
|
||||
const getInitLocale = () => {
|
||||
const envLocaleCode = process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE;
|
||||
|
||||
if (envLocaleCode) {
|
||||
const matchingLocale = isoLocales.find(({ code }) => code === envLocaleCode);
|
||||
|
||||
if (!matchingLocale) {
|
||||
throw new Error(
|
||||
'Unknown locale code provided in the environment variable STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE'
|
||||
);
|
||||
}
|
||||
|
||||
return { ...matchingLocale };
|
||||
}
|
||||
|
||||
return {
|
||||
code: 'en',
|
||||
name: 'English (en)',
|
||||
};
|
||||
};
|
||||
|
||||
const DEFAULT_LOCALE = getInitLocale();
|
||||
|
||||
module.exports = {
|
||||
isoLocales,
|
||||
DEFAULT_LOCALE,
|
||||
getInitLocale,
|
||||
};
|
||||
|
@ -172,7 +172,7 @@ describe('Locales', () => {
|
||||
await localesService.initDefaultLocale();
|
||||
expect(count).toHaveBeenCalledWith();
|
||||
expect(create).toHaveBeenCalledWith({
|
||||
name: 'English',
|
||||
name: 'English (en)',
|
||||
code: 'en',
|
||||
});
|
||||
expect(set).toHaveBeenCalledWith({ key: 'default_locale', value: 'en' });
|
||||
|
Loading…
x
Reference in New Issue
Block a user