mirror of
https://github.com/strapi/strapi.git
synced 2025-11-03 03:17:11 +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}`,
|
||||
}));
|
||||
|
||||
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))
|
||||
|
||||
@ -2,6 +2,12 @@
|
||||
|
||||
const isoLocales = require('./iso-locales');
|
||||
|
||||
const DEFAULT_LOCALE = {
|
||||
name: 'English',
|
||||
code: 'en',
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
isoLocales,
|
||||
DEFAULT_LOCALE,
|
||||
};
|
||||
|
||||
@ -81,7 +81,6 @@ components:
|
||||
localeInputCreate:
|
||||
type: object
|
||||
required:
|
||||
- name
|
||||
- code
|
||||
- isDefault
|
||||
properties:
|
||||
|
||||
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -13,10 +13,8 @@ const createLocaleSchema = yup
|
||||
.shape({
|
||||
name: yup
|
||||
.string()
|
||||
.min(1)
|
||||
.max(50)
|
||||
.nullable()
|
||||
.required(),
|
||||
.nullable(),
|
||||
code: yup
|
||||
.string()
|
||||
.oneOf(allowedLocaleCodes)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user