Merge pull request #9575 from strapi/i18n/defaultLocaleAndEmptyLocaleName

create default locale and allow empty name for a locale
This commit is contained in:
Alexandre BODIN 2021-03-03 14:41:03 +01:00 committed by GitHub
commit 628f128bc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 14 deletions

View File

@ -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))

View File

@ -2,6 +2,12 @@
const isoLocales = require('./iso-locales');
const DEFAULT_LOCALE = {
name: 'English',
code: 'en',
};
module.exports = {
isoLocales,
DEFAULT_LOCALE,
};

View File

@ -81,7 +81,6 @@ components:
localeInputCreate:
type: object
required:
- name
- code
- isDefault
properties:

View File

@ -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();
});
});
});

View File

@ -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,
};

View File

@ -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);

View File

@ -13,10 +13,8 @@ const createLocaleSchema = yup
.shape({
name: yup
.string()
.min(1)
.max(50)
.nullable()
.required(),
.nullable(),
code: yup
.string()
.oneOf(allowedLocaleCodes)