mirror of
https://github.com/strapi/strapi.git
synced 2025-11-15 17:49:57 +00:00
Merge pull request #9575 from strapi/i18n/defaultLocaleAndEmptyLocaleName
create default locale and allow empty name for a locale
This commit is contained in:
commit
628f128bc0
@ -12,18 +12,11 @@ const actions = ['create', 'read', 'update', 'delete'].map(uid => ({
|
|||||||
uid: `locale.${uid}`,
|
uid: `locale.${uid}`,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
const DEFAULT_LOCALE = {
|
|
||||||
code: 'en-US',
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = async () => {
|
module.exports = async () => {
|
||||||
const { actionProvider } = strapi.admin.services.permission;
|
const { actionProvider } = strapi.admin.services.permission;
|
||||||
actionProvider.register(actions);
|
actionProvider.register(actions);
|
||||||
|
|
||||||
const defaultLocale = await getService('locales').getDefaultLocale();
|
await getService('locales').initDefaultLocale();
|
||||||
if (!defaultLocale) {
|
|
||||||
await getService('locales').setDefaultLocale(DEFAULT_LOCALE);
|
|
||||||
}
|
|
||||||
|
|
||||||
Object.values(strapi.models)
|
Object.values(strapi.models)
|
||||||
.filter(model => getService('content-types').isLocalized(model))
|
.filter(model => getService('content-types').isLocalized(model))
|
||||||
|
|||||||
@ -2,6 +2,12 @@
|
|||||||
|
|
||||||
const isoLocales = require('./iso-locales');
|
const isoLocales = require('./iso-locales');
|
||||||
|
|
||||||
|
const DEFAULT_LOCALE = {
|
||||||
|
name: 'English',
|
||||||
|
code: 'en',
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isoLocales,
|
isoLocales,
|
||||||
|
DEFAULT_LOCALE,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -81,7 +81,6 @@ components:
|
|||||||
localeInputCreate:
|
localeInputCreate:
|
||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
- name
|
|
||||||
- code
|
- code
|
||||||
- isDefault
|
- isDefault
|
||||||
properties:
|
properties:
|
||||||
|
|||||||
@ -131,4 +131,51 @@ describe('Locales', () => {
|
|||||||
expect(deletedLocale).toMatchObject(locale);
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { isNil } = require('lodash/fp');
|
const { isNil } = require('lodash/fp');
|
||||||
|
const { DEFAULT_LOCALE } = require('../constants');
|
||||||
|
|
||||||
const { getCoreStore } = require('../utils');
|
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 = {
|
module.exports = {
|
||||||
find,
|
find,
|
||||||
findById,
|
findById,
|
||||||
@ -45,4 +54,5 @@ module.exports = {
|
|||||||
getDefaultLocale,
|
getDefaultLocale,
|
||||||
setIsDefault,
|
setIsDefault,
|
||||||
delete: deleteFn,
|
delete: deleteFn,
|
||||||
|
initDefaultLocale,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -26,6 +26,20 @@ describe('CRUD locales', () => {
|
|||||||
await strapi.destroy();
|
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', () => {
|
describe('Creation', () => {
|
||||||
test('Can create a locale', async () => {
|
test('Can create a locale', async () => {
|
||||||
const locale = {
|
const locale = {
|
||||||
@ -113,11 +127,12 @@ describe('CRUD locales', () => {
|
|||||||
let res = await rq({
|
let res = await rq({
|
||||||
url: '/i18n/locales',
|
url: '/i18n/locales',
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: { code: 'en', name: 'random', isDefault: true },
|
body: { code: 'bas', name: 'random', isDefault: true },
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
expect(res.statusCode).toBe(200);
|
||||||
expect(res.body.isDefault).toBe(true);
|
expect(res.body.isDefault).toBe(true);
|
||||||
|
data.locales[0].isDefault = false;
|
||||||
|
|
||||||
res = await rq({
|
res = await rq({
|
||||||
url: '/i18n/locales',
|
url: '/i18n/locales',
|
||||||
@ -133,7 +148,7 @@ describe('CRUD locales', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
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');
|
const enUsLocale = res.body.find(locale => locale.code === 'en-US');
|
||||||
expect(enLocale.isDefault).toBe(false);
|
expect(enLocale.isDefault).toBe(false);
|
||||||
expect(enUsLocale.isDefault).toBe(true);
|
expect(enUsLocale.isDefault).toBe(true);
|
||||||
|
|||||||
@ -13,10 +13,8 @@ const createLocaleSchema = yup
|
|||||||
.shape({
|
.shape({
|
||||||
name: yup
|
name: yup
|
||||||
.string()
|
.string()
|
||||||
.min(1)
|
|
||||||
.max(50)
|
.max(50)
|
||||||
.nullable()
|
.nullable(),
|
||||||
.required(),
|
|
||||||
code: yup
|
code: yup
|
||||||
.string()
|
.string()
|
||||||
.oneOf(allowedLocaleCodes)
|
.oneOf(allowedLocaleCodes)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user