@procore/core-react
Version:
React library of Procore Design Guidelines
195 lines (182 loc) • 9.7 kB
JavaScript
var _excluded = ["children"];
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { getFallbackList, isCDNFeatureFlagEnabled, useRequestTranslations } from '@procore/cdn-translations';
import { createI18njs } from '@procore/core-i18n-js';
import * as R from 'ramda';
import React from 'react';
import deDE from '../_locales/de-DE.json';
import enAU from '../_locales/en-AU.json';
import enCA from '../_locales/en-CA.json';
import enGB from '../_locales/en-GB.json';
import en from '../_locales/en.json';
import esES from '../_locales/es-ES.json';
import es from '../_locales/es.json';
import frCA from '../_locales/fr-CA.json';
import frFR from '../_locales/fr-FR.json';
import isIS from '../_locales/is-IS.json';
import itIT from '../_locales/it-IT.json';
import jaJP from '../_locales/ja-JP.json';
import nbNO from '../_locales/nb-NO.json';
import plPL from '../_locales/pl-PL.json';
import pseudo from '../_locales/pseudo.json';
import ptBR from '../_locales/pt-BR.json';
import ptPT from '../_locales/pt-PT.json';
import thTH from '../_locales/th-TH.json';
import zhSG from '../_locales/zh-SG.json';
import zhTW from '../_locales/zh-TW.json';
import { OVERWRITE_LOCALE_MAP } from './getI18nProviderLocale';
// Incase of error, Make sure I18njsConfig['locale'] has the same locales as Locale
/**
* @deprecated this type will no longer be supported from core-i18n-js,
* to have a type safe Locale use TmsSupportedLocale or AllLocales from [Globalization toolkit](https://github.com/procore/globalization-toolkit)
* @deprecatedSince 12.10.0
*/
/* Set up core translations */
export var coreTranslations = {
'de-DE': deDE,
en: en,
'en-AU': enAU,
'en-CA': enCA,
'en-GB': enGB,
es: es,
'es-ES': esES,
'fr-CA': frCA,
'fr-FR': frFR,
'is-IS': isIS,
'ja-JP': jaJP,
pseudo: pseudo,
'pt-BR': ptBR,
'th-TH': thTH,
'zh-SG': zhSG,
'pl-PL': plPL,
'nb-NO': nbNO,
'zh-TW': zhTW,
'it-IT': itIT,
'pt-PT': ptPT
};
var FALLBACK_LOCALE_MAP = _objectSpread({
'es-ES': 'es',
'fr-FR': 'fr-CA'
}, OVERWRITE_LOCALE_MAP);
function getTranslationFallbacks(locale) {
if (FALLBACK_LOCALE_MAP[locale]) {
return [FALLBACK_LOCALE_MAP[locale], 'en'];
}
return ['en'];
}
function setTranslationFallbacks(I18n, isCDNEnabled) {
if (isCDNEnabled === true) {
I18n.locales[I18n.locale] = function () {
return getFallbackList(I18n.locale);
};
} else {
I18n.locales[I18n.locale] = function () {
var fallbacks = getTranslationFallbacks(I18n.locale).filter(function (locale) {
return locale !== I18n.locale;
});
return [I18n.locale].concat(_toConsumableArray(fallbacks));
};
}
return I18n;
}
/**
* Hook to create I18n object.
* @param {UseI18n} props - The I18n configuration.
* @param {string} props.locale - The locale to use.
* @param {Record<string, string>} props.translations - The translations to use.
* @param {boolean} props.enableCDN - Whether to pull core-react translations from the CDN or not, by default it follows the value passed down through the parent I18nProvider.
* @returns {I18nWithCDNFlag} The I18n object.
*/
export function useI18n(_ref) {
var locale = _ref.locale,
_ref$translations = _ref.translations,
_translations = _ref$translations === void 0 ? {} : _ref$translations,
enableCDN = _ref.enableCDN;
var i18nContext = React.useContext(I18nContext);
var isCDNEnabled = isCDNFeatureFlagEnabled(i18nContext, enableCDN);
var coreCDNTranslations = useRequestTranslations({
locale: locale !== null && locale !== void 0 ? locale : 'en',
type: 'file',
absolute_file_path: function absolute_file_path(locale) {
return "core/packages/core-react/src/_locales/".concat(locale, ".json");
}
}, {
en: en,
pseudo: pseudo
}, {
oldTranslations: coreTranslations,
enableCDN: isCDNEnabled
});
var translations = R.mergeDeepLeft(_translations, coreCDNTranslations.translations);
var I18n = createI18njs({
locale: locale,
translations: translations
});
var I18nWithFallbacks = setTranslationFallbacks(I18n, isCDNEnabled);
I18nWithFallbacks.enableCDN = isCDNEnabled;
return I18nWithFallbacks;
}
export var I18nContext = /*#__PURE__*/React.createContext(createI18njs({
locale: 'en',
translations: {
en: coreTranslations.en
}
}));
/**
* Provider to wrap the application with the I18n context.
* @param {PropsWithChildren<UseI18n>} props - The I18n configuration.
* @param {React.ReactNode} props.children - The children to render.
* @param {UseI18n} props.locale - The locale to use.
* @param {Record<string, string>} props.translations - The translations to use.
* @param {boolean} props.enableCDN - Whether to pull core-react translations from the CDN or not, by default it follows the value passed down through the parent I18nProvider.
* @returns {React.ReactNode} The I18n context instance.
*/
export function I18nProvider(_ref2) {
var children = _ref2.children,
props = _objectWithoutProperties(_ref2, _excluded);
return /*#__PURE__*/React.createElement(I18nContext.Provider, {
value: useI18n(props)
}, children);
}
// Base type of user provider translations type
// Type to get nested key paths with dot notation
// Type to get the literal string type from a path
// Check if a string has any interpolation variables
// Extract interpolation variables from a string
// always allowed values in the interpolation object (remove [key: string]: any in favor of our strong types)
// overload for when no generic is provided
/**
* Hook to access the I18n context.
*
* @template Translations - An optional generic to provide type safety for translations.
* If specified, it enables typed access to the `t` and `translate` methods of the I18n instance.
* These methods will have type safe access to both the tranlsation scope as well as the interpolation variables.
* If not specified, the default I18nWithCDN instance is returned.
*
* @returns {I18nWithCDNFlag | I18njsWithTypedTranslate<Translations>} The I18n context instance,
* either with or without typed translations based on the provided generic.
*/
export function useI18nContext() {
var ctx = React.useContext(I18nContext);
if (!ctx) {
// TODO investigate a way to do warnings that won't annoy people
// we probably need to introduce a build system to have nice dev warnings
// console.warn()
}
// Cast to typing if user has provided a type
return ctx;
}
//# sourceMappingURL=I18n.js.map