From 5728f3d7bc38c4055dd5b427b9afbeef27c25afb Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Fri, 8 Jan 2021 13:06:07 +0100 Subject: [PATCH 1/6] callback for untranslated --- README.md | 16 ++++++++++++++++ i18n.js | 17 +++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index edefd98..cfab531 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,22 @@ fallbackLocales: { 👀 `eleventy-plugin-i18n` will warn you in the Node console when the intended translation or fallback values can't be found for a given language based on your `translations` data. +#### `notFoundCallback` + +Type: `Function` | Default: ‌`` + +If a matching translation can not be found at all, you may specify a +callback function that deals with the situation. It is invoked with +(key, locale) and may return a +string to be used as a translation. This callback function may be +useful to detect and record untranslated strings. + +```js +notFoundCallBack: (key, locale) => {saveToFile(key,locale); 'Needs translation:'+key} +``` + +👀 `eleventy-plugin-i18n` will warn you in the Node console when the intended translation or fallback values can't be found for a given language based on your `translations` data. + ## Usage Once configured, the `i18n` [Universal filter](https://www.11ty.dev/docs/filters/#universal-filters) is available throughout Nunjucks, Handlebars, Liquid, and JavaScript templates and includes. E.g. To return the translation for our `hello` key in Nunjucks or Liquid syntax: diff --git a/i18n.js b/i18n.js index 4a0b7f2..1864110 100644 --- a/i18n.js +++ b/i18n.js @@ -13,7 +13,8 @@ module.exports = function ( ) { const { translations = {}, - fallbackLocales: fallbackLocales = {} + fallbackLocales: fallbackLocales = {}, + notFoundCallback } = pluginOptions; // Use explicit `locale` argument if passed in, otherwise infer it from URL prefix segment @@ -43,10 +44,14 @@ module.exports = function ( } // Not found - console.warn( - chalk.red( - `[i18n] Translation for '${key}' in '${locale}' not found. No fallback locale specified.` - ) - ); + if (notFoundCallback) { + key = notFoundCallback(key, locale) || key; + } else { + console.warn( + chalk.red( + `[i18n] Translation for '${key}' in '${locale}' not found. No fallback locale specified.` + ) + ); + } return key; }; From 164212b2537c7388ef99ddb89bd84eebcb91a524 Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Fri, 8 Jan 2021 13:08:10 +0100 Subject: [PATCH 2/6] cleanup notFoundCallback docs --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index cfab531..0b864d2 100644 --- a/README.md +++ b/README.md @@ -155,8 +155,6 @@ useful to detect and record untranslated strings. notFoundCallBack: (key, locale) => {saveToFile(key,locale); 'Needs translation:'+key} ``` -👀 `eleventy-plugin-i18n` will warn you in the Node console when the intended translation or fallback values can't be found for a given language based on your `translations` data. - ## Usage Once configured, the `i18n` [Universal filter](https://www.11ty.dev/docs/filters/#universal-filters) is available throughout Nunjucks, Handlebars, Liquid, and JavaScript templates and includes. E.g. To return the translation for our `hello` key in Nunjucks or Liquid syntax: From 71e72fa1b8e1278d90642cdb790c3811e78166d2 Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Sun, 10 Jan 2021 12:27:55 +0100 Subject: [PATCH 3/6] improved docs for notFoundCallback --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b864d2..efd1c01 100644 --- a/README.md +++ b/README.md @@ -152,7 +152,10 @@ string to be used as a translation. This callback function may be useful to detect and record untranslated strings. ```js -notFoundCallBack: (key, locale) => {saveToFile(key,locale); 'Needs translation:'+key} +notFoundCallBack: (key, locale) => { + saveToNeedsTranslationFile(key, locale); + return('Needs translation:' + key); +} ``` ## Usage From 23630c08817ee8a76d554c11d9bcecd1306d6893 Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Sun, 10 Jan 2021 12:53:29 +0100 Subject: [PATCH 4/6] allow alternative lookupFn --- README.md | 25 +++++++++++++++++++++++++ i18n.js | 5 +++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index efd1c01..ba4637d 100644 --- a/README.md +++ b/README.md @@ -158,6 +158,31 @@ notFoundCallBack: (key, locale) => { } ``` +#### `lookupFn` + +Type: `Function` | Default: ‌lodash.get `(key, locale, translations) => get(translations, `[${key}][${locale}]`)` + +Allows changing the behaviour from the default, which is to use a +translation objects where `locale` is at the end of the chain, and + [lodash's `get`](https://lodash.com/docs/#get) for dot notation + lookups. + +The lookupFn is invoked with (key, locale, translations) and returns a +matching translated string. This callback function may be useful to take +notice of what translations are actually in use, or it may be useful +to specify a simpler lookup behaviour: + +```js +lookupFn : (key, locale, translations) => { + // Allow to use dots as keys in + // simple key/value translation object + const values = translations[key]; + if (values) { + return values[locale]; + } +} + + ## Usage Once configured, the `i18n` [Universal filter](https://www.11ty.dev/docs/filters/#universal-filters) is available throughout Nunjucks, Handlebars, Liquid, and JavaScript templates and includes. E.g. To return the translation for our `hello` key in Nunjucks or Liquid syntax: diff --git a/i18n.js b/i18n.js index 1864110..2cd126a 100644 --- a/i18n.js +++ b/i18n.js @@ -14,6 +14,7 @@ module.exports = function ( const { translations = {}, fallbackLocales: fallbackLocales = {}, + lookupFn : lookupFn = (key, locale, translations) => get(translations, `[${key}][${locale}]`), notFoundCallback } = pluginOptions; @@ -23,7 +24,7 @@ module.exports = function ( const locale = localeOverride || contextLocale; // Preferred translation - const translation = get(translations, `[${key}][${locale}]`); + const translation = lookupFn(key, locale, translations); if (translation !== undefined) { return templite(translation, data); @@ -32,7 +33,7 @@ module.exports = function ( // Fallback translation const fallbackLocale = get(fallbackLocales, locale) || get(fallbackLocales, '*'); - const fallbackTranslation = get(translations, `[${key}][${fallbackLocale}]`); + const fallbackTranslation = lookupFn(key, fallbackLocale, translations); if (fallbackTranslation !== undefined) { console.warn( From 5189333385ec7b7ea237bdfb3d852ec9d017d10f Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Sun, 10 Jan 2021 18:16:21 +0100 Subject: [PATCH 5/6] forgot ending backquotes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ba4637d..af67fe0 100644 --- a/README.md +++ b/README.md @@ -181,7 +181,7 @@ lookupFn : (key, locale, translations) => { return values[locale]; } } - +``` ## Usage From 45c132f150f03fbbebaedea0fc712156b3e0825d Mon Sep 17 00:00:00 2001 From: Henrik Hjelte Date: Sat, 6 Feb 2021 23:04:45 +0100 Subject: [PATCH 6/6] more way to control behaviour --- README.md | 10 ++++++++-- i18n.js | 30 ++++++++++++++++++++---------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index af67fe0..55ae5d9 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ Type: `Function` | Default: ‌`` If a matching translation can not be found at all, you may specify a callback function that deals with the situation. It is invoked with -(key, locale) and may return a +(key, locale, data) and may return a string to be used as a translation. This callback function may be useful to detect and record untranslated strings. @@ -167,7 +167,7 @@ translation objects where `locale` is at the end of the chain, and [lodash's `get`](https://lodash.com/docs/#get) for dot notation lookups. -The lookupFn is invoked with (key, locale, translations) and returns a +The lookupFn is invoked with (key, locale, translations, data) and returns a matching translated string. This callback function may be useful to take notice of what translations are actually in use, or it may be useful to specify a simpler lookup behaviour: @@ -182,6 +182,12 @@ lookupFn : (key, locale, translations) => { } } ``` +#### `useTemplite` + +Type: `Boolean` | Default: ‌`true` + +Defines if templite feature is enabled (see data below). If not, data +can be used as an extra parameter passed onto the lookupFn. ## Usage diff --git a/i18n.js b/i18n.js index 2cd126a..1f28bba 100644 --- a/i18n.js +++ b/i18n.js @@ -15,7 +15,9 @@ module.exports = function ( translations = {}, fallbackLocales: fallbackLocales = {}, lookupFn : lookupFn = (key, locale, translations) => get(translations, `[${key}][${locale}]`), - notFoundCallback + useTemplite = true, + notFoundCallback, + silent = false } = pluginOptions; // Use explicit `locale` argument if passed in, otherwise infer it from URL prefix segment @@ -24,24 +26,32 @@ module.exports = function ( const locale = localeOverride || contextLocale; // Preferred translation - const translation = lookupFn(key, locale, translations); + const translation = lookupFn(key, locale, translations, data); if (translation !== undefined) { - return templite(translation, data); + if (useTemplite) { + return templite(translation, data); + } + return translation; } // Fallback translation const fallbackLocale = get(fallbackLocales, locale) || get(fallbackLocales, '*'); - const fallbackTranslation = lookupFn(key, fallbackLocale, translations); + const fallbackTranslation = lookupFn(key, fallbackLocale, translations, data); if (fallbackTranslation !== undefined) { - console.warn( - chalk.yellow( - `[i18n] Could not find '${key}' in '${locale}'. Using '${fallbackLocale}' fallback.` - ) - ); - return templite(fallbackTranslation, data); + if (!silent) { + console.warn( + chalk.yellow( + `[i18n] Could not find '${key}' in '${locale}'. Using '${fallbackLocale}' fallback.` + ) + ); + } + if (useTemplite) { + return templite(fallbackTranslation, data); + } + return fallbackTranslation; } // Not found