@kaliber/i18n
Advanced tools
Comparing version
15
index.js
const i18nContext = React.createContext(null) | ||
const defaultLogMissingTranslation = ({ language, path }) => { | ||
console.warn(`Missing translation (${language}): ${path}`) | ||
} | ||
export function useI18n(...i18nPathSegments) { | ||
const context = React.useContext(i18nContext) | ||
if (context === null) throwMissingContextError() | ||
const { value, language } = context | ||
const { value, language, logMissingTranslation } = context | ||
@@ -13,3 +17,3 @@ return (...pathSegments) => { | ||
if (process.env.NODE_ENV !== 'production' && typeof result === 'undefined') { | ||
console.warn(`Missing translation (${language}): %c${path}`, 'font-weight: bold;') | ||
logMissingTranslation({ language, path }) | ||
} | ||
@@ -27,4 +31,7 @@ | ||
export function I18nProvider({ value, language, children }) { | ||
const providerValue = React.useMemo(() => ({ value, language }), [value, language]) | ||
export function I18nProvider({ value, language, logMissingTranslation = defaultLogMissingTranslation, children }) { | ||
const providerValue = React.useMemo( | ||
() => ({ value, language, logMissingTranslation }), | ||
[value, language, logMissingTranslation] | ||
) | ||
@@ -31,0 +38,0 @@ return ( |
{ | ||
"name": "@kaliber/i18n", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
@@ -149,2 +149,3 @@ # i18n | ||
| `language` | _Optional._ <br />When a language is given, normalization will be attempted at the leave nodes. E.g.: given the language `en` this means a leafnode with the shape `{ en: 'countries', nl: 'landen' }` will be normalized to just `'countries'`. | | ||
| `logMissingTranslation` | _Optional._ (default: `console.warn`)<br />If you want to change how missing translations are logged, you can provide your own `logMissingTranslation` function. This function is called with an object of the shape: `{ language, path }`. | | ||
@@ -151,0 +152,0 @@ The provided i18n object may be a deeply nested object, optionally containing arrays. Values don't have to be strings, you can also provide numbers, functions or React elements (see the example i18n object under [usage](#usage)) |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
9592
5.42%69
9.52%0
-100%200
0.5%