diff --git a/packages/strapi-plugin-i18n/config/functions/bootstrap.js b/packages/strapi-plugin-i18n/config/functions/bootstrap.js index e5e4595a50..cb99c990c2 100644 --- a/packages/strapi-plugin-i18n/config/functions/bootstrap.js +++ b/packages/strapi-plugin-i18n/config/functions/bootstrap.js @@ -12,18 +12,11 @@ const actions = ['create', 'read', 'update', 'delete'].map(uid => ({ uid: `locale.${uid}`, })); -const DEFAULT_LOCALE = { - code: 'en-US', -}; - module.exports = async () => { const { actionProvider } = strapi.admin.services.permission; actionProvider.register(actions); - const defaultLocale = await getService('locales').getDefaultLocale(); - if (!defaultLocale) { - await getService('locales').setDefaultLocale(DEFAULT_LOCALE); - } + await getService('locales').initDefaultLocale(); Object.values(strapi.models) .filter(model => getService('content-types').isLocalized(model)) diff --git a/packages/strapi-plugin-i18n/constants/index.js b/packages/strapi-plugin-i18n/constants/index.js index e9d281f53a..e8b2428a53 100644 --- a/packages/strapi-plugin-i18n/constants/index.js +++ b/packages/strapi-plugin-i18n/constants/index.js @@ -2,6 +2,12 @@ const isoLocales = require('./iso-locales'); +const DEFAULT_LOCALE = { + name: 'English', + code: 'en', +}; + module.exports = { isoLocales, + DEFAULT_LOCALE, }; diff --git a/packages/strapi-plugin-i18n/oas.yml b/packages/strapi-plugin-i18n/oas.yml index 59e53eb997..3404bc05e8 100644 --- a/packages/strapi-plugin-i18n/oas.yml +++ b/packages/strapi-plugin-i18n/oas.yml @@ -81,7 +81,6 @@ components: localeInputCreate: type: object required: - - name - code - isDefault properties: diff --git a/packages/strapi-plugin-i18n/services/__tests__/locales.test.js b/packages/strapi-plugin-i18n/services/__tests__/locales.test.js index 702d6075d0..6d1c9bdc4a 100644 --- a/packages/strapi-plugin-i18n/services/__tests__/locales.test.js +++ b/packages/strapi-plugin-i18n/services/__tests__/locales.test.js @@ -131,4 +131,51 @@ describe('Locales', () => { expect(deletedLocale).toMatchObject(locale); }); }); + + describe('initDefaultLocale', () => { + test('create default local if none exists', async () => { + const count = jest.fn(() => Promise.resolve(0)); + const create = jest.fn(() => Promise.resolve()); + const set = jest.fn(() => Promise.resolve()); + + global.strapi = { + query: () => ({ + count, + create, + }), + store: () => ({ + set, + }), + }; + + await localesService.initDefaultLocale(); + expect(count).toHaveBeenCalledWith(); + expect(create).toHaveBeenCalledWith({ + name: 'English', + code: 'en', + }); + expect(set).toHaveBeenCalledWith({ key: 'default_locale', value: 'en' }); + }); + + test('does not create default local if one already exists', async () => { + const count = jest.fn(() => Promise.resolve(1)); + const create = jest.fn(() => Promise.resolve()); + const set = jest.fn(() => Promise.resolve()); + + global.strapi = { + query: () => ({ + count, + create, + }), + store: () => ({ + set, + }), + }; + + await localesService.initDefaultLocale(); + expect(count).toHaveBeenCalledWith(); + expect(create).not.toHaveBeenCalled(); + expect(set).not.toHaveBeenCalled(); + }); + }); }); diff --git a/packages/strapi-plugin-i18n/services/locales.js b/packages/strapi-plugin-i18n/services/locales.js index a0e2b7c9cb..a778b3063f 100644 --- a/packages/strapi-plugin-i18n/services/locales.js +++ b/packages/strapi-plugin-i18n/services/locales.js @@ -1,6 +1,7 @@ 'use strict'; const { isNil } = require('lodash/fp'); +const { DEFAULT_LOCALE } = require('../constants'); const { getCoreStore } = require('../utils'); @@ -35,6 +36,14 @@ const setIsDefault = async locales => { } }; +const initDefaultLocale = async () => { + const existingLocalesNb = await strapi.query('locale', 'i18n').count(); + if (existingLocalesNb === 0) { + await create(DEFAULT_LOCALE); + await setDefaultLocale({ code: DEFAULT_LOCALE.code }); + } +}; + module.exports = { find, findById, @@ -45,4 +54,5 @@ module.exports = { getDefaultLocale, setIsDefault, delete: deleteFn, + initDefaultLocale, }; diff --git a/packages/strapi-plugin-i18n/tests/locales.test.e2e.js b/packages/strapi-plugin-i18n/tests/locales.test.e2e.js index 89dc22bff2..8eccb85e7f 100644 --- a/packages/strapi-plugin-i18n/tests/locales.test.e2e.js +++ b/packages/strapi-plugin-i18n/tests/locales.test.e2e.js @@ -26,6 +26,20 @@ describe('CRUD locales', () => { await strapi.destroy(); }); + describe('Default locale', () => { + test('Default locale is already created', async () => { + let res = await rq({ + url: '/i18n/locales', + method: 'GET', + }); + + expect(res.statusCode).toBe(200); + expect(res.body).toHaveLength(1); + expect(res.body[0].isDefault).toBe(true); + data.locales.push(res.body[0]); + }); + }); + describe('Creation', () => { test('Can create a locale', async () => { const locale = { @@ -113,11 +127,12 @@ describe('CRUD locales', () => { let res = await rq({ url: '/i18n/locales', method: 'POST', - body: { code: 'en', name: 'random', isDefault: true }, + body: { code: 'bas', name: 'random', isDefault: true }, }); expect(res.statusCode).toBe(200); expect(res.body.isDefault).toBe(true); + data.locales[0].isDefault = false; res = await rq({ url: '/i18n/locales', @@ -133,7 +148,7 @@ describe('CRUD locales', () => { }); expect(res.statusCode).toBe(200); - const enLocale = res.body.find(locale => locale.code === 'en'); + const enLocale = res.body.find(locale => locale.code === 'bas'); const enUsLocale = res.body.find(locale => locale.code === 'en-US'); expect(enLocale.isDefault).toBe(false); expect(enUsLocale.isDefault).toBe(true); diff --git a/packages/strapi-plugin-i18n/validation/locales.js b/packages/strapi-plugin-i18n/validation/locales.js index eb2fb26b23..f070447d7c 100644 --- a/packages/strapi-plugin-i18n/validation/locales.js +++ b/packages/strapi-plugin-i18n/validation/locales.js @@ -13,10 +13,8 @@ const createLocaleSchema = yup .shape({ name: yup .string() - .min(1) .max(50) - .nullable() - .required(), + .nullable(), code: yup .string() .oneOf(allowedLocaleCodes)