2018-05-08 12:23:42 +09:00
|
|
|
import SimpleSchema from 'simpl-schema';
|
|
|
|
|
2018-10-20 15:55:38 +09:00
|
|
|
export const Strings = {};
|
|
|
|
|
|
|
|
export const Domains = {};
|
|
|
|
|
|
|
|
export const addStrings = (language, strings) => {
|
|
|
|
if (typeof Strings[language] === 'undefined') {
|
|
|
|
Strings[language] = {};
|
|
|
|
}
|
|
|
|
Strings[language] = {
|
|
|
|
...Strings[language],
|
|
|
|
...strings
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getString = ({id, values, defaultMessage, locale}) => {
|
|
|
|
const messages = Strings[locale] || {};
|
|
|
|
let message = messages[id] || defaultMessage;
|
|
|
|
if (message && values) {
|
|
|
|
Object.keys(values).forEach(key => {
|
|
|
|
// note: see replaceAll definition in vulcan:lib/utils
|
|
|
|
message = message.replaceAll(`{${key}}`, values[key]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const registerDomain = (locale, domain) => {
|
|
|
|
Domains[domain] = locale;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:46:47 +09:00
|
|
|
export const Locales = [];
|
|
|
|
|
2018-05-10 17:17:44 +09:00
|
|
|
export const registerLocale = locale => {
|
2018-05-09 09:46:47 +09:00
|
|
|
Locales.push(locale);
|
2018-10-25 10:23:00 +02:00
|
|
|
};
|
2018-05-09 09:46:47 +09:00
|
|
|
|
2018-05-08 12:23:42 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Look for type name in a few different places
|
|
|
|
Note: look into simplifying this
|
|
|
|
|
|
|
|
*/
|
|
|
|
export const isIntlField = fieldSchema => {
|
2018-08-17 19:02:44 +09:00
|
|
|
return fieldSchema.intl;
|
2018-10-25 10:23:00 +02:00
|
|
|
};
|
2018-05-08 12:23:42 +09:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Generate custom IntlString SimpleSchema type
|
|
|
|
|
|
|
|
*/
|
2018-05-08 12:29:51 +09:00
|
|
|
export const getIntlString = () => {
|
2018-06-28 21:35:44 +02:00
|
|
|
const schema = {
|
|
|
|
locale: {
|
|
|
|
type: String,
|
2018-10-25 10:23:00 +02:00
|
|
|
optional: true
|
2018-06-28 21:35:44 +02:00
|
|
|
},
|
|
|
|
value: {
|
2018-05-08 12:29:51 +09:00
|
|
|
type: String,
|
2018-10-25 10:23:00 +02:00
|
|
|
optional: true
|
2018-06-28 21:35:44 +02:00
|
|
|
}
|
|
|
|
};
|
2018-05-08 12:29:51 +09:00
|
|
|
|
|
|
|
const IntlString = new SimpleSchema(schema);
|
|
|
|
IntlString.name = 'IntlString';
|
|
|
|
return IntlString;
|
2018-10-25 10:23:00 +02:00
|
|
|
};
|
2018-06-30 11:48:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Custom validation function to check for required locales
|
|
|
|
|
|
|
|
See https://github.com/aldeed/simple-schema-js#custom-field-validation
|
|
|
|
|
|
|
|
*/
|
2018-10-25 10:23:00 +02:00
|
|
|
export const validateIntlField = function() {
|
2018-06-30 11:48:37 +02:00
|
|
|
let errors = [];
|
|
|
|
|
2018-08-17 19:02:44 +09:00
|
|
|
// go through locales to check which one are required
|
|
|
|
const requiredLocales = Locales.filter(locale => locale.required);
|
2018-06-30 11:48:37 +02:00
|
|
|
|
2018-08-17 19:02:44 +09:00
|
|
|
requiredLocales.forEach((locale, index) => {
|
|
|
|
const strings = this.value;
|
2018-10-13 14:13:08 +09:00
|
|
|
const hasString = strings && Array.isArray(strings) && strings.some(s => s && s.locale === locale.id && s.value);
|
2018-08-17 19:02:44 +09:00
|
|
|
if (!hasString) {
|
2018-06-30 11:48:37 +02:00
|
|
|
const originalFieldName = this.key.replace('_intl', '');
|
2018-10-25 10:23:00 +02:00
|
|
|
errors.push({
|
|
|
|
id: '',
|
|
|
|
path: `${this.key}.${index}`,
|
2018-10-25 15:55:47 +02:00
|
|
|
properties: { name: originalFieldName, locale: locale.id }
|
2018-10-25 10:23:00 +02:00
|
|
|
});
|
2018-08-17 19:02:44 +09:00
|
|
|
}
|
|
|
|
});
|
2018-06-30 11:48:37 +02:00
|
|
|
|
2018-08-17 19:02:44 +09:00
|
|
|
if (errors.length > 0) {
|
|
|
|
// hack to work around the fact that custom validation function can only return a single string
|
|
|
|
return `intlError|${JSON.stringify(errors)}`;
|
2018-06-30 11:48:37 +02:00
|
|
|
}
|
2018-10-25 10:23:00 +02:00
|
|
|
};
|