react-i18nify
Advanced tools
Comparing version 4.2.1 to 4.3.0
@@ -6,6 +6,6 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "Translate", { | ||
Object.defineProperty(exports, "I18n", { | ||
enumerable: true, | ||
get: function get() { | ||
return _Translate.default; | ||
return _I18n.default; | ||
} | ||
@@ -19,6 +19,6 @@ }); | ||
}); | ||
Object.defineProperty(exports, "I18n", { | ||
Object.defineProperty(exports, "Translate", { | ||
enumerable: true, | ||
get: function get() { | ||
return _I18n.default; | ||
return _Translate.default; | ||
} | ||
@@ -38,36 +38,36 @@ }); | ||
}); | ||
Object.defineProperty(exports, "getLocale", { | ||
Object.defineProperty(exports, "forceComponentsUpdate", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.getLocale; | ||
return _core.forceComponentsUpdate; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setLocale", { | ||
Object.defineProperty(exports, "getLocale", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.setLocale; | ||
return _core.getLocale; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setLocaleGetter", { | ||
Object.defineProperty(exports, "getTranslations", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.setLocaleGetter; | ||
return _core.getTranslations; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getTranslations", { | ||
Object.defineProperty(exports, "l", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.getTranslations; | ||
return _core.localize; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setTranslations", { | ||
Object.defineProperty(exports, "localize", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.setTranslations; | ||
return _core.localize; | ||
} | ||
}); | ||
Object.defineProperty(exports, "setTranslationsGetter", { | ||
Object.defineProperty(exports, "setHandleFailedLocalization", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.setTranslationsGetter; | ||
return _core.setHandleFailedLocalization; | ||
} | ||
@@ -81,32 +81,38 @@ }); | ||
}); | ||
Object.defineProperty(exports, "translate", { | ||
Object.defineProperty(exports, "setLocale", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.translate; | ||
return _core.setLocale; | ||
} | ||
}); | ||
Object.defineProperty(exports, "localize", { | ||
Object.defineProperty(exports, "setLocaleGetter", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.localize; | ||
return _core.setLocaleGetter; | ||
} | ||
}); | ||
Object.defineProperty(exports, "t", { | ||
Object.defineProperty(exports, "setTranslations", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.translate; | ||
return _core.setTranslations; | ||
} | ||
}); | ||
Object.defineProperty(exports, "l", { | ||
Object.defineProperty(exports, "setTranslationsGetter", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.localize; | ||
return _core.setTranslationsGetter; | ||
} | ||
}); | ||
Object.defineProperty(exports, "forceComponentsUpdate", { | ||
Object.defineProperty(exports, "t", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.forceComponentsUpdate; | ||
return _core.translate; | ||
} | ||
}); | ||
Object.defineProperty(exports, "translate", { | ||
enumerable: true, | ||
get: function get() { | ||
return _core.translate; | ||
} | ||
}); | ||
Object.defineProperty(exports, "translateReplace", { | ||
@@ -113,0 +119,0 @@ enumerable: true, |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.forceComponentsUpdate = exports.localize = exports.translate = exports.setHandleMissingTranslation = exports.setTranslationsGetter = exports.setLocaleGetter = exports.setTranslations = exports.getTranslations = exports.addLocales = exports.addLocale = exports.setLocale = exports.getLocale = exports.settings = void 0; | ||
exports.translate = exports.settings = exports.setTranslationsGetter = exports.setTranslations = exports.setLocaleGetter = exports.setLocale = exports.setHandleMissingTranslation = exports.setHandleFailedLocalization = exports.localize = exports.getTranslations = exports.getLocale = exports.forceComponentsUpdate = exports.addLocales = exports.addLocale = void 0; | ||
@@ -36,2 +36,5 @@ var _parse = _interopRequireDefault(require("date-fns/parse")); | ||
}, | ||
handleFailedLocalization: function handleFailedLocalization() { | ||
return null; | ||
}, | ||
@@ -140,2 +143,12 @@ get translations() { | ||
var setHandleFailedLocalization = function setHandleFailedLocalization(fn) { | ||
if (typeof fn !== 'function') { | ||
throw new Error('Handle failed localization must be a function'); | ||
} | ||
settings.handleFailedLocalization = fn; | ||
}; | ||
exports.setHandleFailedLocalization = setHandleFailedLocalization; | ||
var translate = function translate(key) { | ||
@@ -153,3 +166,3 @@ var replacements = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if (options.returnKeyOnError) return key; | ||
return settings.handleMissingTranslation(key, replacements); | ||
return settings.handleMissingTranslation(key, replacements, options, err); | ||
} | ||
@@ -190,4 +203,4 @@ | ||
}); | ||
} catch (e) { | ||
return e.message; | ||
} catch (err) { | ||
return settings.handleFailedLocalization(value, options, err); | ||
} | ||
@@ -199,4 +212,4 @@ } | ||
return new Intl.NumberFormat(locale, options).format(value); | ||
} catch (e) { | ||
return e.message; | ||
} catch (err) { | ||
return settings.handleFailedLocalization(value, options, err); | ||
} | ||
@@ -203,0 +216,0 @@ } |
@@ -6,3 +6,3 @@ "use strict"; | ||
}); | ||
exports.fetchTranslation = exports.replace = void 0; | ||
exports.replace = exports.fetchTranslation = void 0; | ||
@@ -9,0 +9,0 @@ var _react = _interopRequireDefault(require("react")); |
@@ -14,3 +14,3 @@ const React = require('react'); | ||
const { | ||
addLocales, setTranslations, setLocale, setHandleMissingTranslation, translate, localize, Translate, Localize, I18n, | ||
addLocales, setTranslations, setLocale, setHandleMissingTranslation, setHandleFailedLocalization, translate, localize, Translate, Localize, I18n, | ||
} = ReactI18nfiy; | ||
@@ -55,7 +55,12 @@ | ||
console.log(localize(Math.PI, { maximumFractionDigits: 2 })); | ||
console.log(localize('huh', { dateFormat: 'date.long' })); | ||
setHandleMissingTranslation((key, replacements) => `Missing translation: ${key}`); | ||
setHandleMissingTranslation((key, replacements, options, err) => `Missing translation: ${key}`); | ||
console.log(translate('application.unknown_translation')); | ||
setHandleFailedLocalization((value, options, err) => `Failed localization: ${value}`); | ||
console.log(localize('huh', { dateFormat: 'date.long' })); | ||
function AwesomeComponent() { | ||
@@ -62,0 +67,0 @@ return ( |
{ | ||
"name": "react-i18nify", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"description": "Simple i18n translation and localization components and helpers for React.", | ||
@@ -49,29 +49,29 @@ "main": "./build/index.js", | ||
"dependencies": { | ||
"date-fns": "^2.24.0", | ||
"date-fns": "^2.25.0", | ||
"prop-types": "^15.7.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.15.4", | ||
"@babel/core": "^7.15.5", | ||
"@babel/cli": "^7.15.7", | ||
"@babel/core": "^7.15.8", | ||
"@babel/plugin-proposal-class-properties": "^7.13.0", | ||
"@babel/preset-env": "^7.15.4", | ||
"@babel/preset-env": "^7.15.8", | ||
"@babel/preset-react": "^7.12.13", | ||
"@babel/register": "^7.15.3", | ||
"@types/react": "^17.0.21", | ||
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.3", | ||
"@types/react": "^17.0.33", | ||
"@wojtekmaj/enzyme-adapter-react-17": "^0.6.5", | ||
"babel-eslint": "^10.1.0", | ||
"dtslint": "^4.1.6", | ||
"dtslint": "^4.2.0", | ||
"enzyme": "^3.11.0", | ||
"eslint": "^7.21.0", | ||
"eslint-config-airbnb": "^18.2.1", | ||
"eslint-plugin-import": "^2.24.2", | ||
"eslint-plugin-import": "^2.25.2", | ||
"eslint-plugin-jsx-a11y": "^6.4.1", | ||
"eslint-plugin-react": "^7.25.2", | ||
"eslint-plugin-react": "^7.26.1", | ||
"eslint-plugin-react-hooks": "^4.2.0", | ||
"jest": "^27.2.0", | ||
"jest": "^27.3.1", | ||
"react": "^17.0.2", | ||
"react-dom": "^17.0.2", | ||
"rimraf": "^3.0.2", | ||
"typescript": "^4.4.3" | ||
"typescript": "^4.4.4" | ||
} | ||
} |
@@ -107,2 +107,4 @@ # React I18nify | ||
// => 3,14 | ||
localize('huh', { dateFormat: 'date.long' }); | ||
// => 1 december 2013 | ||
``` | ||
@@ -261,3 +263,3 @@ | ||
setHandleMissingTranslation((key, replacements) => `Missing translation: ${key}`); | ||
setHandleMissingTranslation((key, replacements, options, err) => `Missing translation: ${key}`); | ||
@@ -268,2 +270,18 @@ translate('application.unknown_translation'); | ||
### `setHandleFailedLocalization(fn)` | ||
By default, when a localization failed, `null` will be returned, | ||
as can be seen in the `localize('huh', { dateFormat: 'date.long' });` example above. | ||
You can however overwrite this behavior by setting a function to handle failed localizations. | ||
```javascript | ||
import { setHandleFailedLocalization, localize } from 'react-i18nify'; | ||
setHandleFailedLocalization((value, options, err) => `Failed localization: ${value}`); | ||
localize('huh', { dateFormat: 'date.long' }); | ||
// => Failed localization: huh | ||
``` | ||
### `translate(key, replacements = {})` | ||
@@ -270,0 +288,0 @@ |
@@ -14,2 +14,3 @@ export { default as Translate } from './lib/Translate'; | ||
setHandleMissingTranslation, | ||
setHandleFailedLocalization, | ||
translate, | ||
@@ -16,0 +17,0 @@ localize, |
@@ -17,2 +17,3 @@ /* eslint global-require: "off" */ | ||
handleMissingTranslation: (text) => text.split('.').pop(), | ||
handleFailedLocalization: () => null, | ||
@@ -93,2 +94,9 @@ get translations() { | ||
export const setHandleFailedLocalization = (fn) => { | ||
if (typeof fn !== 'function') { | ||
throw new Error('Handle failed localization must be a function'); | ||
} | ||
settings.handleFailedLocalization = fn; | ||
}; | ||
export const translate = (key, replacements = {}, options = {}) => { | ||
@@ -105,3 +113,3 @@ const locale = options.locale || settings.locale; | ||
if (options.returnKeyOnError) return key; | ||
return settings.handleMissingTranslation(key, replacements); | ||
return settings.handleMissingTranslation(key, replacements, options, err); | ||
} | ||
@@ -135,4 +143,4 @@ return replace(translation, replacements); | ||
); | ||
} catch (e) { | ||
return e.message; | ||
} catch (err) { | ||
return settings.handleFailedLocalization(value, options, err); | ||
} | ||
@@ -143,4 +151,4 @@ } | ||
return new Intl.NumberFormat(locale, options).format(value); | ||
} catch (e) { | ||
return e.message; | ||
} catch (err) { | ||
return settings.handleFailedLocalization(value, options, err); | ||
} | ||
@@ -147,0 +155,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58906
989
320
Updateddate-fns@^2.25.0