@shopify/react-i18n
Advanced tools
Comparing version 0.10.5 to 0.11.0
@@ -11,1 +11,2 @@ export declare class MissingTranslationError extends Error { | ||
} | ||
export declare type I18nError = MissingTranslationError | MissingReplacementError | MissingCurrencyCodeError | MissingCountryError | InvalidI18nConnectionError; |
@@ -19,2 +19,3 @@ /// <reference types="react" /> | ||
readonly defaultTimezone?: string; | ||
readonly onError: NonNullable<I18nDetails['onError']>; | ||
readonly language: string; | ||
@@ -29,3 +30,3 @@ readonly region: string | undefined; | ||
readonly isLtrLanguage: boolean; | ||
constructor(translations: TranslationDictionary[], { locale, currency, timezone, country, pseudolocalize }: I18nDetails); | ||
constructor(translations: TranslationDictionary[], { locale, currency, timezone, country, pseudolocalize, onError, }: I18nDetails); | ||
translate(id: string, options: TranslateOptions, replacements?: PrimitiveReplacementDictionary): string; | ||
@@ -43,3 +44,3 @@ translate(id: string, options: TranslateOptions, replacements?: ComplexReplacementDictionary): React.ReactElement<any>; | ||
weekStartDay(argCountry?: I18n['defaultCountry']): Weekdays; | ||
getCurrencySymbol: (currencyCode?: string | undefined) => { | ||
getCurrencySymbol: (currencyCode?: string | undefined) => "" | { | ||
symbol: string; | ||
@@ -46,0 +47,0 @@ prefixed: boolean; |
@@ -15,3 +15,3 @@ "use strict"; | ||
function I18n(translations, _a) { | ||
var locale = _a.locale, currency = _a.currency, timezone = _a.timezone, country = _a.country, _b = _a.pseudolocalize, pseudolocalize = _b === void 0 ? false : _b; | ||
var locale = _a.locale, currency = _a.currency, timezone = _a.timezone, country = _a.country, _b = _a.pseudolocalize, pseudolocalize = _b === void 0 ? false : _b, onError = _a.onError; | ||
var _this = this; | ||
@@ -22,3 +22,4 @@ this.translations = translations; | ||
if (currency == null) { | ||
throw new errors_1.MissingCurrencyCodeError("No currency code provided. formatCurrency cannot be called without a currency code."); | ||
_this.onError(new errors_1.MissingCurrencyCodeError("No currency code provided. formatCurrency cannot be called without a currency code.")); | ||
return ''; | ||
} | ||
@@ -32,2 +33,3 @@ return _this.getCurrencySymbolLocalized(_this.locale, currency); | ||
this.pseudolocalize = pseudolocalize; | ||
this.onError = onError || defaultOnError; | ||
} | ||
@@ -96,3 +98,9 @@ Object.defineProperty(I18n.prototype, "language", { | ||
} | ||
return utilities_1.translate(id, normalizedOptions, this.translations, this.locale); | ||
try { | ||
return utilities_1.translate(id, normalizedOptions, this.translations, this.locale); | ||
} | ||
catch (error) { | ||
this.onError(error); | ||
return ''; | ||
} | ||
}; | ||
@@ -104,3 +112,4 @@ I18n.prototype.formatNumber = function (amount, _a) { | ||
if (as === 'currency' && currency == null && options.currency == null) { | ||
throw new errors_1.MissingCurrencyCodeError("No currency code provided. formatNumber(amount, {as: 'currency'}) cannot be called without a currency code."); | ||
this.onError(new errors_1.MissingCurrencyCodeError("No currency code provided. formatNumber(amount, {as: 'currency'}) cannot be called without a currency code.")); | ||
return ''; | ||
} | ||
@@ -214,1 +223,4 @@ return new Intl.NumberFormat(locale, tslib_1.__assign({ style: as, maximumFractionDigits: precision, currency: currency }, options)).format(amount); | ||
} | ||
function defaultOnError(error) { | ||
throw error; | ||
} |
@@ -132,3 +132,5 @@ "use strict"; | ||
} | ||
var possibleLocales = getPossibleLocales(this.details.locale); | ||
var possibleLocales = getPossibleLocales(this.details.locale, { | ||
exclude: this.details.fallbackLocale, | ||
}); | ||
var translations = possibleLocales.map(function (locale) { | ||
@@ -237,8 +239,16 @@ var id = localeId(connection, locale); | ||
} | ||
function getPossibleLocales(locale) { | ||
function getPossibleLocales(locale, _a) { | ||
var exclude = (_a === void 0 ? {} : _a).exclude; | ||
var normalizedLocale = locale.toLowerCase(); | ||
var split = normalizedLocale.split('-'); | ||
return split.length > 1 | ||
? [split[0] + "-" + split[1].toUpperCase(), normalizedLocale, split[0]] | ||
: [normalizedLocale]; | ||
if (split.length > 1) { | ||
var locales = [split[0] + "-" + split[1].toUpperCase(), normalizedLocale]; | ||
if (split[0] !== exclude) { | ||
locales.push(split[0]); | ||
} | ||
return locales; | ||
} | ||
else { | ||
return normalizedLocale === exclude ? [] : [normalizedLocale]; | ||
} | ||
} | ||
@@ -245,0 +255,0 @@ function isPromise(maybePromise) { |
/// <reference types="react" /> | ||
import { I18nError } from './errors'; | ||
export declare enum LanguageDirection { | ||
@@ -13,2 +14,3 @@ Rtl = 0, | ||
fallbackLocale?: string; | ||
onError?(error: I18nError): void; | ||
} | ||
@@ -15,0 +17,0 @@ export interface TranslationDictionary { |
@@ -114,3 +114,3 @@ "use strict"; | ||
if (!replacements.hasOwnProperty(replacement)) { | ||
throw new Error("No replacement found for key '" + replacement + "'. The following replacements were passed: " + Object.keys(replacements) | ||
throw new errors_1.MissingReplacementError("No replacement found for key '" + replacement + "'. The following replacements were passed: " + Object.keys(replacements) | ||
.map(function (key) { return "'" + key + "'"; }) | ||
@@ -117,0 +117,0 @@ .join(', ')); |
{ | ||
"name": "@shopify/react-i18n", | ||
"version": "0.10.5", | ||
"version": "0.11.0", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "i18n utilities for React handling translations, formatting, and more.", |
@@ -26,2 +26,3 @@ # `@shopify/react-i18n` | ||
- `pseudolocalize`: whether to perform [pseudolocalization](https://github.com/Shopify/pseudolocalization) on your translations. | ||
- `onError`: a callback to use when recoverable i18n-related errors happen. If not provided, these errors will be re-thrown wherever they occur. If it is provided and it does not re-throw the passed error, the translation or formatting that caused the error will return an empty string. This function will be called with the error object. | ||
@@ -35,3 +36,8 @@ ```ts | ||
const locale = 'en'; | ||
const i18nManager = new I18nManager({locale}); | ||
const i18nManager = new I18nManager({ | ||
locale, | ||
onError(error) { | ||
Bugsnag.notify(error); | ||
}, | ||
}); | ||
@@ -38,0 +44,0 @@ export default function App() { |
89943
1941
331