From e939a2aa5a121e369563b2f443994e13db2fac26 Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Tue, 20 Apr 2021 09:41:23 +0200 Subject: [PATCH 1/2] Add an env var to set the init locale code on startup --- .../constants/__tests__/index.test.js | 26 +++++++++++++++++ .../strapi-plugin-i18n/constants/index.js | 29 +++++++++++++++++-- .../services/__tests__/locales.test.js | 2 +- 3 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 packages/strapi-plugin-i18n/constants/__tests__/index.test.js diff --git a/packages/strapi-plugin-i18n/constants/__tests__/index.test.js b/packages/strapi-plugin-i18n/constants/__tests__/index.test.js new file mode 100644 index 0000000000..0abdf2e372 --- /dev/null +++ b/packages/strapi-plugin-i18n/constants/__tests__/index.test.js @@ -0,0 +1,26 @@ +'use strict'; + +const { getInitLocale } = require('../'); + +describe('I18N default locale', () => { + describe('getInitLocale', () => { + test('The init locale is english by default', () => { + expect(getInitLocale()).toEqual({ + 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()).toMatchObject({ + code: 'fr', + }); + }); + + test('Throws if env var code is unknown in iso list', () => { + process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE = 'zzzzz'; + expect(() => getInitLocale()).toThrow(); + }); + }); +}); diff --git a/packages/strapi-plugin-i18n/constants/index.js b/packages/strapi-plugin-i18n/constants/index.js index e8b2428a53..d9455c543c 100644 --- a/packages/strapi-plugin-i18n/constants/index.js +++ b/packages/strapi-plugin-i18n/constants/index.js @@ -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 envrionment 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, }; diff --git a/packages/strapi-plugin-i18n/services/__tests__/locales.test.js b/packages/strapi-plugin-i18n/services/__tests__/locales.test.js index 74ced80d73..2384785635 100644 --- a/packages/strapi-plugin-i18n/services/__tests__/locales.test.js +++ b/packages/strapi-plugin-i18n/services/__tests__/locales.test.js @@ -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' }); From 7669b52aebc8e794f0431391fcfee03a5236beef Mon Sep 17 00:00:00 2001 From: Alexandre Bodin Date: Tue, 20 Apr 2021 10:52:22 +0200 Subject: [PATCH 2/2] Fix typo --- .../strapi-plugin-i18n/constants/__tests__/index.test.js | 5 +++-- packages/strapi-plugin-i18n/constants/index.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/strapi-plugin-i18n/constants/__tests__/index.test.js b/packages/strapi-plugin-i18n/constants/__tests__/index.test.js index 0abdf2e372..8112154766 100644 --- a/packages/strapi-plugin-i18n/constants/__tests__/index.test.js +++ b/packages/strapi-plugin-i18n/constants/__tests__/index.test.js @@ -5,7 +5,7 @@ const { getInitLocale } = require('../'); describe('I18N default locale', () => { describe('getInitLocale', () => { test('The init locale is english by default', () => { - expect(getInitLocale()).toEqual({ + expect(getInitLocale()).toStrictEqual({ code: 'en', name: 'English (en)', }); @@ -13,8 +13,9 @@ describe('I18N default locale', () => { test('The init locale can be configured by an env var', () => { process.env.STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE = 'fr'; - expect(getInitLocale()).toMatchObject({ + expect(getInitLocale()).toStrictEqual({ code: 'fr', + name: 'French (fr)', }); }); diff --git a/packages/strapi-plugin-i18n/constants/index.js b/packages/strapi-plugin-i18n/constants/index.js index d9455c543c..9eb537d692 100644 --- a/packages/strapi-plugin-i18n/constants/index.js +++ b/packages/strapi-plugin-i18n/constants/index.js @@ -14,7 +14,7 @@ const getInitLocale = () => { if (!matchingLocale) { throw new Error( - 'Unknown locale code provided in the envrionment variable STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE' + 'Unknown locale code provided in the environment variable STRAPI_PLUGIN_I18N_INIT_LOCALE_CODE' ); }