2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Collection of helper functions to deal with localization.
|
|
|
|
*/
|
2016-08-06 16:46:54 +02:00
|
|
|
import { i18nRecord } from "../models/i18n";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Get the preferred locales from the browser, as an array sorted by preferences.
|
|
|
|
*/
|
2016-08-10 23:50:23 +02:00
|
|
|
export function getBrowserLocales() {
|
2016-08-10 21:36:11 +02:00
|
|
|
let langs = [];
|
2016-07-28 01:22:48 +02:00
|
|
|
|
|
|
|
if (navigator.languages) {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Chrome does not currently set navigator.language correctly
|
|
|
|
// https://code.google.com/p/chromium/issues/detail?id=101138
|
2016-07-28 01:22:48 +02:00
|
|
|
// but it does set the first element of navigator.languages correctly
|
2016-08-01 00:26:52 +02:00
|
|
|
langs = navigator.languages;
|
2016-07-28 01:22:48 +02:00
|
|
|
} else if (navigator.userLanguage) {
|
|
|
|
// IE only
|
2016-08-01 00:26:52 +02:00
|
|
|
langs = [navigator.userLanguage];
|
2016-07-28 01:22:48 +02:00
|
|
|
} else {
|
|
|
|
// as of this writing the latest version of firefox + safari set this correctly
|
2016-08-01 00:26:52 +02:00
|
|
|
langs = [navigator.language];
|
2016-07-28 01:22:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Some browsers does not return uppercase for second part
|
2016-08-05 00:00:25 +02:00
|
|
|
let locales = langs.map(function (lang) {
|
|
|
|
let locale = lang.split("-");
|
2016-08-01 00:26:52 +02:00
|
|
|
return locale[1] ? `${locale[0]}-${locale[1].toUpperCase()}` : lang;
|
|
|
|
});
|
2016-07-28 01:22:48 +02:00
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
return locales;
|
2016-07-28 01:22:48 +02:00
|
|
|
}
|
2016-07-28 23:14:52 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an array of messagesDescriptors to a map.
|
|
|
|
*/
|
2016-07-28 23:14:52 +02:00
|
|
|
export function messagesMap(messagesDescriptorsArray) {
|
2016-08-05 00:00:25 +02:00
|
|
|
let messagesDescriptorsMap = {};
|
2016-07-28 23:14:52 +02:00
|
|
|
|
|
|
|
messagesDescriptorsArray.forEach(function (item) {
|
|
|
|
messagesDescriptorsMap[item.id] = item;
|
|
|
|
});
|
|
|
|
|
|
|
|
return messagesDescriptorsMap;
|
|
|
|
}
|
2016-08-06 16:46:54 +02:00
|
|
|
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Format an error message from the state.
|
|
|
|
*
|
|
|
|
* Error message can be either an i18nRecord, which is to be formatted, or a
|
|
|
|
* raw string. This function performs the check and returns the correctly
|
|
|
|
* formatted string.
|
|
|
|
*
|
|
|
|
* @param errorMessage The error message from the state, either plain
|
|
|
|
* string or i18nRecord.
|
|
|
|
* @param formatMessage react-i18n formatMessage.
|
|
|
|
* @param messages List of messages to use for formatting.
|
|
|
|
*
|
|
|
|
* @return A string for the error.
|
|
|
|
*/
|
2016-08-06 16:46:54 +02:00
|
|
|
export function handleErrorI18nObject(errorMessage, formatMessage, messages) {
|
|
|
|
if (errorMessage instanceof i18nRecord) {
|
2016-08-10 21:36:11 +02:00
|
|
|
// If it is an object, format it and return it
|
2016-08-06 16:46:54 +02:00
|
|
|
return formatMessage(messages[errorMessage.id], errorMessage.values);
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
// Else, it's a string, just return it
|
2016-08-06 16:46:54 +02:00
|
|
|
return errorMessage;
|
|
|
|
}
|