2021-01-27 12:35:24 +01:00
|
|
|
'use strict';
|
|
|
|
|
2021-11-05 10:40:11 +01:00
|
|
|
const utils = require('@strapi/utils');
|
2021-02-05 15:39:11 +01:00
|
|
|
const { pick } = require('lodash/fp');
|
2021-02-09 15:49:42 +01:00
|
|
|
const { getService } = require('../utils');
|
2021-01-27 12:35:24 +01:00
|
|
|
const { validateCreateLocaleInput, validateUpdateLocaleInput } = require('../validation/locales');
|
|
|
|
const { formatLocale } = require('../domain/locale');
|
|
|
|
|
2021-11-05 10:40:11 +01:00
|
|
|
const { setCreatorFields, sanitize } = utils;
|
|
|
|
const { ApplicationError } = utils.errors;
|
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const sanitizeLocale = (locale) => {
|
2021-08-06 18:09:49 +02:00
|
|
|
const model = strapi.getModel('plugin::i18n.locale');
|
2021-01-27 17:52:41 +01:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
return sanitize.contentAPI.output(locale, model);
|
2021-01-27 17:52:41 +01:00
|
|
|
};
|
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
module.exports = {
|
|
|
|
async listLocales(ctx) {
|
|
|
|
const localesService = getService('locales');
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
const locales = await localesService.find();
|
2021-11-04 15:47:53 +01:00
|
|
|
const sanitizedLocales = await sanitizeLocale(locales);
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
ctx.body = await localesService.setIsDefault(sanitizedLocales);
|
2021-02-02 11:33:17 +01:00
|
|
|
},
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
async createLocale(ctx) {
|
|
|
|
const { user } = ctx.state;
|
|
|
|
const { body } = ctx.request;
|
2022-08-08 23:33:39 +02:00
|
|
|
const { isDefault, ...localeToCreate } = body;
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-10-20 17:30:05 +02:00
|
|
|
await validateCreateLocaleInput(body);
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
const localesService = getService('locales');
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
const existingLocale = await localesService.findByCode(body.code);
|
|
|
|
if (existingLocale) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError('This locale already exists');
|
2021-02-02 11:33:17 +01:00
|
|
|
}
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const localeToPersist = setCreatorFields({ user })(formatLocale(localeToCreate));
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2022-08-08 23:33:39 +02:00
|
|
|
const locale = await localesService.create(localeToPersist);
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-09 11:36:28 +01:00
|
|
|
if (isDefault) {
|
|
|
|
await localesService.setDefaultLocale(locale);
|
|
|
|
}
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
const sanitizedLocale = await sanitizeLocale(locale);
|
|
|
|
|
|
|
|
ctx.body = await localesService.setIsDefault(sanitizedLocale);
|
2021-02-02 11:33:17 +01:00
|
|
|
},
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
async updateLocale(ctx) {
|
|
|
|
const { user } = ctx.state;
|
|
|
|
const { id } = ctx.params;
|
|
|
|
const { body } = ctx.request;
|
2022-08-08 15:50:34 +02:00
|
|
|
const { isDefault, ...updates } = body;
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-10-20 17:30:05 +02:00
|
|
|
await validateUpdateLocaleInput(body);
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
const localesService = getService('locales');
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-02 11:33:17 +01:00
|
|
|
const existingLocale = await localesService.findById(id);
|
|
|
|
if (!existingLocale) {
|
|
|
|
return ctx.notFound('locale.notFound');
|
|
|
|
}
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-16 10:26:47 +01:00
|
|
|
const allowedParams = ['name'];
|
2021-02-05 15:39:11 +01:00
|
|
|
const cleanUpdates = setCreatorFields({ user, isEdition: true })(pick(allowedParams, updates));
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-02-09 11:36:28 +01:00
|
|
|
const updatedLocale = await localesService.update({ id }, cleanUpdates);
|
|
|
|
|
|
|
|
if (isDefault) {
|
|
|
|
await localesService.setDefaultLocale(updatedLocale);
|
|
|
|
}
|
2021-01-27 12:35:24 +01:00
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
const sanitizedLocale = await sanitizeLocale(updatedLocale);
|
|
|
|
|
|
|
|
ctx.body = await localesService.setIsDefault(sanitizedLocale);
|
2021-02-02 11:33:17 +01:00
|
|
|
},
|
2021-02-12 15:38:25 +01:00
|
|
|
|
|
|
|
async deleteLocale(ctx) {
|
|
|
|
const { id } = ctx.params;
|
|
|
|
|
|
|
|
const localesService = getService('locales');
|
|
|
|
|
|
|
|
const existingLocale = await localesService.findById(id);
|
|
|
|
if (!existingLocale) {
|
|
|
|
return ctx.notFound('locale.notFound');
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultLocaleCode = await localesService.getDefaultLocale();
|
|
|
|
if (existingLocale.code === defaultLocaleCode) {
|
2021-10-20 17:30:05 +02:00
|
|
|
throw new ApplicationError('Cannot delete the default locale');
|
2021-02-12 15:38:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
await localesService.delete({ id });
|
|
|
|
|
2021-11-04 15:47:53 +01:00
|
|
|
const sanitizedLocale = await sanitizeLocale(existingLocale);
|
|
|
|
|
|
|
|
ctx.body = await localesService.setIsDefault(sanitizedLocale);
|
2021-02-12 15:38:25 +01:00
|
|
|
},
|
2021-01-27 12:35:24 +01:00
|
|
|
};
|