Skip to content

Commit

Permalink
refactor(i18nHelpers): sw-922 translate break apart, testing (#1079)
Browse files Browse the repository at this point in the history
* helpers, move noopTranslate
* i18nHelpers, break apart for testing, noopTranslate
  • Loading branch information
cdcabrera committed Mar 21, 2023
1 parent e0ed6cb commit 617d1a5
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 104 deletions.
24 changes: 0 additions & 24 deletions src/common/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ Download the debug log file.
* [~isPromise(obj)](#Helpers.module_General..isPromise) ⇒ <code>boolean</code>
* [~generateHash(anyValue, options)](#Helpers.module_General..generateHash) ⇒ <code>\*</code> \| <code>string</code>
* [~memo(func, options)](#Helpers.module_General..memo) ⇒ <code>function</code>
* [~noopTranslate(key, value, components)](#Helpers.module_General..noopTranslate) ⇒ <code>string</code>
* [~numberDisplay(value)](#Helpers.module_General..numberDisplay) ⇒ <code>numbro.Numbro</code> \| <code>\*</code>
* [~objFreeze(obj)](#Helpers.module_General..objFreeze) ⇒ <code>\*</code>
* [~browserExpose(obj, options)](#Helpers.module_General..browserExpose)
Expand Down Expand Up @@ -529,29 +528,6 @@ Simple memoize, cache based arguments with adjustable limit.
</tr> </tbody>
</table>

<a name="Helpers.module_General..noopTranslate"></a>

### General~noopTranslate(key, value, components) ⇒ <code>string</code>
A placeholder for "t", translation method.
Associated with the i18n package, and typically used as a default prop.

**Kind**: inner method of [<code>General</code>](#Helpers.module_General)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>key</td><td><code>string</code> | <code>Array</code></td>
</tr><tr>
<td>value</td><td><code>string</code> | <code>object</code> | <code>Array</code></td>
</tr><tr>
<td>components</td><td><code>Array</code></td>
</tr> </tbody>
</table>

<a name="Helpers.module_General..numberDisplay"></a>

### General~numberDisplay(value) ⇒ <code>numbro.Numbro</code> \| <code>\*</code>
Expand Down
3 changes: 0 additions & 3 deletions src/common/__tests__/__snapshots__/helpers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ exports[`Helpers should expose a window object: limited window object 1`] = `
"memo": [Function],
"noop": [Function],
"noopPromise": Promise {},
"noopTranslate": [Function],
"numberDisplay": [Function],
"objFreeze": [Function],
}
Expand Down Expand Up @@ -90,7 +89,6 @@ exports[`Helpers should expose a window object: window object 1`] = `
"memo": [Function],
"noop": [Function],
"noopPromise": Promise {},
"noopTranslate": [Function],
"numberDisplay": [Function],
"objFreeze": [Function],
}
Expand Down Expand Up @@ -146,7 +144,6 @@ exports[`Helpers should have specific functions: helpers 1`] = `
"memo": [Function],
"noop": [Function],
"noopPromise": Promise {},
"noopTranslate": [Function],
"numberDisplay": [Function],
"objFreeze": [Function],
}
Expand Down
24 changes: 0 additions & 24 deletions src/common/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,6 @@ const noop = Function.prototype;
*/
const noopPromise = Promise.resolve({});

/**
* A placeholder for "t", translation method.
* Associated with the i18n package, and typically used as a default prop.
*
* @param {string|Array} key
* @param {string|object|Array} value
* @param {Array} components
* @returns {string}
*/
const noopTranslate = (key, value, components) => {
const updatedKey = (Array.isArray(key) && `[${key}]`) || key;
const updatedValue =
(typeof value === 'string' && value) ||
(Array.isArray(value) && `[${value}]`) ||
(Object.keys(value || '').length && JSON.stringify(value)) ||
'';
const updatedComponents = (components && `${components}`) || '';

return `t(${updatedKey}${(updatedValue && `, ${updatedValue}`) || ''}${
(updatedComponents && `, ${updatedComponents}`) || ''
})`;
};

/**
* ToDo: review adding "locale" for numbro
*/
Expand Down Expand Up @@ -434,7 +411,6 @@ const helpers = {
memo,
noop,
noopPromise,
noopTranslate,
numberDisplay,
objFreeze,
DEV_MODE,
Expand Down
116 changes: 109 additions & 7 deletions src/components/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2701,18 +2701,111 @@ Default props.

* [i18nHelpers](#i18n.module_i18nHelpers)
* [~EMPTY_CONTEXT](#i18n.module_i18nHelpers..EMPTY_CONTEXT) : <code>string</code>
* [~translate(translateKey, values, components, options)](#i18n.module_i18nHelpers..translate) ⇒ <code>string</code> \| <code>React.ReactNode</code>
* [~translateComponent(Component)](#i18n.module_i18nHelpers..translateComponent) ⇒ <code>React.ReactNode</code>
* [~noopTranslate(key, value, components)](#i18n.module_i18nHelpers..noopTranslate) ⇒ <code>string</code>
* [~splitContext(value, settings)](#i18n.module_i18nHelpers..splitContext) ⇒ <code>string</code> \| <code>Array.&lt;string&gt;</code>
* [~parseContext(translateKey, context, settings)](#i18n.module_i18nHelpers..parseContext) ⇒ <code>Object</code>
* [~parseTranslateKey(translateKey)](#i18n.module_i18nHelpers..parseTranslateKey) ⇒ <code>\*</code>
* [~translate(translateKey, values, components, settings)](#i18n.module_i18nHelpers..translate) ⇒ <code>string</code> \| <code>React.ReactNode</code>
* [~translateComponent(Component, settings)](#i18n.module_i18nHelpers..translateComponent) ⇒ <code>React.ReactNode</code>

<a name="i18n.module_i18nHelpers..EMPTY_CONTEXT"></a>

### i18nHelpers~EMPTY\_CONTEXT : <code>string</code>
Check to help provide an empty context.

**Kind**: inner constant of [<code>i18nHelpers</code>](#i18n.module_i18nHelpers)
<a name="i18n.module_i18nHelpers..noopTranslate"></a>

### i18nHelpers~noopTranslate(key, value, components) ⇒ <code>string</code>
A placeholder for "t", translation method.
Associated with the i18n package, and typically used as a default prop.

**Kind**: inner method of [<code>i18nHelpers</code>](#i18n.module_i18nHelpers)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>key</td><td><code>string</code> | <code>Array</code></td>
</tr><tr>
<td>value</td><td><code>string</code> | <code>object</code> | <code>Array</code></td>
</tr><tr>
<td>components</td><td><code>Array</code></td>
</tr> </tbody>
</table>

<a name="i18n.module_i18nHelpers..splitContext"></a>

### i18nHelpers~splitContext(value, settings) ⇒ <code>string</code> \| <code>Array.&lt;string&gt;</code>
Split a string on underscore.

**Kind**: inner method of [<code>i18nHelpers</code>](#i18n.module_i18nHelpers)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>value</td><td><code>string</code></td>
</tr><tr>
<td>settings</td><td><code>object</code></td>
</tr><tr>
<td>settings.emptyContextValue</td><td><code>string</code></td>
</tr> </tbody>
</table>

<a name="i18n.module_i18nHelpers..parseContext"></a>

### i18nHelpers~parseContext(translateKey, context, settings) ⇒ <code>Object</code>
Parse extend context arrays/lists, and apply values to a concatenated translate key.

**Kind**: inner method of [<code>i18nHelpers</code>](#i18n.module_i18nHelpers)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>translateKey</td><td><code>string</code> | <code>Array</code></td>
</tr><tr>
<td>context</td><td><code>*</code> | <code>string</code> | <code>Array</code></td>
</tr><tr>
<td>settings</td><td><code>object</code></td>
</tr><tr>
<td>settings.emptyContextValue</td><td><code>string</code></td>
</tr><tr>
<td>settings.splitContext</td><td><code>function</code></td>
</tr> </tbody>
</table>

<a name="i18n.module_i18nHelpers..parseTranslateKey"></a>

### i18nHelpers~parseTranslateKey(translateKey) ⇒ <code>\*</code>
Parse a translation key. If an array, filter for defined strings.

**Kind**: inner method of [<code>i18nHelpers</code>](#i18n.module_i18nHelpers)
<table>
<thead>
<tr>
<th>Param</th><th>Type</th>
</tr>
</thead>
<tbody>
<tr>
<td>translateKey</td><td><code>*</code> | <code>string</code> | <code>Array</code></td>
</tr> </tbody>
</table>

<a name="i18n.module_i18nHelpers..translate"></a>

### i18nHelpers~translate(translateKey, values, components, options) ⇒ <code>string</code> \| <code>React.ReactNode</code>
### i18nHelpers~translate(translateKey, values, components, settings) ⇒ <code>string</code> \| <code>React.ReactNode</code>
Apply a string towards a key. Optional replacement values and component/nodes.
See, https://react.i18next.com/

Expand All @@ -2734,16 +2827,21 @@ See, https://react.i18next.com/
<td>components</td><td><code>Array</code></td><td></td><td><p>An array of HTML/React nodes used to replace string tokens. i.e. &quot;[<span />, &lt;React.Fragment /&gt;]&quot;</p>
</td>
</tr><tr>
<td>options</td><td><code>object</code></td><td></td><td></td>
<td>settings</td><td><code>object</code></td><td></td><td></td>
</tr><tr>
<td>options.emptyContextValue</td><td><code>string</code></td><td></td><td><p>Check to allow an empty context value.</p>
</td>
<td>settings.isDebug</td><td><code>function</code></td><td></td><td></td>
</tr><tr>
<td>settings.noopTranslate</td><td><code>function</code></td><td></td><td></td>
</tr><tr>
<td>settings.parseContext</td><td><code>function</code></td><td></td><td></td>
</tr><tr>
<td>settings.parseTranslateKey</td><td><code>function</code></td><td></td><td></td>
</tr> </tbody>
</table>

<a name="i18n.module_i18nHelpers..translateComponent"></a>

### i18nHelpers~translateComponent(Component) ⇒ <code>React.ReactNode</code>
### i18nHelpers~translateComponent(Component, settings) ⇒ <code>React.ReactNode</code>
Apply string replacements against a component, HOC.

**Kind**: inner method of [<code>i18nHelpers</code>](#i18n.module_i18nHelpers)
Expand All @@ -2756,6 +2854,10 @@ Apply string replacements against a component, HOC.
<tbody>
<tr>
<td>Component</td><td><code>React.ReactNode</code></td>
</tr><tr>
<td>settings</td><td><code>object</code></td>
</tr><tr>
<td>settings.noopTranslate</td><td><code>function</code></td>
</tr> </tbody>
</table>

Expand Down
18 changes: 9 additions & 9 deletions src/components/i18n/__tests__/__snapshots__/i18n.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ exports[`I18n Component should generate a predictable locale key output snapshot
},
],
},
{
"file": "./src/common/helpers.js",
"keys": [
{
"key": "",
"match": "t(\${updatedKey}\${(updatedValue && \`, \${updatedValue}\`)",
},
],
},
{
"file": "./src/components/authentication/authentication.js",
"keys": [
Expand Down Expand Up @@ -143,6 +134,15 @@ exports[`I18n Component should generate a predictable locale key output snapshot
},
],
},
{
"file": "./src/components/i18n/i18nHelpers.js",
"keys": [
{
"key": "",
"match": "t(\${updatedKey}\${(updatedValue && \`, \${updatedValue}\`)",
},
],
},
{
"file": "./src/components/inventoryCard/inventoryCard.js",
"keys": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ exports[`I18nHelpers should attempt to perform translate with a node: translated
exports[`I18nHelpers should have specific functions: i18nHelpers 1`] = `
{
"EMPTY_CONTEXT": "LOCALE_EMPTY_CONTEXT",
"noopTranslate": [Function],
"parseContext": [Function],
"parseTranslateKey": [Function],
"splitContext": [Function],
"translate": [Function],
"translateComponent": [Function],
}
Expand Down
Loading

0 comments on commit 617d1a5

Please sign in to comment.