handsontable
Version:
Handsontable is a JavaScript Data Grid available for React, Angular and Vue.
167 lines (154 loc) • 6.88 kB
JavaScript
exports.__esModule = true;
exports.getDefaultLanguageDictionary = getDefaultLanguageDictionary;
exports.getLanguageDictionary = getLanguageDictionary;
exports.getLanguagesDictionaries = getLanguagesDictionaries;
exports.getTranslatedPhrase = getTranslatedPhrase;
exports.getValidLanguageCode = getValidLanguageCode;
exports.hasLanguageDictionary = hasLanguageDictionary;
exports.registerLanguageDictionary = registerLanguageDictionary;
require("core-js/modules/esnext.iterator.constructor.js");
require("core-js/modules/esnext.iterator.reduce.js");
var _object = require("../helpers/object");
var _mixed = require("../helpers/mixed");
var _utils = require("./utils");
var _staticRegister = require("../utils/staticRegister");
var _phraseFormatters = require("./phraseFormatters");
var _enUS = _interopRequireDefault(require("./languages/en-US"));
var _dictionaryKeys = _interopRequireWildcard(require("./constants"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
const dictionaryKeys = exports.dictionaryKeys = _dictionaryKeys;
const DEFAULT_LANGUAGE_CODE = exports.DEFAULT_LANGUAGE_CODE = _enUS.default.languageCode;
const {
register: registerGloballyLanguageDictionary,
getItem: getGlobalLanguageDictionary,
hasItem: hasGlobalLanguageDictionary,
getValues: getGlobalLanguagesDictionaries
} = (0, _staticRegister.staticRegister)('languagesDictionaries');
/**
* Register automatically the default language dictionary.
*/
registerLanguageDictionary(_enUS.default);
/**
* Register language dictionary for specific language code.
*
* @param {string|object} languageCodeOrDictionary Language code for specific language i.e. 'en-US', 'pt-BR', 'de-DE' or object representing dictionary.
* @param {object} dictionary Dictionary for specific language (optional if first parameter has already dictionary).
* @returns {object}
*/
function registerLanguageDictionary(languageCodeOrDictionary, dictionary) {
let languageCode = languageCodeOrDictionary;
let dictionaryObject = dictionary;
// Dictionary passed as first argument.
if ((0, _object.isObject)(languageCodeOrDictionary)) {
dictionaryObject = languageCodeOrDictionary;
languageCode = dictionaryObject.languageCode;
}
extendLanguageDictionary(languageCode, dictionaryObject);
registerGloballyLanguageDictionary(languageCode, (0, _object.deepClone)(dictionaryObject));
// We do not allow user to work with dictionary by reference, it can cause lot of bugs.
return (0, _object.deepClone)(dictionaryObject);
}
/**
* Extend handled dictionary by default language dictionary. As result, if any dictionary key isn't defined for specific language, it will be filled with default language value ("dictionary gaps" are supplemented).
*
* @private
* @param {string} languageCode Language code.
* @param {object} dictionary Dictionary which is extended.
*/
function extendLanguageDictionary(languageCode, dictionary) {
if (languageCode !== DEFAULT_LANGUAGE_CODE) {
(0, _utils.extendNotExistingKeys)(dictionary, getGlobalLanguageDictionary(DEFAULT_LANGUAGE_CODE));
}
}
/**
* Get language dictionary for specific language code.
*
* @param {string} languageCode Language code.
* @returns {object} Object with constants representing identifiers for translation (as keys) and corresponding translation phrases (as values).
*/
function getLanguageDictionary(languageCode) {
if (!hasLanguageDictionary(languageCode)) {
return null;
}
return (0, _object.deepClone)(getGlobalLanguageDictionary(languageCode));
}
/**
*
* Get if language with specified language code was registered.
*
* @param {string} languageCode Language code for specific language i.e. 'en-US', 'pt-BR', 'de-DE'.
* @returns {boolean}
*/
function hasLanguageDictionary(languageCode) {
return hasGlobalLanguageDictionary(languageCode);
}
/**
* Get default language dictionary.
*
* @returns {object} Object with constants representing identifiers for translation (as keys) and corresponding translation phrases (as values).
*/
function getDefaultLanguageDictionary() {
return _enUS.default;
}
/**
* Get registered language dictionaries.
*
* @returns {Array}
*/
function getLanguagesDictionaries() {
return getGlobalLanguagesDictionaries();
}
/**
* Get phrase for specified dictionary key.
*
* @param {string} languageCode Language code for specific language i.e. 'en-US', 'pt-BR', 'de-DE'.
* @param {string} dictionaryKey Constant which is dictionary key.
* @param {*} argumentsForFormatters Arguments which will be handled by formatters.
*
* @returns {string}
*/
function getTranslatedPhrase(languageCode, dictionaryKey, argumentsForFormatters) {
const languageDictionary = getLanguageDictionary(languageCode);
if (languageDictionary === null) {
return null;
}
const phrasePropositions = languageDictionary[dictionaryKey];
if ((0, _mixed.isUndefined)(phrasePropositions)) {
return null;
}
const formattedPhrase = getFormattedPhrase(phrasePropositions, argumentsForFormatters);
if (Array.isArray(formattedPhrase)) {
return formattedPhrase[0];
}
return formattedPhrase;
}
/**
* Get formatted phrase from phrases propositions for specified dictionary key.
*
* @private
* @param {Array|string} phrasePropositions List of phrase propositions.
* @param {*} argumentsForFormatters Arguments which will be handled by formatters.
*
* @returns {Array|string}
*/
function getFormattedPhrase(phrasePropositions, argumentsForFormatters) {
return (0, _phraseFormatters.getPhraseFormatters)().reduce((phrase, formatter) => {
return formatter(phrase, argumentsForFormatters);
}, phrasePropositions);
}
/**
* Returns valid language code. If the passed language code doesn't exist default one will be used.
*
* @param {string} languageCode Language code for specific language i.e. 'en-US', 'pt-BR', 'de-DE'.
* @returns {string}
*/
function getValidLanguageCode(languageCode) {
let normalizedLanguageCode = (0, _utils.normalizeLanguageCode)(languageCode);
if (!hasLanguageDictionary(normalizedLanguageCode)) {
normalizedLanguageCode = DEFAULT_LANGUAGE_CODE;
(0, _utils.warnUserAboutLanguageRegistration)(languageCode);
}
return normalizedLanguageCode;
}
;